1

Hi I'm new in angularjs i just have question to ask.

this is my ng-repeat code.

<ul ng-repeat="todo in filteredData | filter:search">

And it seems that this code will only filter the filteredData not the alldata itself.

filteredData is always equal to 10 becuase i'am using a pagination in angularjs.

Now my question is there a way that i can filter alldata not just the filteredData? i tried

<tr ng-repeat="todo in filteredData | filter:alldata:search">

but is dosen't work.

Thanks

  • A filter is applied to a variable before '|'. So `filteredData` will be filtered. How do you get `filteredData`? – Gosha_Fighten Nov 28 '14 at 02:11
  • 1
    How does filteredData get created? That is where you need to appy the search filter. The `filter` filter just filters whatever the source is, so you would need to do `todo in alldata | filter:search`, then apply the pagination. – Jason Goemaat Nov 28 '14 at 02:12
  • @GoshaFighten i get the filteredData by dividing the allData/numPerPage. –  Nov 28 '14 at 02:17
  • @JasonGoemaat yeah but if i use allData it will display all and pagination won't work already –  Nov 28 '14 at 02:20
  • Think of it this way... If you go to a fruit stand and ask for ten apples and they give you 6 green and 4 red apples, then you take only the green apples you will only have 6 apples. You want to first ask for green apples, then take 10 of those. The filter needs to happen before your pagination. – Jason Goemaat Nov 28 '14 at 02:23

2 Answers2

1

The best way to do this is to create pagination as a filter. Then your code will be

<tr ng-repeat="todo in alldata | filter:search | pagination:pageNum:perPage">

Pagination filter:

module.filter('pagination', function(){
  return function(array, page, perPage){
    return array.slice(page * perPage, (page+1) * perPage);
  }
});

Full working codepen here: http://codepen.io/SimeonC/pen/gbpqNM?editors=101 (Excuse the jade and coffeescript - the eye above the editor shows you the compiled HTML/JS)

Simeon Cheeseman
  • 1,748
  • 1
  • 13
  • 24
0

you can use something like this let assume your full data list is stored in

$scope.allData

then you can uses in your <li>

<li ng-repeat="todo in filterdData = (allData | filter:search)"> </li>
madu
  • 344
  • 3
  • 14