1

I'm having trouble using two filters with ng-hide. Here's a fiddle that displays my problem: http://jsfiddle.net/czPf6/2/ Here's my code that conditionally selects which filter to apply based on input values in an input box and a select box:

function applySearchFilter() {
        var filterCategory = $scope.filters.cat.toLowerCase();
        //console.log(filterCategory);
        var category = $scope.friend.job.toLowerCase();
        //console.log(category);
        var filter = $scope.filters.name.toLowerCase();
        //console.log(filter);
        var name = $scope.friend.name.toLowerCase();
        console.log(name + " " + category);
        console.log("filter:" + filter + "/category:" + filterCategory);
        if($scope.filters.name == "" && $scope.filters.cat !== ""){
            var isCategory = (filterCategory == category);
            console.log(isCategory + "1st");
            $scope.isExcludedByFilter =  ! isCategory;
            console.log($scope.isExcludedByFilter);
        } else if($scope.filters.name !=="" && $scope.filters.cat == ""){

            var isSubstring = ( name.indexOf( filter ) !== -1 );
            console.log(isSubstring + "2nd");
            $scope.isExcludedByFilter = ! isSubstring;
            console.log($scope.isExcludedByFilter);
        } else if($scope.filters.name !=="" && $scope.filters.cat !==""){

            var isCategory = (filterCategory == category);
            var isSubstring = ( name.indexOf( filter ) !== -1 );
            console.log(isCategory + "3rd");
            console.log(isSubstring + "3rd");
            $scope.isExcludedByFilter = ! isSubstring && ! isCategory;
            console.log($scope.isExcludedByFilter);
        } 

The log statements print out what I would expect them to be, but the filter only works when both input fields have a value. What am I doing wrong here?

1 Answer 1

1

It's your isExcludedByFilter thing at the end that's causing it:

$scope.isExcludedByFilter = ! isSubstring || ! isCategory;

I've commented that line out.

I've changed:

} else if($scope.filters.name !=="" && $scope.filters.cat == ""){

    var isSubstring = ( name.indexOf( filter ) !== -1 );
    $scope.isExcludedByFilter = ! isSubstring && ! isCategory; // added && !isCategory

} else if($scope.filters.name !=="" && $scope.filters.cat !==""){

    var isCategory = (filterCategory == category);
    var isSubstring = ( name.indexOf( filter ) !== -1 );

    $scope.isExcludedByFilter = ! isSubstring || ! isCategory; // changed && to ||
}

i also added the following at the end of the above if statement for the "Any" filter. Which means it'll show all 'friends' in the list:

        } else if($scope.filters.name =="" && $scope.filters.cat == ""){
             $scope.isExcludedByFilter = false;
        }

fiddle: http://jsfiddle.net/czPf6/10/

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

1 Comment

Thank you for catching that! The sample app worked with that conditional flow but for some reason my real app would not. I was able to fix this by placing the check for no input values at the beginning of the nested if statements.

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.