# Programmatically Managing Filters Inside the Angular Filter Component

## Environment

<table>
    <tbody>
	    <tr>
	    	<td>Product</td>
	    	<td>Progress® Kendo UI for Angular Filter</td>
	    </tr>
    </tbody>
</table>

## 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](https://www.telerik.com/kendo-angular-ui-develop/components/filter), consider the following approaches:

* [Using the Built-In Property](https://www.telerik.com/kendo-angular-ui-develop/components/knowledge-base/filter-programmatically-manage-filters#using-the-built-in-property)
* [Modifying the Filters Internally](https://www.telerik.com/kendo-angular-ui-develop/components/knowledge-base/filter-programmatically-manage-filters#modifying-the-filters-internally)

### Using the Built-In Property

1. Bind the [`value`](https://www.telerik.com/kendo-angular-ui-develop/components/filter/api/filtercomponent#value) property of the Filter component to a variable in your component.

   ```html
   <kendo-filter #filter [filters]="filters" [value]="filterValue"></kendo-filter>
   ```

2. Define methods in your component to add, remove, and clear filters.

   * Adding a filter:

     ```ts
     public addFilter() {
         const filter = {
           field: 'ProductName',
           operator: 'eq',
           value: 'Chai',
         };

         this.filterValue.filters.push(filter);
         ...
     }
     ```

   * Removing the first filter:

     ```ts
     public removeFirstFilter() {
         if (this.filterValue.filters.length > 0) {
           this.filterValue.filters.splice(0, 1);
           ...
         }
     }
     ```

   * Clearing all filters:

     ```ts
     public clearAll() {
         this.filterValue.filters = [];
         ...
     }
     ```

3. 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.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/knowledge-base/filter-programmatic-control/using-built-in-property/source.txt)

### Modifying the Filters Internally

1. Obtain a reference to the Filter component.

   ```ts
   @ViewChild('filter') public filterComponent: FilterComponent;
   ```

2. Use the internal `currentFilter` and `filters` property to add, remove, and clear filters inside the component.

   * Adding a filter:

     ```ts
     public addFilter() {
         const filter = {
           field: 'ProductName',
           operator: 'eq',
           value: 'Chai',
         };

         this.filterComponent.currentFilter.filters.push(filter);
         ...
     }
     ```

   * Removing the first filter:

     ```ts
     public removeFirstFilter() {
         if (this.filterComponent.currentFilter.filters.length > 0) {
             this.filterComponent.currentFilter.filters.splice(0, 1);

         }
     }
     ```

   * Clearing all filters:

     ```ts
     public clearAll() {
         this.filterComponent.currentFilter.filters = [];
         ...
     }
     ```

3. 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.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/knowledge-base/filter-programmatic-control/internal-approach/source.txt)

## See Also

* [Filter Overview](https://www.telerik.com/kendo-angular-ui-develop/components/filter)
* [Grid Overview](https://www.telerik.com/kendo-angular-ui-develop/components/grid)
* [API Reference of the Filter Component](https://www.telerik.com/kendo-angular-ui-develop/components/filter/api/filtercomponent)
* [API Reference of the Grid Component](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent)
* [External Filtering in Kendo UI for Angular Grid](https://www.telerik.com/kendo-angular-ui-develop/components/grid/filtering/external-filtering)
