How to dynamically disable a filter inside ng-repeat

Can I remove the filter using the checkbox?

If the checkboxes are checked, the filters inside ng-repeat will be disabled. For example, if countryfilter and winetypefilter are checked , related filters will be disabled.

Source code (filters included)

<li ng-repeat="wine in wines | winetypefilter:winetypes| countryfilter:countrytypes | stylefilter:styletypes">
                {{wine.name}} is a {{wine.type}} with {{wine.style}} style from {{wine.country}}
</li>

(filters are disabled using the flags, countryfilter and winetypefilter ). Result:

<li ng-repeat="wine in wines | stylefilter:styletypes">
            {{wine.name}} is a {{wine.type}} with {{wine.style}} style from {{wine.country}}
</li>
+4
source share
1 answer

... , , , , ...

...

app.filter('winetypefilter', function () {
  return function(input, filter, isEnable) {
    // if isEnable then filter out wines
    if (isEnable) {
      var result = [];
      angular.forEach(input, function (wine) {
          angular.forEach(filter, function (isfiltered, type) {
              if (isfiltered && type === wine.type) {
                  result.push(wine);
              }
          });
      });
      return result;
    } 
    // otherwise just do not any filter just send input without changes
    else{
      return input
    }
  };
});

PLUNKER...

+5

Source: https://habr.com/ru/post/1543780/


All Articles