Andy Nguyen
Andy Nguyen

Reputation: 461

AngularJS checkbox to toggle my filter on/off

Hi I am trying to get a filter to return instances that have a certain quantity but can't seem to get it to work. What I have in my HTML:

I would like my filter to apply when I hit this checkbox:

<input type="checkbox" value="true" ng-model='filterSales' >

Heres what I have:

<div class='col-md-12' ng-repeat='sku in value | QuantityFilter'>

I was just able to learn how to create a custom filter but now I am having trouble figuring out how to toggle it.

Upvotes: 1

Views: 157

Answers (1)

Guest
Guest

Reputation: 88

If you just want to know how you can see whether the checkbox is set or not, you can add a parameter to your filter function as shown in the example at the bottom of the documentation: https://docs.angularjs.org/api/ng/filter/filter

You would need to add :filterSales to your ng-repeat where you apply the filter.

<div class='col-md-12' ng-repeat='sku in value | quantityFilter:filterSales'>

In js you need to add another parameter, in this example "array" contains the array which is repeated (in your case "value") and toggleVar has the checkbox value.

"use strict";

app.filter("quantityFilter", function () {

    function quantityFilter(array, toggleVar) {
        if(!toggleVar){
            return array;
        }

        let result = [];

       // TODO: Build new array based on your criteria

        return result;
    }

    return quantityFilter;
});

Upvotes: 2

Related Questions