Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS : Custom filters and ng-repeat

I'm an AngularJS newbie and I'm building up a small proof-of-concept car hire listings app that pulls in some JSON and renders out various bits of that data via an ng-repeat, with a couple of filters:

   <article data-ng-repeat="result in results | filter:search" class="result">         <header><h3>{{result.carType.name}}, {{result.carDetails.doors}} door, &pound;{{result.price.value}} - {{ result.company.name }}</h3></header>             <ul class="result-features">                 <li>{{result.carDetails.hireDuration}} day hire</li>                 <li data-ng-show="result.carDetails.airCon">Air conditioning</li>                 <li data-ng-show="result.carDetails.unlimitedMileage">Unlimited Mileage</li>                 <li data-ng-show="result.carDetails.theftProtection">Theft Protection</li>             </ul>     </article>      <h2>Filters</h2>      <h4>Doors:</h4>      <select data-ng-model="search.carDetails">         <option value="">All</option>         <option value="2">2</option>         <option value="4">4</option>         <option value="9">9</option>     </select>      <h4>Provider:</h4>     Atlas Choice <input type="checkbox"  data-ng-model="search.company" ng-true-value="Atlas Choice" ng-false-value="" value="Atlas Choice" /><br>     Holiday Autos <input type="checkbox"  data-ng-model="search.company" ng-true-value="Holiday Autos" ng-false-value="" value="Holiday Autos" /><br>     Avis <input type="checkbox"  data-ng-model="search.company" ng-true-value="Avis" ng-false-value="" value="Avis" /><br>       

Now I want to create a custom filter in my controller, that can iterate over the items in my ng-repeat and return only the items that meet certain criteria - for example, I might create an array of values based on which 'provider' checkboxes are checked, then evaluate each ng-repeat item against that. I just can't work out how I'd do that though, in terms of the syntax - can anyone help?

Here's my Plunker: http://plnkr.co/edit/lNJNYagMC2rszbSOF95k?p=preview

like image 508
ParkerDigital Avatar asked May 15 '13 10:05

ParkerDigital


People also ask

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.

What is Ng-repeat in AngularJS?

AngularJS ng-repeat Directive The ng-repeat directive repeats a set of HTML, a given number of times. The set of HTML will be repeated once per item in a collection. The collection must be an array or an object. Note: Each instance of the repetition is given its own scope, which consist of the current item.

What is the correct way to apply multiple filters in AngularJS?

Filters can be applied to the result of another filter. This is called "chaining" and uses the following syntax: {{ expression | filter1 | filter2 | ... }} E.g. the markup {{ 1234 | number:2 }} formats the number 1234 with 2 decimal points using the number filter.

What is custom filter in AngularJS?

Introduction to AngularJS Custom Filter. In AngularJS filters are used to modify or update the data before rendering the data on view or UI. Filters are clubbed in expression or directives using pipe (|) symbol.


1 Answers

If you want to run some custom filter logic you can create a function which takes the array element as an argument and returns true or false based on whether it should be in the search results. Then pass it to the filter instruction just like you do with the search object, for example:

JS:

$scope.filterFn = function(car) {     // Do some tests      if(car.carDetails.doors > 2)     {         return true; // this will be listed in the results     }      return false; // otherwise it won't be within the results }; 

HTML:

... <article data-ng-repeat="result in results | filter:search | filter:filterFn" class="result"> ... 

As you can see you can chain many filters together, so adding your custom filter function doesn't force you to remove the previous filter using the search object (they will work together seamlessly).

like image 98
mirrormx Avatar answered Oct 04 '22 20:10

mirrormx