Filtering by multiple checkboxes in AngularJS

前端 未结 2 886
被撕碎了的回忆
被撕碎了的回忆 2020-12-06 03:18

first time I\'ve posted a question on here, so apologies in advance if I breach any Stack Overflow etiquette! :-)

I\'m having a go at some AngularJS for the first ti

相关标签:
2条回答
  • 2020-12-06 03:52

    I would bind all the checkboxes to one object say:

    app.js

    $scope.cartypes = {mini: false, compact:false};
    

    index.html

    <input type="checkbox" data-ng-model="cartypes.mini"> Mini
    <input type="checkbox" data-ng-model="cartypes.compact"> Compact
    

    And then create a custom filter function which returns whether the object contains all (I assume thats what you want) of the checked options.

    app.js

    app.filter('myfilter', function() {
        return function(items, options ) {
          // loop over all the options and if true ensure the car has them
          // I cant do this for you beacause I don't know how you would store this info in the car object but it should not be difficult
          return carMatches;
        };
    });
    

    Then you can add it to your template like this:

    index.html

    <article data-ng-repeat="result in results | filter:search | myfilter:cartypes" class="result">
    
    0 讨论(0)
  • 2020-12-06 03:58

    I have implemented this solution like this:

    @myapp.filter "storeFilter", ->
      (stores, type) ->
        _.filter stores, (store) -> type[store.name]
    

    and in the view i have passed it like that:

    store in stores | storeFilter:store_type
    
    0 讨论(0)
提交回复
热议问题