elenat82
elenat82

Reputation: 37

How to keep ng-repeat's $index count while using angular-ui-bootstrap uib-pagination in angularjs

I am using AngularJS v1.6.6 and angular-ui-bootstrap Version: 2.5.0 to create a paginated list built using ng-repeat directive.

Pagination works but every time the page changes the $index variable count starts again, I need to pass this value as parameter in a function so, for example, if I click on first item of second page I need to pass the value 10.

See this fiddle: 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;
    }
};
});

Upvotes: 3

Views: 1083

Answers (3)

Andrew Adam
Andrew Adam

Reputation: 1582

How about just quickly calculating it in the DOM like this? :

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

Upvotes: 1

Immanuel Kirubaharan
Immanuel Kirubaharan

Reputation: 1094

Can you try something like the below changes in your code as you can check with this fiddler with your example scenario.

Template:

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

Controller:

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

Upvotes: 1

illeb
illeb

Reputation: 2947

Sadly, i fear there is no buit-in way to do it.

I would use a simple indexOf to retrieve the absolute position of your element:

<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>

Simple fiddle based on your example: http://jsfiddle.net/zj9c6d52/

Upvotes: 3

Related Questions