2
我無法理解。如何在分頁時進行正確的搜索? 英文寫作不好。 我這樣做:如何在分頁時進行正確搜索
var app = angular.module('appTelDirectory', []);
app.controller('directoryList', function($scope) {
$scope.currentPage = 0;
$scope.pageSize = 10;
$scope.users = [{}]
$scope.numberOfPages = function() {
return Math.ceil($scope.users.length/$scope.pageSize);
}
for (var i = 0; i < 45; i++) {
$scope.users.push({
'name': 'user' + i
});
}
});
app.filter('startFrom', function() {
return function(input, start) {
start = +start; //parse to int
return input.slice(start);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="appTelDirectory" ng-controller="directoryList">
<input placeholder="Поиск..." ng-model="searchAll" class="form-control">
<ul>
<li ng-repeat="item in users | filter:searchAll | startFrom:currentPage*pageSize | limitTo:pageSize">{{item.name}}</li>
</ul>
<table>
<tr ng-repeat="item in users | startFrom:currentPage*pageSize | limitTo:pageSize">
</table>
<button ng-disabled="currentPage == 0" ng-click="currentPage=currentPage-1">Previous</button>
{{currentPage+1}}/{{numberOfPages()}}
<button ng-disabled="currentPage >= users.length/pageSize - 1" ng-click="currentPage=currentPage+1">
Next
</button>
</div>
如何更改項目的數量,取決於用戶列表上。 NumberOfPages不變...
如何添加例如按類別搜索? https://jsfiddle.net/0hqmocvw/17/ – JohnLemon
默認情況下,$ filter中的$'匹配所有內容。所以如果你只是想按類別過濾,你可以用''category''替換'$'。這在https://docs.angularjs.org/api/ng/filter/filter –
中得到了正確的解釋,我沒有理解鏈接。它如何連接到filteredUsers – JohnLemon