Implementing Dynamic Multi-value OR Filtering with Custom Filters in AngularJS

Nov 23, 2025 · Programming · 9 views · 7.8

Keywords: AngularJS | Custom Filters | Multi-value Filtering | OR Operation | Dynamic Filtering

Abstract: This article provides an in-depth exploration of implementing multi-value OR filtering in AngularJS, focusing on the creation of custom filters. Through detailed analysis of filtering logic, dynamic parameter handling, and practical application scenarios, it offers complete code implementations and best practices. The article also compares the advantages and disadvantages of different implementation approaches to help developers choose the most suitable solution for their specific needs.

Introduction

In AngularJS application development, data filtering is a common requirement. When needing to filter based on multiple values with OR logic, the standard filter functionality may not meet complex needs. This article provides a detailed explanation of how to implement dynamic multi-value OR filtering by creating custom filters.

Problem Analysis

Assume we have a collection of movie data, where each movie object contains title and genre properties. Users want to dynamically filter and display movies that match selected criteria by checking multiple genre checkboxes. For example, when users select both "Action" and "Comedy" genres, all movies belonging to either of these genres should be displayed.

Custom Filter Implementation

Creating a custom filter is the optimal solution for this problem. Below is a complete implementation example:

angular.module('movieApp', []).filter('genreFilter', function() {
  return function(movies, selectedGenres) {
    var filteredMovies = [];
    
    // Iterate through all movies
    angular.forEach(movies, function(movie) {
      // Check if movie genre is in selected genres list
      if (selectedGenres && selectedGenres[movie.genre]) {
        filteredMovies.push(movie);
      }
    });
    
    return filteredMovies;
  };
});

Controller Configuration

In the controller, we need to initialize movie data and genre filter states:

app.controller('MovieController', function($scope) {
  $scope.movies = [
    {title: 'The Terminator', genre: 'Action'},
    {title: 'Kung Fu Panda', genre: 'Comedy'},
    {title: 'Inception', genre: 'Sci-Fi'},
    {title: 'Mad Max', genre: 'Action'},
    {title: 'A Chinese Odyssey', genre: 'Comedy'}
  ];
  
  $scope.genreFilters = {
    'Action': false,
    'Comedy': false,
    'Sci-Fi': false
  };
});

Template Implementation

In the HTML template, we use the ng-repeat directive with our custom filter:

<div ng-controller="MovieController">
  <h3>Select Movie Genres:</h3>
  <label>
    <input type="checkbox" ng-model="genreFilters.Action" /> Action
  </label>
  <label>
    <input type="checkbox" ng-model="genreFilters.Comedy" /> Comedy
  </label>
  <label>
    <input type="checkbox" ng-model="genreFilters.Sci-Fi" /> Sci-Fi
  </label>
  
  <h3>Filtered Results:</h3>
  <ul>
    <li ng-repeat="movie in movies | genreFilter:genreFilters">
      {{movie.title}} - {{movie.genre}}
    </li>
  </ul>
</div>

Filtering Logic Detailed Explanation

The core logic of the custom filter involves iterating through the movie array and checking whether each movie's genre property exists in the user's selected genres list. When the value corresponding to a genre in the genreFilters object is true, that movie is included in the filtered results.

Advantages of this implementation approach include:

Alternative Approaches Comparison

Besides custom filters, other implementation approaches exist:

Controller Function Filtering

A filter function can be defined in the controller:

$scope.filterMovies = function(movie) {
  var selected = [];
  angular.forEach($scope.genreFilters, function(value, key) {
    if (value) selected.push(key);
  });
  return selected.indexOf(movie.genre) !== -1;
};

Used in template as: <li ng-repeat="movie in movies | filter:filterMovies">

Comparative Analysis

The custom filter approach is better suited for complex filtering logic and reusability requirements, while the controller function approach is more straightforward for simple scenarios. The advantage of custom filters lies in their modularity and testability.

Performance Optimization Recommendations

For large datasets, consider the following optimization measures:

Conclusion

By creating custom filters, we have successfully implemented dynamic multi-value OR filtering functionality in AngularJS. This approach not only solves the original problem but also provides excellent extensibility and maintainability. Developers can choose the most suitable implementation based on specific requirements, balancing functional complexity and performance considerations.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.