2014-06-08 26 views
4

可以使用複選框刪除過濾器嗎?如何動態禁用ng-repeat內的過濾器

如果選中複選框,則禁用ng-repeat內的過濾器。例如,如果複選框countryfilterwinetypefilter被選中,相關的過濾器將被禁用。

原代碼(啓用過濾器)

<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> 

(過濾器與複選框禁用,countryfilterwinetypefilter) 會導致:

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

我認爲你將不得不編寫自己的自定義過濾器 – nilsK

回答

5
當然,你可以啓用禁用的

您動態過濾...有很多方法可以做到這一點最簡單的解決方案,我想到的只是發送第三個參數作爲布爾值che CK如果過濾器是啓用或不...

這裏是過濾器樣品...

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 ...

+0

感謝您的幫助,它的工作正常:) –

+0

感謝您的這一點,我的情況有點複雜,我有不同的過濾器依賴於另一個,但這個答案幫助我找到了實現我所需要的方式...... –