[英]Angular search filter doesn't search in entire table
问题是搜索过滤器仅从当前页中查找记录,而我希望它从整个表中查找记录。 我怎样才能做到这一点?
<input type="text" placeholder="Search By Any..." ng-model="search.$"/>
<table dir="ltr" width="477" border="1" class="table table-striped table-bordered">
<thead>
<tr>
<th><a style="font-size: 16px;" href="#" ng-click="changeSort('name')">User</a></th>
<th><a style="font-size: 16px;" href="#" ng-click="changeSort('contentType')">Content Type</a></th>
<th><a style="font-size: 16px;" href="#" ng-click="changeSort('contentName')">Content Name</a></th>
<th><a style="font-size: 16px;" href="#" ng-click="changeSort('startTime')">Start Time</a></th>
<th><a style="font-size: 16px;" href="#" ng-click="changeSort('endTime')">End Time</a></th>
<th><a style="font-size: 16px;" href="#" ng-click="changeSort('duration')">Duration(In Secs)</a></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="record in filteredRecords | filter: search | orderBy:sort:reverse track by $index">
<td>{{record.user}}</td>
<td>{{record.contentType}}</td>
<td>{{record.contentName}}</td>
<td>{{record.startTime}}</td>
<td>{{record.endTime}}</td>
<td>{{record.duration}}</td>
</tr>
</tbody>
</table>
<pagination class="pull-right" style="cursor: pointer;" num-pages="numPages()" current-page="currentPage" on-select-page="pageChanged(page)"></pagination>
角度代码:
angular.module("contentViewStatusApp")
.controller("contentViewStatusController", function($scope, contentViewStatusService){
$scope.records = contentViewStatusService.list();
$scope.currentPage = 1
$scope.numPerPage = 10
$scope.maxSize = 5;
$scope.numPages = function(){
return Math.ceil($scope.records.length / $scope.numPerPage);
};
$scope.changeSort = function(value){
if ($scope.sort == value){
$scope.reverse = !$scope.reverse;
return;
}
$scope.sort = value;
$scope.reverse = false;
}
$scope.$watch('currentPage + numPerPage', function(){
var begin = (($scope.currentPage - 1) * $scope.numPerPage), end = begin + $scope.numPerPage;
$scope.filteredRecords = $scope.records.slice(begin, end);
});
});
您可以使用过滤器进行分页,而不是创建另一个数组。 这样,它将在筛选之前搜索整个阵列。
此页面显示了使用过滤器进行分页的示例。
然后,您可以只先执行搜索查询,它应该搜索整个数组。
更新资料
这是一种具有分页功能的功能,可根据搜索文本进行更新。
它监视搜索文本并在过滤后更新页面范围:
$scope.$watch('searchText.name', function (v) {
$scope.currentPage = 0;
$scope.pages = $scope.range();
});
然后,基于过滤的结果更新pageCount
:
$scope.pageCount = function () {
var pages = $filter('filter')($scope.items, $scope.searchText);
return Math.ceil(pages.length / $scope.itemsPerPage);
};
是的,我同意@agreco,但我认为您必须以始终与search.$ model
一起使用的方式定义过滤的记录。 为此,请看一下这个小提琴 。
希望这能解决您的问题。 祝好运。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.