Programmatically Managing Filters Inside the Angular Filter Component
Environment
Product | Progress® Kendo UI for Angular Filter |
Description
In some scenarios, I need to programmatically control the filters applied to the Kendo UI for Angular Filter. This includes adding new filters, removing specific filters, and clearing all filters, for instance, when implementing a query box over a Grid component.
This Knowledge Base article also answers the following questions:
- How can I add a filter to the Kendo UI for Angular Filter programmatically?
- How do I remove a filter from the Kendo UI for Angular Filter via code?
- What is the method to clear all filters in the Kendo UI for Angular Filter component?
Solution
To programmatically manage filters inside the Kendo UI for Angular Filter, consider the following approaches:
Using the Built-In Property
-
Bind the
value
property of the Filter component to a variable in your component.html<kendo-filter #filter [filters]="filters" [value]="filterValue"></kendo-filter>
-
Define methods in your component to add, remove, and clear filters.
-
Adding a filter:
tspublic addFilter() { const filter = { field: 'ProductName', operator: 'eq', value: 'Chai', }; this.filterValue.filters.push(filter); ... }
-
Removing the first filter:
tspublic removeFirstFilter() { if (this.filterValue.filters.length > 0) { this.filterValue.filters.splice(0, 1); ... } }
-
Clearing all filters:
tspublic clearAll() { this.filterValue.filters = []; ... }
-
-
Create a designated method that applies the filters to the desired component.
The following example demonstrates a Filter component synced with a Kendo UI for Angular Grid.
Modifying the Filters Internally
-
Obtain a reference to the Filter component.
ts@ViewChild('filter') public filterComponent: FilterComponent;
-
Use the internal
currentFilter
andfilters
property to add, remove, and clear filters inside the component.-
Adding a filter:
tspublic addFilter() { const filter = { field: 'ProductName', operator: 'eq', value: 'Chai', }; this.filterComponent.currentFilter.filters.push(filter); ... }
-
Removing the first filter:
tspublic removeFirstFilter() { if (this.filterComponent.currentFilter.filters.length > 0) { this.filterComponent.currentFilter.filters.splice(0, 1); } }
-
Clearing all filters:
tspublic clearAll() { this.filterComponent.currentFilter.filters = []; ... }
-
-
Define a method that applies the filters to the desired component.
The following example demonstrates how to internally modify the filters inside the Filter component.