AngularJS checkbox filter

前端 未结 3 587
遥遥无期
遥遥无期 2020-12-04 13:42

I would like to filter the results.

There is a list of wines, my wish is when no checkbox is checked, the entire list of wine is displayed.

  • when only 1
3条回答
  •  感动是毒
    2020-12-04 14:28

    There are several implementations possible. Here's one:

    1. Have a $scope.filter = {} object to hold the state of each filter. E.g. {red: true, white: false...}.

    2. Associate each checkbox with the corresponding property using ng-model. E.g.: input type="checkbox" ng-model="filter['red']" />.

    3. Have a function (e.g. $scope.filterByCategory(wine)) that decides if a wine should be displayed or not (based on the $scope.filter object).

    4. Use that function to filter the items based on their category. E.g.


    The filterByCategory function could be implemented like this:

    function filterByCategory(wine) {
      // Display the wine if
      var displayWine =
          // the wine's category checkbox is checked (`filter[category]` is true)
          $scope.filter[wine.category] ||   // or 
    
          // no checkbox is checked (all `filter[...]` are false)
          noFilter($scope.filter);
    
      return displayWine;
    };
    

    where noFilter() is a function that checks if there is any filter activated (and returns true if there is none):

    function noFilter(filterObj) {
      return Object.
        keys(filterObj).
        every(function (key) { return !filterObj[key]; });
    }
    

    See, also, this short demo.


    UPDATE:

    I created a modified version, which supports multiple filters (not just filtering by category).
    Basically, it dynamically detects the available properties (based on the first wine element), adds controls (groups of check-boxes) for applying filters based on each property and features a custom filter function that:

    1. Filters each wine item, based on every property.
    2. If a property has no filter applied (i.e. no check-box checked), it is ignored.
    3. If a property has check-boxes checked, it is used for filtering out wine items (see above).
    4. There is code for applying multiple filters using AND (i.e. all properties must match) or OR (at least one property must match).

    See, also, this updated demo.

提交回复
热议问题