# Angular Grid Filter Menu

The [Angular Grid](https://www.telerik.com/kendo-angular-ui-develop/components/grid) enables you to embed a drop-down filter menu in each column header. To add a filter menu to the Grid, set [`filterable`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent#filterable) to `menu`.

The following example demonstrates the default filter menu of the Grid.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/grid/filtering/menu/basic/source.txt)

## Built-in Filter-Menu Components

If you want to change the default appearance of any default filter menu UI, the [`FilterMenuTemplateDirective`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/filtermenutemplatedirective) comes in handy.

The Grid package provides default filter menu UI components for the most common data types—`string`, `number`, `Date`, and `boolean`. These components are useful when you apply specific configurations to the default filter menu UI, modify the filter operators, and so on.

The following ready-for-use components are available for the `FilterMenuTemplateDirective`:

* [kendo-grid-string-filter-menu](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/stringfiltermenucomponent)
* [kendo-grid-numeric-filter-menu](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/numericfiltermenucomponent)
* [kendo-grid-boolean-filter-menu](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/booleanfiltermenucomponent)
* [kendo-grid-date-filter-menu](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/datefiltermenucomponent)

All built-in filter menu components require you to bind the `column`, `filter`, and `filterService` input options to the respective template context properties.

```html
<kendo-grid-column field="ProductName" title="Product Name">
    <ng-template kendoGridFilterMenuTemplate let-filter let-column="column" let-filterService="filterService">
      <kendo-grid-string-filter-menu [column]="column" [filter]="filter" [filterService]="filterService">
      </kendo-grid-string-filter-menu>
    </ng-template>
</kendo-grid-column>
```

The built-in filter menu components enable you to use the available configuration options and customize the filter settings of the Grid at the same time.

### Default Filter Operator

The default operators for the built-in filter menu components are:

| Component                        | Default Operator |
| -------------------------------- | ---------------- |
| `kendo-grid-string-filter-menu`  | `"contains"`     |
| `kendo-grid-numeric-filter-menu` | `"eq"`           |
| `kendo-grid-boolean-filter-menu` | `"eq"`           |
| `kendo-grid-date-filter-menu`    | `"gte"`          |

> If `operator` is set to `""`, the Grid will use the first operator from the defined list of operators as its default operator.

You can override the default filter operator by setting the `operator` property of the built-in filter menu components.

```html
<kendo-grid-column field="customerName" title="Customer Name">
    <ng-template kendoGridFilterMenuTemplate let-filter let-column="column" let-filterService="filterService">
        <kendo-grid-string-filter-menu
            [column]="column"
            [filter]="filter"
            [filterService]="filterService"
            operator="startswith">
                <kendo-filter-startswith-operator></kendo-filter-startswith-operator>
                <kendo-filter-eq-operator></kendo-filter-eq-operator>
        </kendo-grid-string-filter-menu>
    </ng-template>
</kendo-grid-column>
```

The following example demonstrates how to configure a string filter with a default `startswith` operator.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/grid/filtering/menu/default-operator/source.txt)

### Order of Filter Operators

You can manually fine-tune the order and number of available operators by declaring the desired operator components in the template.

```html
<kendo-grid-column field="customerName" title="Customer Name">
    <ng-template kendoGridFilterMenuTemplate let-filter let-column="column" let-filterService="filterService">
        <kendo-grid-string-filter-menu
            [column]="column"
            [filter]="filter"
            [filterService]="filterService">
                <kendo-filter-contains-operator></kendo-filter-contains-operator>
                <kendo-filter-eq-operator></kendo-filter-eq-operator>
        </kendo-grid-string-filter-menu>
    </ng-template>
</kendo-grid-column>
```

The following example demonstrates how to set the order of operators of the **Customer Name** column.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/grid/filtering/menu/operators-order/source.txt)

## Multi-Checkbox Filtering

The Grid provides a built-in Excel-like multi-checkbox filter menu. For more information, refer to the [Multi-Checkbox Filter](https://www.telerik.com/kendo-angular-ui-develop/components/grid/filtering/multi-checkbox-filter) article.

The following example demonstrates the built-in multi-checkbox filter menu.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/grid/filtering/multi-checkbox-overview/source.txt)

If you need to implement a custom multi-checkbox filter with heavily customized behavior or additional features beyond the built-in functionality, refer to the [Creating a Custom Multi-Checkbox Filter Menu Component](https://www.telerik.com/kendo-angular-ui-develop/components/knowledge-base/grid-custom-multi-checkbox-filter-menu) knowledge base article.

## Custom Filters

The filter menu template also allows you to add any custom content—HTML elements and/or Angular components. This feature is especially useful for scenarios in which the configuration options of the built-in filter menu components are not sufficient to achieve the desired result.

The following steps demonstrate how to implement a custom multi-select filter.

1. Add a template tag with the `kendoGridFilterMenuTemplate` directive for the column. Inside the template, place the [MultiSelect component](https://www.telerik.com/kendo-angular-ui-develop/components/dropdowns/multiselect) and bind `(valueChange)` to a handler that will apply the filter.

   ```html
   <kendo-grid-column field="status.current" title="Status" [width]="180">
       <ng-template kendoGridFilterMenuTemplate
           let-column="column"
           let-filter="filter"
           let-filterService="filterService"
           >
           <kendo-multiselect
               style="width:220px"
               [data]="statuses"
               [valuePrimitive]="true"
               [value]="filter | filterValues"
               (valueChange)="statusChange($event, filterService)"
               >
           </kendo-multiselect>
       </ng-template>
   </kendo-grid-column>
   ```

2. Create a `FilterValuesPipe` that extracts the currently active filter values from the `CompositeFilterDescriptor` provided by the template context. The pipe flattens nested filter groups and maps each descriptor to its value, so the MultiSelect stays in sync with the applied filter.

   ```ts
   import { Pipe, PipeTransform } from '@angular/core';
   import { CompositeFilterDescriptor, FilterDescriptor, isCompositeFilterDescriptor } from '@progress/kendo-data-query';

   // Flatten nested filter groups so the MultiSelect reflects every selected value.
   const flatten = (filter: CompositeFilterDescriptor): FilterDescriptor[] => {
       const filters = (filter || {}).filters;
       if (!filters) { return []; }
       return filters.reduce(
           (result, current) => result.concat(isCompositeFilterDescriptor(current) ? flatten(current) : [current]),
           []
       );
   };

   @Pipe({ name: 'filterValues', standalone: true })
   export class FilterValuesPipe implements PipeTransform {
       public transform(filter: CompositeFilterDescriptor): unknown[] {
           return flatten(filter).map((descriptor) => descriptor.value);
       }
   }
   ```

3. Handle the MultiSelect `valueChange` event and set the filter descriptor through the `filterService` instance.

   ```ts
   public statusChange(values: string[], filterService: FilterService): void {
       // OR lets rows match any selected status.
       filterService.filter({
           filters: values.map(value => ({
               field: "status.current",
               operator: "eq",
               value
           })),
           logic: "or"
       });
   }
   ```

The following example demonstrates the full implementation of this approach.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/grid/filtering/multiselect-filter-menu/source.txt)

## Custom Filter Menu Components

You can also create custom filter components and use them within the [`FilterMenuTemplateDirective`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/filtermenutemplatedirective). This scenario is suitable when the content of the `FilterMenuTemplateDirective` must be extracted to a component that can be reused in multiple Grid columns as well as across multiple Grid instances throughout the application.

To implement a reusable filter menu component in the Grid:

1. Create a custom component that will take the `filter` of the current column and the Grid `FilterService` as inputs, and place it within the `FilterMenuTemplateDirective`.

   ```html
   <kendo-grid-column field="customerName" title="Customer Name" [width]="200">
       <ng-template kendoGridFilterMenuTemplate
           let-filter="filter"
           let-filterService="filterService">
           <my-custom-filter-menu-component
               [currentFilter]="filter"
               [filterService]="filterService">
           </my-custom-filter-menu-component>
       </ng-template>
   </kendo-grid-column>
   ```

2. Use the passed `filter` value in the custom component to sync the filtering component value with the currently applied filter for the column.

3. Within the custom filter-menu component, on a user-defined event (typically the component's value change event), update the Grid filters by passing a [`CompositeFilterDescriptor`](https://www.telerik.com/kendo-angular-ui-develop/components/data-query/api/compositefilterdescriptor) to the [`filter()`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/filterservice#filter) method of the `FilterService` instance.

### Custom DropDownList Filter Menu Component

1. Create a custom component that will take the `filter` of the current column and the `FilterService` as inputs and place it within the `FilterMenuTemplateDirective`.

   ```html
   <kendo-grid-column field="customerName" title="Customer Name" [width]="200">
       <ng-template kendoGridFilterMenuTemplate
           let-column="column"
           let-filter="filter"
           let-filterService="filterService"
           >
           <dropdownlist-filter
               [isPrimitive]="true"
               [field]="column.field"
               [currentFilter]="filter"
               [filterService]="filterService"
               [textField]="column.field"
               [valueField]="column.field"
               [data]="distinctPrimitive(column.field)">
           </dropdownlist-filter>
       </ng-template>
   </kendo-grid-column>
   ```

2. Use the passed `filter` value in the custom component to sync the filtering component value with the currently applied filter for the column.

   ```ts
   public ngAfterViewInit(): void {
       this.currentData = this.data;
       const currentColumnFilter: FilterDescriptor =
           this.currentFilter.filters.find(
               (filter: FilterDescriptor) => filter.field === this.field
           );
       if (currentColumnFilter) {
           // Restore the selected value from the active filter for this field.
           this.value = currentColumnFilter.value;
       }
   }
   ```

3. Within the custom filter-menu component, on a user-defined event (typically the component's value change event), update the Grid filters by passing a [`CompositeFilterDescriptor`](https://www.telerik.com/kendo-angular-ui-develop/components/data-query/api/compositefilterdescriptor) to the [`filter()`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/filterservice#filter) method of the `FilterService` instance.

   ```ts
   public onValueChange(value: number): void {
       const myFilter: CompositeFilterDescriptor = {
           filters: [{ field: this.field, operator: 'eq', value: value }],
           logic: 'and',
       };
       this.filterService.filter(myFilter);
   }
   ```

The following example demonstrates how to create a reusable filter menu component that contains a DropDownList.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/grid/filtering/custom-dropdownlist-filter/source.txt)

## Filter Menu with Popup

By default, the filter menu closes when the user clicks outside its container. This behavior can cause issues when using components like the DatePicker, which open their own popups. For example, clicking the DatePicker's calendar popup will close the Grid filter menu.

To prevent the filter menu from closing, you can override the default behavior by following these steps:

1. Inject the [`SinglePopupService`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/singlepopupservice) in the custom filter component to manage the popup behavior.
2. Subscribe to the [`onClose`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/popupcloseevent) event to intercept the filter menu's closing.
3. Check if the active element is part of the filter menu or a related popup (for example, DatePicker). If so, prevent the default closing behavior.

```ts
// custom-filter-menu.component.ts
constructor(private element: ElementRef, private popupService: SinglePopupService) {
  popupService.onClose.subscribe(
    (e: PopupCloseEvent) => {
      if (/* TODO: check whether focus is inside the menu or popup */) {
        e.preventDefault();
      }
    }
  );
}
```

The following example demonstrates how to keep the filter menu open while interacting with a DatePicker popup.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/grid/filtering/date-range-menu/source.txt)

## Suggested Links

* [Common Issues with the Grid Filter Menu](https://www.telerik.com/kendo-angular-ui-develop/components/knowledge-base/grid-filter-menu-common-issues)
* [Multi-Checkbox Menu Filtering](https://www.telerik.com/kendo-angular-ui-develop/components/grid/filtering/multi-checkbox-filter)
* [FilterDescriptors](https://www.telerik.com/kendo-angular-ui-develop/components/data-query/api/filterdescriptor)
* [dataStateChange](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent#datastatechange)
* [Built-In Row Filter Templates](https://www.telerik.com/kendo-angular-ui-develop/components/grid/filtering/filter-row)
* [Reusable Custom Filter Components](https://www.telerik.com/kendo-angular-ui-develop/components/grid/filtering/filter-row#custom-filter-row-components)
* [API Reference of the GridComponent](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent)
* [Data Binding Basics](https://www.telerik.com/kendo-angular-ui-develop/components/grid/data-binding/basics)
* [Automatic Data Processing](https://www.telerik.com/kendo-angular-ui-develop/components/grid/data-binding/local-data#automatic-data-processing)
