angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

filtering with object fails in 1.1.4 when comparing to boolean value

Open kwehrle opened this issue 13 years ago • 7 comments

I think there's a bug in version 1.1.4; see this Plunkr: http://plnkr.co/eGw6DVq7C3rVqsa3IE2o

Setting a filter like 'item in items | filter: { booleanfield: true} works as expected. Setting the field to false shows all items, regardless of the value of booleanfield of the objects.

I also tried with angular 1.0.x Version and there everything is ok.

kwehrle avatar May 21 '13 20:05 kwehrle

I've encountered this problem too. It seems like in angular 1.1.3 and higher, you can "activate" and "deactivate" the filter. So if you have a boolean field to filter and you activate the filter, it shows only the truethly fields.

It's kind of "only show active fields" filters.

Regards

buehler avatar May 22 '13 08:05 buehler

That means you have a solution for the problem? Then I would be delighted, if you could state a code sample. I didn't found anything about the activation or deactivation you described. Is it just a guess?

kwehrle avatar May 22 '13 08:05 kwehrle

I have a solution yes (more or less though). The problem is, it's not described in the documentation. But they've changed the filter "attribute" and now the default parameter is an active-flag. Meaning: if you filter by boolean values and you have filter:{myBooleanField:true} it returns all the fields where myBooleanField is true. If you have filter:{myBooleanField:false} it deactivates the filter on myBooleanField which means you get everything. I made you a jsFiddle with three different filters and their uses:

http://jsfiddle.net/buehler/HCjrQ/

Example 1: Basic activation of an angularJS filter Example 2: Only show the real value (if checkbox says true or false I mean true or false) Example 3: Deactivatable textfilter

I'm only guessing that there will be more options in the future ;-)

Regards

edit: you have to write your own filters. Another option instead of writing them with the filter() constructor is to write the function directly in the scope ($scope.onlyBooleanValueFilter = function(input, value){/.../}) and use them in the controller.

buehler avatar May 22 '13 11:05 buehler

@buehler: Thank you again for the information. This was my first thought; I already build a new (correct) filter which is working (probably there is a shorter solution, but this one works for me):

filter('FILTER', function () {
        return function (items, objectToCompare) {
            var attr, match, i, arr = [];
            for (i in items) {
                match = true;
                for (attr in objectToCompare)
                    if (items[i][attr] !== objectToCompare[attr]) {
                        match = false;
                        break;  // don't test following attributes if one comparison fails
                    }
                if (match)
                    arr.push(items[i]);
            }
            return arr;
        }
    })

I'm quite sure that this is a bug, cause the result is not what anyone would expect. Furthermore everything was ok in Version 1.0.x and even 1.1.0 to 1.1.2!

But there's missing an official statement from the Angular Team :(

kwehrle avatar May 27 '13 10:05 kwehrle

We have also run into this issue and have found that putting quotes around the false value seems to give us the correct output. For example {booleanfield: 'false'}. Is this an appropriate solution or should we be creating a custom filter function?

mc00lidge avatar Sep 04 '13 20:09 mc00lidge

it's appropriate for the cause :) if they change the behavior, you might have to change your code again. if you do a custom filtermethod, you don't have to change that again.

On Wed, Sep 4, 2013 at 10:16 PM, mc00lidge [email protected] wrote:

We have also run into this issue and have found that putting quotes around the false value seems to give us the correct output. For example {booleanfield: 'false'}. Is this an appropriate solution or should we be creating a custom filter function?

— Reply to this email directly or view it on GitHubhttps://github.com/angular/angular.js/issues/2733#issuecomment-23820408 .

buehler avatar Sep 05 '13 06:09 buehler

It seems to work correctly in 1.2.13 and the current master: http://plnkr.co/edit/UEbuCwgOjl6CCd5MCQLS?p=preview

pkozlowski-opensource avatar Feb 25 '14 19:02 pkozlowski-opensource