【问题标题】:How to keep ng-repeat's $index count while using angular-ui-bootstrap uib-pagination in angularjs如何在angularjs中使用angular-ui-bootstrap uib-pagination时保持ng-repeat的$index计数
【发布时间】:2018-06-04 23:44:08
【问题描述】:

我正在使用 AngularJS v1.6.6 和 angular-ui-bootstrap 版本:2.5.0 创建使用 ng-repeat 指令构建的分页列表。

分页有效,但每次页面更改 $index 变量计数重新开始时,我需要将此值作为参数传递给函数,因此,例如,如果我单击第二页的第一项,我需要传递该值10.

看到这个小提琴:http://jsfiddle.net/elenat82/vLfLtrxc/

HTML:

<div ng-app="myApp">
<div ng-controller="someController">
<div>list.length = {{list.length}}</div>
<div>currentPage = {{currentPage}}</div>
<div>itemPage = {{itemPage}}</div>
<div>maxSize = {{maxSize}}</div>
<br><br><br><br>
<table>
    <tbody>
        <tr ng-repeat="item in list | pagination:(currentPage-1)*itemPage | limitTo:itemPage track by $index">
            <td>{{ $index +1 }}</td>
            <td>{{ item.task }}</td>
        </tr>
    </tbody>
</table>
<div>
    <ul uib-pagination 
    total-items="list.length" ng-model="currentPage" max-size="maxSize" boundary-links="true" boundary-link-numbers="true" force-ellipses="true" items-per-page="itemPage" first-text="First" last-text="Last" next-text="Next" previous-text="Prev">
    </ul>
</div>

JS:

var app = angular.module('myApp', ['ui.bootstrap']);

app.controller('someController', function($scope, $filter) {
$scope.list = [
{task: "task n. 1"},
{task: "task n. 2"},
{task: "task n. 3"},
{task: "task n. 4"},
{task: "task n. 5"},
{task: "task n. 6"},
{task: "task n. 7"},
{task: "task n. 8"},
{task: "task n. 9"},
{task: "task n. 10"},
{task: "task n. 11"},
{task: "task n. 12"},
{task: "task n. 13"},
{task: "task n. 14"},
{task: "task n. 15"},
{task: "task n. 16"},
{task: "task n. 17"},
{task: "task n. 18"},
{task: "task n. 19"},
{task: "task n. 20"}
];

$scope.currentPage = 1;
$scope.itemPage = 5;
$scope.maxSize = 5;

});

app.filter('pagination', function () {
  return function (input, start) {
  start = +start;
    if (input) {
        return input.slice(start);
    }
    else {
        return input;
    }
};
});

【问题讨论】:

  • 以下答案是正确的。请在下面选择这个。

标签: angularjs angular-ui-bootstrap


【解决方案1】:

遗憾的是,我担心没有内置的方法可以做到这一点。

我会使用一个简单的indexOf 来检索元素的绝对位置:

<tr ng-repeat="item in list | pagination:(currentPage-1)*itemPage | limitTo:itemPage track by $index">
     <td>{{ list.indexOf(item) + 1 }}</td>
     <td>{{ item.task }}</td>
</tr>

基于您的示例的简单小提琴:http://jsfiddle.net/zj9c6d52/

【讨论】:

  • 谢谢 Luxor001,您的解决方案也有效,这里是完整的示例:jsfiddle.net/elenat82/vLfLtrxc/16。太糟糕了,我不能接受一个以上的答案......
  • 不用担心,接受最适合您的用例的答案,这就是 stackoverflow 存在的目的。
【解决方案2】:

您能否像check with this fiddler 那样在您的示例场景中尝试以下代码更改。

模板:

<td>{{ $index + serial }}</td>

控制器:

  $scope.serial = 1;
  $scope.$watch('currentPage', function(){
    $scope.serial = $scope.currentPage * $scope.itemPage - ($scope.itemPage-1);
  });

【讨论】:

  • 谢谢伊曼纽尔,ito 工作,这里是完整的例子:jsfiddle.net/elenat82/vLfLtrxc/12
  • 一旦你开始过滤那个数组,它就会中断
  • @svarog 在这里你有一个带有过滤功能的工作示例,这个plunker链接。
【解决方案3】:

像这样在 DOM 中快速计算它怎么样? :

<td>{{ $index +1 + (currentPage-1)*itemPage }}</td>

【讨论】:

  • 谢谢 Andrew,它可以工作,我什至不需要使用其他变量或函数,这是使用您的解决方案的完整示例:jsfiddle.net/elenat82/vLfLtrxc/14。抱歉,当我已经接受了另一个答案时,我看到了您的评论。
  • 你仍然可以接受这个,但无论如何很乐意提供帮助! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-24
  • 2015-05-05
相关资源
最近更新 更多