Javascript 带搜索查询和移动到服务的角度分页
我有一个用户列表,我正在尝试对表进行分页。该表使用了一个ng重复,并应用了多个过滤器 下表所示:Javascript 带搜索查询和移动到服务的角度分页,javascript,angularjs,pagination,Javascript,Angularjs,Pagination,我有一个用户列表,我正在尝试对表进行分页。该表使用了一个ng重复,并应用了多个过滤器 下表所示: <input class='form-control' id='admin-search' ng-model='query' ng-change='setPage(0)'> <table class='table table-striped'> <thead> <tr> <th>Last Name</th&g
<input class='form-control' id='admin-search' ng-model='query' ng-change='setPage(0)'>
<table class='table table-striped'>
<thead>
<tr>
<th>Last Name</th>
<th>First Name</th>
<th>Username</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr ng-repeat='user in users | filter:query | pagination:currentPage * numPages() | limitTo: pageSize | orderBy:Sort.getOrderProp()'>
<td>{{user.last}}</td>
<td>{{user.first}}</td>
<td>{{user.username}}</td>
<td>{{user.email}}</td>
</tr>
</tbody>
</table>
<ul class='pagination'>
<li><a ng-click='previousPage()'>«</a></li>
<li ng-repeat='n in range()'><a ng-click='setPage(n)'>{{n + 1}}</a></li>
<li><a ng-click='nextPage()'>»</a></li>
</ul>
现在,我所有的分页代码都在我的用户控制器中。因为它将在其他控制器中使用,所以我想将它的大部分移动到服务中,但这样做会破坏它,并且无法正确更新
# Set up pagination
$scope.currentPage = 0
$scope.pageSize = 5
$scope.numPages = ->
Math.ceil($scope.users.length / $scope.pageSize) - 1
$scope.nextPage = ->
if $scope.currentPage < $scope.numPages()
$scope.currentPage++
$scope.previousPage = ->
if $scope.currentPage > 0
$scope.currentPage--
$scope.setPage = (page) ->
$scope.currentPage = page
$scope.range = ->
rangeSize = if 5 > $scope.numPages() then ($scope.numPages() + 1) else 5;
ret = [];
start = $scope.currentPage;
if start > ($scope.numPages() - rangeSize)
start = $scope.numPages() - rangeSize + 1;
i = start
while i < (start + rangeSize)
ret.push i
i++
return ret;
我尝试将上面的代码移动到一个名为pagination的服务中,并将其注入到我的用户控制器中,但如果用户进行了过滤,则它根本不会更新
基本上,我的两个问题是:
如果对数据执行搜索,如何重置分页
对分页元素重新编号
将尽可能多的分页代码移到
尽可能地提供服务。
您自己实现分页有什么特别的原因吗?您是否考虑过使用AngularUI引导分页指令?Go to-find for pagination我一直在研究这个问题,除非我弄错了,它只是pagination元素的包装器。我仍然需要为我的表中的内容创建一个过滤器,并遇到许多我已经遇到的问题。
# Set up pagination
$scope.currentPage = 0
$scope.pageSize = 5
$scope.numPages = ->
Math.ceil($scope.users.length / $scope.pageSize) - 1
$scope.nextPage = ->
if $scope.currentPage < $scope.numPages()
$scope.currentPage++
$scope.previousPage = ->
if $scope.currentPage > 0
$scope.currentPage--
$scope.setPage = (page) ->
$scope.currentPage = page
$scope.range = ->
rangeSize = if 5 > $scope.numPages() then ($scope.numPages() + 1) else 5;
ret = [];
start = $scope.currentPage;
if start > ($scope.numPages() - rangeSize)
start = $scope.numPages() - rangeSize + 1;
i = start
while i < (start + rangeSize)
ret.push i
i++
return ret;