Keywords: AngularJS | Custom Filters | ng-repeat
Abstract: This article explores the integration of custom filters with the ng-repeat directive in AngularJS, using a car rental listing application as a case study to detail how to create and use functional filters for complex data filtering logic. It begins with the basics of ng-repeat and built-in filters, then focuses on two implementation methods for custom filters: controller functions and dedicated filter services, illustrated through code examples that demonstrate chaining multiple filters for flexible data processing. Finally, it discusses performance optimization and best practices, providing comprehensive technical guidance for developers.
In AngularJS applications, the ng-repeat directive is a core tool for rendering dynamic list data, while filters offer powerful data transformation and filtering capabilities. This article uses a car rental listing case study to delve into how to seamlessly integrate custom filters with ng-repeat for complex data filtering logic.
Basic Usage of ng-repeat and Built-in Filters
In AngularJS, the ng-repeat directive iterates over arrays or objects, generating DOM elements for each item. Combined with the built-in filter filter, it enables simple data filtering. For example, in a car rental app, results can be rendered as follows:
<article data-ng-repeat="result in results | filter:search" class="result">
<header><h3>{{result.carType.name}}, {{result.carDetails.doors}} door, £{{result.price.value}} - {{ result.company.name }}</h3></header>
<ul class="result-features">
<li>{{result.carDetails.hireDuration}} day hire</li>
<li data-ng-show="result.carDetails.airCon">Air conditioning</li>
<li data-ng-show="result.carDetails.unlimitedMileage">Unlimited Mileage</li>
<li data-ng-show="result.carDetails.theftProtection">Theft Protection</li>
</ul>
</article>
Here, filter:search uses a search object as criteria, with AngularJS automatically matching item properties. For instance, selecting door count via a dropdown:
<select data-ng-model="search.carDetails">
<option value="">All</option>
<option value="2">2</option>
<option value="4">4</option>
<option value="9">9</option>
</select>
This built-in filter works for simple matches, but custom filters are needed for complex logic, such as dynamic filtering based on multiple conditions.
Creating Custom Filters: Controller Function Approach
A common way to create custom filters is by defining a function in the controller that takes a data item as an argument and returns a boolean indicating inclusion. For example, filtering cars with more than 2 doors:
$scope.filterFn = function(car) {
if (car.carDetails.doors > 2) {
return true;
}
return false;
};
In HTML, this function can be chained with built-in filters:
<article data-ng-repeat="result in results | filter:search | filter:filterFn" class="result">
This approach allows flexible combination of multiple criteria, e.g., applying both search-based and custom logic. Chained filters execute sequentially, ensuring data is refined step-by-step.
Creating Custom Filters: Dedicated Filter Service Approach
Another method uses AngularJS's filter service to create reusable filters. For example, a filter based on car type:
app.filter('cartypefilter', function() {
return function(items, search) {
if (!search) {
return items;
}
var carType = search.carType;
if (!carType || '' === carType) {
return items;
}
return items.filter(function(element) {
return element.carType.name === search.carType;
});
};
});
Usage in HTML:
<article data-ng-repeat="result in results | cartypefilter:search" class="result">
This method is better for complex or reusable filtering logic but may increase code complexity.
Performance Optimization and Best Practices
When using custom filters, consider performance impacts. AngularJS executes filter functions in each digest cycle, so avoid expensive operations. Recommendations:
- Keep filter functions concise, focusing on data filtering logic.
- For large datasets, use pagination or virtual scrolling to reduce DOM operations.
- In chained filters, place the most restrictive filter first for efficiency.
With proper design, custom filters can significantly enhance data processing in AngularJS applications.