0

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 ?

Prasad
  • 1,562
  • 5
  • 26
  • 40
  • could you elaborate more what you want? – Pankaj Parkar May 04 '15 at 12:55
  • Hey Pankaj Please Take a Look i Just improved Question – Prasad May 04 '15 at 13:03
  • @PRASAD, so, you have a list of restaurants (`objs` in your example) and you want to filter them by type? – New Dev May 04 '15 at 13:05
  • Yes It may Consists more than one value in array but i need to filter by first value or any value of array . But i Prefer to Go with First Value – Prasad May 04 '15 at 13:06
  • What does it mean "by first value or any"? Doesn't that just reduce to "any value"? – New Dev May 04 '15 at 13:11
  • If you're specifically wondering how to filter out results, perhaps this could help: http://stackoverflow.com/questions/13464809/reverse-polarity-of-an-angular-js-filter – Ed B May 04 '15 at 13:13
  • Sorry for the Confusion .. i just need to filter by one of the Value Specifically . If you have suggestion on Filtering Multiple values I can use that for efficient Filtering :-) – Prasad May 04 '15 at 13:14
  • Just the built-in filter should work for the array just as it would if it was a primitive: https://docs.angularjs.org/api/ng/filter/filter. Have you looked at it? – New Dev May 04 '15 at 13:18
  • Yes I did But the Doc having Friends = [{obj},{obj}] But in My Scenario is types:['Mexican','Italian'] (types is embedded Field in Schema ).. where i stuck up – Prasad May 04 '15 at 13:20
  • 1
    Are you unable to change the array to a better-suited JSON format, like this? `[{type:'Mexican'}, {type:'Indian'}];` – Ed B May 04 '15 at 13:23
  • In the example, `friends` is equivalent to your `objs`, and `types` is one of the properties, like `name` in the "Friends" example – New Dev May 04 '15 at 13:24
  • @Ed B Yes may be i need to raise request for Schema Change – Prasad May 04 '15 at 13:26
  • @ New Dev let me Update My Model with some Fields – Prasad May 04 '15 at 13:27
  • @ all Could you Please Give me an Idea How to Filter By Mexican ? – Prasad May 04 '15 at 13:28

1 Answers1

3

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

New Dev
  • 48,427
  • 12
  • 87
  • 129