【发布时间】:2014-12-06 01:41:38
【问题描述】:
我正在尝试使用 ngTable 将来自 REST Web 服务的数据填充到表中。 显示了数据,但它们既不可排序也不可过滤。过滤总是返回空列表。
这是我引用的 API 的链接:http://bazalt-cms.com/ng-table/
JS:
$scope.dataInstances = [];
$scope.getDataInstances = function() {
$http({
method : 'GET',
url : '/rest/v1/data/instances',
headers : {
"Authorization" : "Basic " + btoa("USERNAME" + ":" + "PASSWORD")
},
})
.success(function(data, status) {
$scope.dataInstances = data;
$scope.tableParams.reload();
// just some logging
console.log(JSON.stringify(data));
})
.error(function(data, status) {
alert("Error: " + status);
});
};
$scope.tableParams = new ngTableParams({
page: 1, // show first page
count: 10, // count per page
filter: {
},
sorting: {
date: 'asc' // initial sorting
}
},
{
total: $scope.dataInstances.length, // length of data
getData: function($defer, params) {
// use build-in angular filter
var filteredData = params.filter() ?
$filter('filter')($scope.dataInstances, params.filter()) :
$scope.dataInstances;
var orderedData = params.sorting() ?
$filter('orderBy')(filteredData, params.orderBy()) :
$scope.dataInstances;
params.total(orderedData.length); // set total for recalc pagination
$defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
}
});
HTML:
<div ng-controller="myController" ng-init="getDataInstances()">
<table ng-table="tableParams" show-filter="true" class="table table-condensed">
<tr ng-repeat="dataInstance in dataInstances">
<td data-title="'Name'" sortable="'name'" filter="{ 'name' : 'text'}">{{dataInstance.name}}</td>
<td data-title="'Date'" sortable="'date'" filter="{ 'date' : 'text'}">{{dataInstance.date | date:'dd.MM.yyyy HH:mm' }}</td>
<td data-title="'Type'" sortable="'type'" filter="{ 'type' : 'text'}">{{dataInstance.type}}</td>
</tr>
</table>
</div>
您有任何提示如何使其正常工作吗? 尝试了不同的其他可能的解决方案 - 没有一个有效 - 比如:
- asynchronously populating an AngularJS ngTable with json data
- Loading JSON via AJAX with NgTable parameters
- Sorting ngTable doesn't work when heading gets clicked
提前致谢!
【问题讨论】:
-
是否对
type和name列进行过滤和排序? -
没有。它一般不起作用。
-
plunk在这里会有用,你能设置一下吗?
-
已编辑帖子,带有指向 plunk 的链接
-
这很有帮助
标签: json angularjs sorting filtering ngtable