Search code examples
angularjsangularjs-filter

AngularJS mysql filter multiple element


Hi this is my hotel project. But I'm having a problem with a filter. I want to filter the data in the amenities column.

This is: my fiddle

It works if you select a single checkbox but it does not work if you select multiple checkboxes.

I suspect the problem arises from indexof instead of what I can use. What method should I follow?

How to change this line: indexOf(x);

This is my bad code:

//PROBLEM FILTER HERE 

$scope.am_en = function()
{
    //get input value
    x = $(".hosting_amenities input:checkbox:checked").map(function(){return $(this).val();}).get();
    //filter
    $scope.ot_Filter = function (location) {
        return location.amenities.indexOf(x) !== -1;
    };
}

Solution

  • The problem is indeed caused by the function $scope.am_en, in the declaration of the inner function $scope.ot_Filter

    When you select multiple checkboxes, your x variable is an array of objects, so you should do a loop and can create a variable to check whether the element should be shown or not. You can do it as follows:

    $scope.ot_Filter = function (location) {
        var shouldBeShown = false;
            for (var i = 0; i < x.length; i++) {
                if (location.amenities.indexOf(x[i]) !== -1) {
                    shouldBeShown = true;
                break;
            }
        }
        return shouldBeShown;
    };
    

    I modified your jsfiddle, so that you can see this solution working properly.