0

I would like to filter a ng-repeat list of items with multiple catgories checkboxes.

i read this Filtering by multiple checkboxes in AngularJS and watched the videos by Egghead, but i have an error on a simple for loop and i don't understand:

ReferenceError: i is not defined 

here is a plunker with the code : http://plnkr.co/edit/p538ALfs00JTFQ6mKT9j

thank you for your help

1 Answer 1

0

If you're going to use strict mode ('use strict';), you need to declare your variables. Your checkboxFilter uses many variables that are never defined (i included). You can get past the your initial issues by changing script.js:22-23 to:

      var i,j,k,filter_cat,filter_value,filter_name,matchingItems = [];

You have three other problems after that...

  1. Line 37, This code is looking for a property named filter_cat: items[i].filter_cat, when what you wanted was to look for a property name with the value of filter_cat, so this is what you want: items[i].[filter_cat].
  2. Your json data has lowercase field names and your code is searching on uppercase (Type vs type)
  3. Your json data has lowercase values and your code is searching on uppercase (Fruit vs fruit)

Here is a partial edit that gets you on the right track. You'll still need to modify your json or compare lowercase (I partially modified your json): http://plnkr.co/edit/d7p4QthXJg4ao34ATWla?p=preview

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

3 Comments

thank you ! i copied 'use strict'; from angular-seed and now i'm understanding what it is doing. You solved my problem. Now, my code still has an error : TypeError: Cannot read property 'length' of undefined. but when I console.log(prefs), the array is returned. any idea ?
Yes, you need to write more defensive code. Basically wrap if (prefs) {} around your for (j = 0... loop. It's because of a race condition... you're using ngResource and referencing $scope.users in your filter before it has loaded.
ok. i corrected all the typos, and the mistakes you pointed. Thanks a lot. It's working when the page loads (plunker updated). How can i make it change each time I click a checkbox, or when i click a button ?

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.