Prasad
Prasad

Reputation: 1600

How can i Apply filter by taking the arrays First value as Input On check?

Iam Learning AngularJs ...

Example : -

My Json Having an array with some values as types :-

Lets Say A Restaurant would be Mexican or Italian Etc My example

 {
  name:'res 123',
  description:'Italian Rest'
   types:['Mexican','Indian']
   }

<input type="checkbox" data-ng-model="Mexican"/> // Iam Using Textbox  Oncheck Filter Need to Filter all the Objects with types:['Mexican'] 

 Filter Code :- 
 <div class="col-xs-12" data-ng-repeat="obj in objs| filter : objs.types[1]: Mexican" > <!-- Filter applied Like this -->
 Realted looping 
</div>

How can i Apply filter by taking the types:['Mexican'] value as Input for Filter On check ?

Upvotes: 0

Views: 65

Answers (1)

New Dev
New Dev

Reputation: 49590

A built-in filter in Angular accepts a hash that specifies by what properties to match against each element in an array.

So, if you have an array of restaurants:

var restaurants = [
  { name: "foo", types: ["Mexican", "Indian"] },
  { name: "bar", types: ["Mexican"] },
  { name: "baz", types: ["Italian"] }
];

then if you need to filter by name, the input to filter would be {name: 'b'} - which would give you "bar" and "baz".

Likewise, if you need to filter by types - even though it is an array - a similar approach would work: {types: "Mexican"}.

And so, you just need to construct that object - let's call it filterBy.

<input type="checkbox" ng-model="filterBy.types" 
       ng-true-value="'Mexican'"
       ng-false-value="undefined"> Mexican

<div ng-repeat="r in restaurants | filter: filterBy>
  {{r.name}}
</div>

Demo

Upvotes: 3

Related Questions