【发布时间】:2015-09-09 10:31:06
【问题描述】:
如何为 ngTable 选择过滤器设置预选选项?我的代码如下,它类似于官方 ngTable 页面中的example。现在选择的值是一个空选项,我希望默认情况下预先选择第一个带有值的选项(显示表中所有数据的选项):
$scope.customersTable = new ngTableParams({
page: 1, // show first page
count: 10,
sorting: {
importDate: 'desc'
}
}, {
total: $scope.customers.length, // length of data
getData: function ($defer, params) {
var orderedData = params.sorting() ?
$filter('orderBy')($scope.customers, params.orderBy()) :
$scope.customers;
orderedData = params.filter ?
$filter('filter')(orderedData, params.filter()) :
orderedData;
$scope.custs = orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count());
params.total(orderedData.length);
$defer.resolve($scope.custs);
}
});
var inArray = Array.prototype.indexOf ?
function (val, arr) {
return arr.indexOf(val)
} :
function (val, arr) {
var i = arr.length;
while (i--) {
if (arr[i] === val) return i;
}
return -1
};
$scope.importDates = function () {
var def = $q.defer(),
arr = [],
importDates = [];
$scope.$watch('customers', function () {
angular.forEach($scope.customers, function (item) {
if (inArray(item.importDate, arr) === -1) {
arr.push(item.importDate);
importDates.push({
'id': item.importDate,
'title': item.importDate
});
}
});
});
def2.resolve(importDates);
return def2;
};
还有 HTML:
<table ng-table="customersTable" show-filter="true" class="table table-hover">
<tbody>
<tr data-ng-repeat="customer in $data">
<td data-title="'First Name'" sortable="'firstName'">
{{customer.firstName}}
</td>
<td data-title="'Import Date'" sortable="'importDate'" filter="{ 'importDate': 'select' }" filter-data="importDates()">
{{customer.importDate | date}}
</td>
</tr>
</tbody>
</table>
【问题讨论】:
标签: angularjs angularjs-filter ngtable