0

I am trying to provide a pagination to a table:
Let's say there are 100 records and records per page is 10. If I am in 3rd page, records from 21 to 30 should be prepared in the table.

So I am trying to filter those records using angular filter but couldn't succeed. I tried multiple ways:

  1. <tr on-last-repeat ng-repeat="record in data | filter:( $index>= fromRow && $index <= toRow)" role="row">
  2. Created a custom filter but unable to pass either $index or record to the filter. If record could be passed to the filter, so that I can get the index by records.indexOf(record).

Trying to pass $index:

<tr on-last-repeat ng-repeat="record in data | paging:$index:fromRow:toRow track by $index" role="row">

I found a solution in StackOverflow which is named as native filter:

<tr on-last-repeat ng-repeat="record in data | filter:filterPaging()" role="row">

$scope.filterPaging = function(){
    return function(record){
        var idx = $scope.data.indexOf(record)+1;
        if(idx >= $scope.fromRow && idx <= $scope.toRow) return true;
        return false;
    }
};

But this function is getting called so many times. My table has 4 columns and 10 records and the function is being called for 40 times. I am concerning about performance if there are thousands of records.

Can somebody please take few minutes to have a look on this.

5
  • It may help you but not much sure stackoverflow.com/questions/30750068/… Commented Jun 12, 2015 at 7:15
  • Do you know there is built-in limitTo filter? Btw. on our projects we always ended up filtering arrays on-demand in the controller so that the ngRepeat does not use any filters. Commented Jun 12, 2015 at 7:26
  • @PavelHoral Well no, I have never heard. Thanks, it worked well. BTW, still I am curious to know if there is any way to pass either $index or looped item to filter. Can you keep the comment as answer so that I can mark it as 'Accepted'. Commented Jun 12, 2015 at 8:49
  • 1
    Unfortunately the filter works before any child scope (hence $index or item) is created. So no, you can not use those in the ngRepeat expression. Commented Jun 12, 2015 at 8:54
  • 1
    it is happening because your filter method is getting call on each digest call.. Commented Jun 12, 2015 at 9:50

2 Answers 2

1

There is built-in limitTo filter doing exactly what you are trying to achieve.

By the way on our projects we always ended up filtering arrays on-demand in the controller (or custom directive) so that the ngRepeat doesn't have to use any filters.

Sign up to request clarification or add additional context in comments.

Comments

0

Why don't you store all the data in an array, then filter it only when fromRow or toRow change?

var fullData = $scope.data;
$scope.$watch('fromRow', function(){
        // frowRow minus 1 to get the index
        $scope.data = fullData.splice(fromRow - 1, toRow);
});

1 Comment

since it is bindable data and user is allowed to change data, so it is difficult to syncronize data b/w data and fullData. It gives more pain than filter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.