我有一个我在ngtable中使用的数组,但需要帮助过滤数据我点击标题时无法排序。请帮忙
HTML
<table class="table" ng-table="namingConventionParams" show-filter="true">
<tr ng-repeat="item in $data | orderBy:'toString()'">
<td style="word-break:break-all" data-title="items.tableHeader" align="left" filter="{ '0': 'text' }" sortable="'valueOf()[0]'">{{item}}</td>
</tr>
</table>
控制器
$scope.namingConventionParams = new ngTableParams({
page: 1, // show first page
count: 10
}, {
defaultSort: "asc",
total: $scope.items.instanceData.length, // length of data
counts: [],
getData: function($defer, params) {
var data = $scope.items.instanceData;
var orderedData = params.filter() ? $filter('filter')(data, params.filter()) : data;
orderedData = params.sorting() ? $filter('orderBy')(orderedData, params.orderBy()) : orderedData;
params.total(orderedData.length);
$defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
}
});
答案 0 :(得分:1)
您的代码存在的问题是您的数据数组只包含字符串而不是对象,因此当您拥有sortable="valueOf()[0]"
解决方案是使用对象。以下是需要更新的相关代码:
HTML
<td ... filter="{'name':'text'}" sortable="name">{{item.name}}</td>
JS
var data = $scope.items.instanceData.map(function(text) {
return {name: text};
});