0

Angular JS Select for filter

I have a create a search module which is able to search with input type text.. now i want to add select option in form to search filter it

Here is the Plunker : http://embed.plnkr.co/yPJADtUDcgU720dx8ChJ/preview

1
  • Your select box ng option data is wrong try putting {{search.category}} in your html and see that output. Commented Mar 23, 2015 at 6:19

2 Answers 2

1

You can extract the categories from the json, then removing the duplication:

//Filter all the categories, return an array of duplicated categories
var allCategories = json.modules.map(function(item) {
  return item.category;
});

//Remove the duplication from the first array
var filteredCategories = [];
allCategories.forEach(function(item) {
  if (filteredCategories.indexOf(item) < 0) {
    filteredCategories.push(item);
  }
});

//Assign the filtered array to scope
$scope.categories = filteredCategories;

Change the html too:

<select ng-model="search.category" ng-options="category for category in categories"></select>

Working plunker: http://plnkr.co/edit/Z6IUMTzePO7UoWoGuUfP?p=preview

Sign up to request clarification or add additional context in comments.

2 Comments

How to populate the same category list items to Checkbox options.. so i can select more than one at a time
The checkbox is a little complicated, I don't have a clean solution. You can try asking another question to get everyone's answers.
0

Just use ng-model="search" like this:

<select ng-model="search" ng-options="module.category for module in ocw.modules"></select>

Link demo: http://plnkr.co/edit/U0DudD80VIf0ExKh5ixO?p=preview

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.