# Angular TreeList Filter Menu

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

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

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

## Built-in Filter-Menu Components

To change the default appearance of any default filter menu UI, use the [`FilterMenuTemplateDirective`](https://www.telerik.com/kendo-angular-ui-develop/components/treelist/api/filtermenutemplatedirective).

The TreeList 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-to-use components are available for the `FilterMenuTemplateDirective`:

* [`kendo-treelist-string-filter-menu`](https://www.telerik.com/kendo-angular-ui-develop/components/treelist/api/stringfiltermenucomponent)
* [`kendo-treelist-numeric-filter-menu`](https://www.telerik.com/kendo-angular-ui-develop/components/treelist/api/numericfiltermenucomponent)
* [`kendo-treelist-boolean-filter-menu`](https://www.telerik.com/kendo-angular-ui-develop/components/treelist/api/booleanfiltermenucomponent)
* [`kendo-treelist-date-filter-menu`](https://www.telerik.com/kendo-angular-ui-develop/components/treelist/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-treelist-column field="name">
    <ng-template kendoTreeListFilterMenuTemplate
      let-filter
      let-column="column"
      let-filterService="filterService"
    >
        <kendo-treelist-string-filter-menu
          [column]="column"
          [filter]="filter"
          [filterService]="filterService">
        </kendo-treelist-string-filter-menu>
    </ng-template>
</kendo-treelist-column>
```

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

### Default Filter Operator

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

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

> If `operator` is set to `""`, the TreeList 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-treelist-column field="name" title="Name" [width]="180">
    <ng-template kendoTreeListFilterMenuTemplate
      let-filter
      let-column="column"
      let-filterService="filterService"
    >
        <kendo-treelist-string-filter-menu
          [column]="column"
          [filter]="filter"
          [filterService]="filterService"
          operator="startswith">
            <kendo-treelist-filter-startswith-operator></kendo-treelist-filter-startswith-operator>
            <kendo-treelist-filter-eq-operator></kendo-treelist-filter-eq-operator>
        </kendo-treelist-string-filter-menu>
    </ng-template>
</kendo-treelist-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/treelist/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-treelist-column field="name" title="Name" [width]="180">
    <ng-template kendoTreeListFilterMenuTemplate
      let-filter
      let-column="column"
      let-filterService="filterService"
    >
        <kendo-treelist-string-filter-menu
          [column]="column"
          [filter]="filter"
          [filterService]="filterService">
            <kendo-treelist-filter-eq-operator></kendo-treelist-filter-eq-operator>
            <kendo-treelist-filter-contains-operator></kendo-treelist-filter-contains-operator>
        </kendo-treelist-string-filter-menu>
    </ng-template>
</kendo-treelist-column>
```

The following example demonstrates how to set the order of operators.

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

## Multi-Checkbox Menu Filtering

To create a custom multi-checkbox menu component and use it in the [Filter Menu](https://www.telerik.com/kendo-angular-ui-develop/components/treelist/api/filtermenutemplatedirective) template of the TreeList:

1. Create a custom component that will take the `filter` of the current column and the `FilterService` as inputs. Use a collection of the distinct TreeList records for the respective field as data. The custom filter component can work with both primitive values and complex objects.

   ```html
   <kendo-treelist-column  field="name" title="Name">
     <ng-template kendoTreeListFilterMenuTemplate
           let-column="column"
           let-filter="filter"
           let-filterService="filterService"
           >
           <multicheck-filter
             [isPrimitive]="true"
             [field]="column.field"
             [filterService]="filterService"
             [currentFilter]="filter"
             [data]="distinctPrimitive(column.field)"></multicheck-filter>
       </ng-template>
   </kendo-treelist-column>
   ```

2. Loop through the data to create a list of items with checkboxes and text. The items will correspond to the values of the respective TreeList column. Attach the required handlers and bindings that will manage the selection and deselection of items.

   ```html
   <ul>
       <li *ngIf="showFilter">
           <kendo-textbox (valueChange)="onValueChange($event)"></kendo-textbox>
       </li>
       <li
           *ngFor="let item of currentData; let i = index;"
           (click)="onSelectionChange(isPrimitive ? item : item[valueField])"
           [ngClass]="{'k-selected': isItemSelected(item)}">
           <input
             type="checkbox"
             id="chk-{{isPrimitive ? item : item[valueField]}}"
             kendoCheckBox
             [checked]="isItemSelected(item)" />
           <kendo-label
             class="k-multiselect-checkbox"
             [for]="'chk-' + (isPrimitive ? item : item[valueField])"
             [text]="isPrimitive ? item : item[textField]">
           </kendo-label>
       </li>
   </ul>
   ```

3. Monitor the selected items and call the `filterService.filter()` method accordingly.

   ```ts
   public onSelectionChange(item) {
       if(this.value.some(x => x === item)) {
           this.value = this.value.filter(x => x !== item);
       } else {
           this.value.push(item);
       }

       this.filterService.filter({
           filters: this.value.map(value => ({
               field: this.field,
               operator: 'eq',
               value
           })),
           logic: 'or'
       });
   }
   ```

4. (Optional) Add a filtering feature for the list of options.

   ```html
   <li *ngIf="showFilter">
       <kendo-textbox (valueChange)="onValueChange($event)"></kendo-textbox>
   </li>
   ```

   ```ts
   public onInput(e) {
       this.currentData = distinct([
       ...this.currentData.filter(dataItem => this.value.some(val => val === this.valueAccessor(dataItem))),
       ...filterBy(this.data, {
           operator: 'contains',
           field: this.textField,
           value: e.target.value
       })],
       this.textField
       );
   }
   ```

The following example demonstrates the full implementation of the described approach.

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

## Custom Filters

The filter menu template also allows you to add any custom content—HTML elements and 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 `kendoTreeListFilterMenuTemplate` directive for the `title` column. Inside the template tag, place the [MultiSelect component](https://www.telerik.com/kendo-angular-ui-develop/components/dropdowns/multiselect). Notice that the `value` of the MultiSelect component is composed from the provided filter descriptors through a custom pipe.

   ```html
   <kendo-treelist-column field="title" title="Title" [width]="180">
       <ng-template kendoTreeListFilterMenuTemplate
           let-column="column"
           let-filter="filter"
           let-filterService="filterService"
           >
           <kendo-multiselect
               style="width:220px"
               [data]="titles"
               [valuePrimitive]="true"
               [value]="titleFilters(filter)"
               (valueChange)="titleChange($event, filterService)"
               >
           </kendo-multiselect>
       </ng-template>
   </kendo-treelist-column>
   ```

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

   ```ts
   public titleChange(values: unknown[], filterService: FilterService): void {
       filterService.filter({
           filters: values.map(value => ({
               field: 'title',
               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/treelist/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/treelist/api/filtermenutemplatedirective). This scenario is suitable when the content of the `FilterMenuTemplateDirective` must be extracted to a component that can be reused in multiple TreeList columns as well as across multiple TreeList instances throughout the application.

To implement a reusable filter menu component in the TreeList:

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

   ```html
   <kendo-treelist-column field="title" title="Title" [width]="180">
       <ng-template kendoTreeListFilterMenuTemplate
         let-filter="filter"
         let-filterService="filterService"
       >
           <my-custom-filter-menu-component
               [currentFilter]="filter"
               [filterService]="filterService">
           </my-custom-filter-menu-component>
       </ng-template>
   </kendo-treelist-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 TreeList 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/treelist/api/filterservice#filter) method of the `FilterService` instance.

### Custom DropDownList Filter Menu Component

You can use the Kendo UI for Angular [DropDownList](https://www.telerik.com/kendo-angular-ui-develop/components/dropdowns/dropdownlist) in the TreeList and filter the data by choosing a single predefined value from a list. To integrate the DropDownList 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-treelist-column field="title" title="Title" [width]="180">
       <ng-template kendoTreeListFilterMenuTemplate
         let-filter="filter"
         let-filterService="filterService"
       >
           <my-dropdown-filter
             [filter]="filter"
             [filterService]="filterService"
             [data]="titles"
             field="title"
             textField="text"
             valueField="value">
           </my-dropdown-filter>
       </ng-template>
   </kendo-treelist-column>
   ```

2. Set the value of the DropDownList.

   ```ts
   public ngAfterViewInit(): void {
       this.currentData = this.data;
       const currentColumnFilter: any = this.filter.filters.find(
           (filter: FilterDescriptor) => filter.field === this.field
       );
       if (currentColumnFilter) {
           this.value = currentColumnFilter.value;
       }
   }
   ```

3. Set the current column filter by calling the `filter()` function of the `filterService`.

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

The following example demonstrates how to create reusable Filter Menu component that contains a DropDownList.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/treelist/filtering/menu/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 TreeList 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/treelist/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/treelist/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
constructor(private element: ElementRef, private popupService: SinglePopupService) {
  popupService.onClose.subscribe(
    (e: PopupCloseEvent) => {
            if (/* Check whether the active element belongs to 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/treelist/filtering/date-range-menu/source.txt)

## Suggested Links

* [FilterDescriptors](https://www.telerik.com/kendo-angular-ui-develop/components/data-query/api/filterdescriptor)
* [dataStateChange](https://www.telerik.com/kendo-angular-ui-develop/components/treelist/api/treelistcomponent#datastatechange)
* [Built-In Row Filter Templates](https://www.telerik.com/kendo-angular-ui-develop/components/treelist/filtering/filter-row)
* [Built-In Menu Filter Templates](https://www.telerik.com/kendo-angular-ui-develop/components/treelist/filtering/filter-menu)
* [Reusable Custom Filter Components](https://www.telerik.com/kendo-angular-ui-develop/components/treelist/filtering/filter-row#custom-filter-row-components)
* [API Reference of the TreeListComponent](https://www.telerik.com/kendo-angular-ui-develop/components/treelist/api/treelistcomponent)
* [Data Binding Basics](https://www.telerik.com/kendo-angular-ui-develop/components/treelist/data-binding/basics)
* [Automatic Data Binding](https://www.telerik.com/kendo-angular-ui-develop/components/treelist/data-binding/local-data#automatic-data-processing)
