How to dynamically disable a filter inside ng-repeat

不打扰是莪最后的温柔 提交于 2019-12-05 02:10:31

问题


It is possible to remove filter using a checkbox?

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

Original code (filters enabled)

<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 disabled with the checkboxes, countryfilter and winetypefilter ) Would result:

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

回答1:


of course you can enable disable your filter dynamically... there can be many ways to do it simplest solution that comes to my mind is just sending third parameters as a boolean to check if filter is enable or not...

here is filter sample...

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
    }
  };
});

and here is PLUNKER...



来源:https://stackoverflow.com/questions/24110189/how-to-dynamically-disable-a-filter-inside-ng-repeat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!