代码之家  ›  专栏  ›  技术社区  ›  elenat82

如何在angularjs中使用AngularUI引导uib分页时保持ng repeat的$索引计数

  •  3
  • elenat82  · 技术社区  · 8 年前

    我正在使用AngularJS v1.6.6和AngularUI引导版本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;
        }
    };
    });
    
    3 回复  |  直到 8 年前
        1
  •  3
  •   illeb    8 年前

    遗憾的是,我担心没有现成的方法来做这件事。

    我会用一个简单的 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/

        2
  •  1
  •   Immanuel Kirubaharan    8 年前

    您能否尽可能在代码中尝试以下更改 check with this fiddler 使用您的示例场景。

    模板:

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

    控制器:

      $scope.serial = 1;
      $scope.$watch('currentPage', function(){
        $scope.serial = $scope.currentPage * $scope.itemPage - ($scope.itemPage-1);
      });
    
        3
  •  1
  •   Andrew Adam    8 年前

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

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