Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-repeat with multiple filters on large data set

Tags:

angularjs

I'm still new to AngularJS, so I'm just trying to do a simple CRUD app. Currently I pull the data (in a JSON file) with $http in a div handled by a controller MyCtrl1.

function MyCtrl1($scope, $http) {     $http.get('data/accounts.json').success(function(data) {         $scope.accounts = data;     ...     });  } 

Inside this div is a table with the following tbody:

<tbody>     <tr ng-repeat="account in accounts | orderBy:sort.field:sort.desc | startFrom:currentPage * pageSize | limitTo:pageSize">         <td contentEditable="true" ng-repeat="(label, value) in account" ng-show="fields[label].visible">{{value}}</td>     </tr> </tbody> 

The orderBy filter sorts according to a selected field; startFrom slices the array to start at a certain point; limitTo filters according to a preset page size. Without the pagination filters the performance was pretty terrible, but I was wonder if there was an alternative way to go about this?

I have Batarang for Chrome and under the Performance tab it showed ngRepeatWatch taking up the most time, and I reckon it has to do with all the filtering I'm doing..

like image 986
actaeon Avatar asked Jan 02 '13 18:01

actaeon


People also ask

Is the correct way to apply multiple filters in Angular JS?

Answer: A is the correct option. The syntax of applying multiple filters in AngularJS can be written as: {{ expression | filter1 | filter2 | ... }}

What can I use instead of NG-repeat?

You can consider using transclusion inside a custom directive, to achieve the behavior you are looking for without using ng-repeat.

How do I filter in NG-repeat?

The ng-repeat values can be filtered according to the ng-model in AngularJS by using the value of the input field as an expression in a filter. We can set the ng-model directive on an input field to filter ng-repeat values.

Does ng-repeat create a new scope?

Directives that Create Scopes In most cases, directives and scopes interact but do not create new instances of scope. However, some directives, such as ng-controller and ng-repeat, create new child scopes and attach the child scope to the corresponding DOM element.


2 Answers

{{ expression | filter1 | filter2 }}

Just use

<tr ng-repeat="account in accounts | filter1 | filter2 | filter3" >   <td contentEditable="true" ng-repeat="(label, value) in account" ng-show="fields[label].visible">{{value}}</td> </tr> 

Angular 2 uses pipes, but its looks like filters:

<div>The chained hero's birthday is <p>{{  birthday | date:'fullDate' | uppercase}}</p> <div> 
like image 79
neftedollar Avatar answered Sep 22 '22 20:09

neftedollar


I know this question is old but for anyone in the future. Filtering in line is expensive (computationally) because it works directly on the DOM, if you are dealing with large amounts of data, 1000+ rows, it is much better to filter your collection in your controller then repeat on that instead.

like image 28
Robert Cadmire Avatar answered Sep 22 '22 20:09

Robert Cadmire