0

span class="list-group-item" ng-click="filterByCtg = {ctgid:'12'}">category

div ng-repeat="product in products | filter:search | filter:filterByCtg |orderBy:orderByFilter:reverse">

I want to give filterByCtg an array of id's that are sub categories and sub sub categories. how can i achieve this?

the filter doesnt work when im giving it an array.

sorry for bad engish.

4
  • can you post a fiddle? Commented Jul 17, 2014 at 10:16
  • sorry, its on a framework and i dont think i can or know how to jsfiddle with all the php...sorry for being a noob, ill try to explain better: i have a list<ul> with <li> and so on, that is my "menu" for a product catalog, the li's are passing a category id to the filter and the filter works. but i want to pass the filter multiple id's, so it will show subcategories. Commented Jul 17, 2014 at 10:28
  • according to the docs the filter expression can be a string, object, or a function. It doesn't say anything about passing a an array. docs.angularjs.org/api/ng/filter/filter Commented Jul 17, 2014 at 10:30
  • can i somehow achieve this with custom filter? Commented Jul 17, 2014 at 10:31

1 Answer 1

2

A custom filter is exactly the right answer. A stub for it would look something like this:

angular
  .module('myApp', [])
  .filter('myFilter', function () {
    return function(input, query) {
      var result = [];

      // Now do whatever you want to create the 'result' array
      // input will contain the source to the ng-repeat, categories
      // query will contain whatever you gave it as a parameter - see below
      // You should return an array of items. Each item will become 'entry' in the ng-repeat

      return result;
   };
 });

and you would use it like this:

<div class="whatever" ng-repeat="entry in categories | myFilter:someParameter track by $index"></div>

Note that you can set $scope.someParameter = 'A'; and that will be passed into your filter as query.

Also, note that your filter is just like any other Angular module now. It can do a lot of clever things, like asking for a service to be injected that it can get things like subcategory data from. But be aware that this is a performance sensitive section of code. Your filter will get run a lot more than you expect because many things will trigger Angular to verify that this list has not changed. Therefore you should make this as efficient as possible.

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

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.