# Client-side Filtering

> Learn how to configure client-side filtering for your Infinite Table Angular DataGrid

Canonical page: https://infinite-table.com/angular/docs/learn/filtering/filtering-client-side

The most common way to use filtering in Infinite Table is by configuring filters for columns (this works both for client-side and server-side filtering).

Filtering happens client-side by default - whether the DataSource [`data`](https://infinite-table.com/angular/docs/reference/datasource-props/index.md#data) property is an array, a `Promise` or a function. The only exceptions are [`lazyLoad`](https://infinite-table.com/angular/docs/reference/datasource-props/index.md#lazyLoad) and [`livePagination`](https://infinite-table.com/angular/docs/reference/datasource-props/index.md#livePagination), which filter server-side by default.

When the [`data`](https://infinite-table.com/angular/docs/reference/datasource-props/index.md#data) property is a function, it's not called again when the filters change. If you also want to reload the data on every filter change, and then filter it client-side, set [shouldReloadData.filterValue: true](https://infinite-table.com/angular/docs/reference/datasource-props/index.md#shouldReloadData.filterValue) together with `[filterFunction]="defaultFilterFunction"` (with `defaultFilterFunction` exposed as a field of your component).

## Showing the Column Filters

In order to show the column filter editors in the column headers, you need to specify either the uncontrolled [`defaultFilterValue`](https://infinite-table.com/angular/docs/reference/datasource-props/index.md#defaultFilterValue) property or the controlled [`[(filterValue)]`](https://infinite-table.com/angular/docs/reference/datasource-props/index.md#filterValue) version.

This example shows remote data with local filtering - the data function is called once, and the filtering happens in the browser.

In addition, the `filterDelay` property is set to `0` for instant feedback.

**Example: Client-side filtering in action**

```ts
import { Component } from '@angular/core';

import {
  InfiniteTable,
  DataSource,
  defaultFilterTypes,
} from '@infinite-table/infinite-angular';
import type {
  DataSourceData,
  InfiniteTablePropColumns,
} from '@infinite-table/infinite-angular';

type Developer = {
  id: number;
  firstName: string;
  lastName: string;

  currency: string;
  preferredLanguage: string;
  stack: string;
  canDesign: 'yes' | 'no';

  salary: number;
};

defaultFilterTypes.string.operators.push({
  name: 'Not includes',
  label: 'Not Includes',
  fn: ({ currentValue, filterValue, emptyValues }) => {
    if (
      emptyValues.includes(currentValue) ||
      emptyValues.includes(filterValue)
    ) {
      return true;
    }
    return (
      typeof currentValue === 'string' &&
      typeof filterValue == 'string' &&
      !currentValue.toLowerCase().includes(filterValue.toLowerCase())
    );
  },
});

const data: DataSourceData<Developer> = () => {
  return fetch(process.env.NEXT_PUBLIC_BASE_URL + `/developers1k-sql?`)
    .then((r) => r.json())
    .then((data: Developer[]) => data);
};

const columns: InfiniteTablePropColumns<Developer> = {
  id: {
    field: 'id',
    type: 'number',
    defaultWidth: 70,
    defaultFilterable: false,
  },
  salary: {
    field: 'salary',
    type: 'number',
  },

  firstName: {
    field: 'firstName',
  },
  stack: { field: 'stack' },
  currency: { field: 'currency' },
};

const domProps = {
  style: {
    height: '100%',
  },
};

@Component({
  selector: 'demo-root',
  imports: [DataSource, InfiniteTable],
  template: `
    <infinite-data-source
      [data]="data"
      primaryKey="id"
      [defaultFilterValue]="[]"
      [filterDelay]="0"
    >
      <infinite-table
        debugId="basic-local-filter-example"
        [domProps]="domProps"
        [columnDefaultWidth]="150"
        [columnMinWidth]="50"
        [columns]="columns"
      />
    </infinite-data-source>
  `,
})
export default class App {
  protected readonly data = data;
  protected readonly domProps = domProps;
  protected readonly columns = columns;
}
```

If you still want filtering to be enabled with the default functionality of using the [`[(filterValue)]`](https://infinite-table.com/angular/docs/reference/datasource-props/index.md#filterValue) (or uncontrolled [`defaultFilterValue`](https://infinite-table.com/angular/docs/reference/datasource-props/index.md#defaultFilterValue)), but want to hide the column filter editors, you can set the [showColumnFilters](https://infinite-table.com/angular/docs/reference/datasource-props/index.md#showColumnFilters) property to `false`.

## Using Filter Types

As already documented in the [Understanding Filter Types](./#understanding-filter-types) section, you can specify the types of the filters the `<DataSource />` will support, by using the [`filterTypes`](https://infinite-table.com/angular/docs/reference/datasource-props/index.md#filterTypes) property.

The default filter types are `string` and `number` - read the next section to see how you can add new operators to those filter types.

A filter type is basically a collection of operators available for a type of data. Each operator needs a name and a function that will be used to filter the data, when that operator is applied.

```ts
// <infinite-data-source [(filterValue)]="filterValue" />
protected readonly filterValue = signal<DataSourcePropFilterValue<Developer>>(
  [
    {
      field: 'firstName',
      filter: {
        type: 'string',
        operator: 'includes',
        value: 'John',
      },
    },
    {
      field: 'age',
      filter: {
        type: 'number',
        operator: 'gt',
        value: 30,
      },
    },
  ],
);
```

The above filter value specifies that there are 2 filters applied:

- the `firstName` column applies a filter that will only match rows with `firstName` containining the string `John`
- the `age` column has an additional filter, that will only match rows with `age` greater than `30`

When filtering client-side, the rows are filtered using the functions of the `includes` operator in the `string` filter type and of the `gt` operator in the `number` filter type.

Here's a snippet of code from the `string` filter type showing the `includes` operator:

```ts
operators: [
  {
    name: 'includes',
    // an Angular component
    components: { Icon: () => renderComponent(IncludesOperatorIcon) },
    label: 'Includes',
    fn: ({ currentValue, filterValue }) => {
      return (
        typeof currentValue === 'string' &&
        typeof filterValue == 'string' &&
        currentValue.toLowerCase().includes(filterValue.toLowerCase())
      );
    },
  },
  //...
],
```

Let's now look at another example, of implementing a custom `salary` filter type.

For this, we override the `filterTypes` property of the `<DataSource />` component:

```ts
// <infinite-data-source [filterTypes]="filterTypes" />
protected readonly filterTypes: DataSourcePropFilterTypes<Developer> = {
  salary: {
    defaultOperator: 'gt',
    emptyValues: ['', null, undefined],
    operators: [
      /*...*/
    ],
  },
};
```

When you specify new [`filterTypes`](https://infinite-table.com/angular/docs/reference/datasource-props/index.md#filterTypes), the default filter types of `string` and `number` are still available - unless the new object contains those keys and overrides them explicitly.

The `salary` column has a custom filter type, with the following operators: `gt`, `gte`, `lt` and `lte`.

**Example: Client-side filtering in action with custom filter type**

```ts
import { Component, input } from '@angular/core';

import {
  DataSource,
  InfiniteTable,
  renderComponent,
} from '@infinite-table/infinite-angular';
import type {
  DataSourceData,
  DataSourcePropFilterTypes,
  InfiniteTablePropColumns,
} from '@infinite-table/infinite-angular';

type Developer = {
  id: number;
  firstName: string;
  lastName: string;

  currency: string;
  preferredLanguage: string;
  stack: string;
  canDesign: 'yes' | 'no';

  salary: number;
};

const data: DataSourceData<Developer> = () => {
  return fetch(process.env.NEXT_PUBLIC_BASE_URL + `/developers1k-sql?`)
    .then((r) => r.json())
    .then((data: Developer[]) => data);
};

const columns: InfiniteTablePropColumns<Developer> = {
  id: {
    field: 'id',
    type: 'number',
    defaultWidth: 100,
  },
  salary: {
    defaultFilterable: true,
    field: 'salary',
    type: 'number',
    filterType: 'salary',
  },

  firstName: {
    field: 'firstName',
  },
  stack: { field: 'stack' },
  currency: { field: 'currency', defaultFilterable: false },
};

@Component({
  selector: 'operator-icon',
  host: {
    style:
      'width: 20px; display: flex; justify-content: center; flex-flow: row',
  },
  template: `{{ icon() }}`,
})
class OperatorIcon {
  readonly icon = input.required<string>();
}

function getIcon(icon: string) {
  return () => renderComponent(OperatorIcon, { inputs: { icon } });
}

const filterTypes: DataSourcePropFilterTypes<Developer> = {
  salary: {
    defaultOperator: 'gt',
    emptyValues: ['', null, undefined],
    operators: [
      {
        name: 'gt',
        label: 'Greater Than',
        components: {
          Icon: getIcon('>'),
        },
        fn: ({ currentValue, filterValue }) => {
          return currentValue > filterValue;
        },
      },
      {
        name: 'gte',
        components: {
          Icon: getIcon('>='),
        },
        label: 'Greater Than or Equal',
        fn: ({ currentValue, filterValue }) => {
          return currentValue >= filterValue;
        },
      },
      {
        name: 'lt',
        components: {
          Icon: getIcon('<'),
        },
        label: 'Less Than',
        fn: ({ currentValue, filterValue }) => {
          return currentValue < filterValue;
        },
      },
      {
        name: 'lte',
        components: {
          Icon: getIcon('<='),
        },
        label: 'Less Than or Equal',
        fn: ({ currentValue, filterValue }) => {
          return currentValue <= filterValue;
        },
      },
    ],
  },
};

const domProps = {
  style: {
    height: '100%',
  },
};

@Component({
  selector: 'demo-root',
  imports: [DataSource, InfiniteTable],
  template: `
    <infinite-data-source
      [data]="data"
      primaryKey="id"
      [defaultFilterValue]="[]"
      [filterDelay]="0"
      [filterTypes]="filterTypes"
    >
      <infinite-table
        debugId="filter-custom-filter-type-example"
        [domProps]="domProps"
        [columnDefaultWidth]="150"
        [columnMinWidth]="50"
        [columns]="columns"
      />
    </infinite-data-source>
  `,
})
export default class App {
  protected readonly data = data;
  protected readonly filterTypes = filterTypes;
  protected readonly domProps = domProps;
  protected readonly columns = columns;
}
```

### Customizing Default Filter Types

By default, the `string` and `number` filter types are available. You can import the default filter types like this:

```ts
import { defaultFilterTypes } from '@infinite-table/infinite-angular';
```

If you want to make all your instances of `InfiniteTable` have new operators for those filter types, you can simply mutate the exported `defaultFilterTypes` object.

The `string` columns have a new `Not includes` operator.

**Example: Enhanced string filter type - new 'Not includes' operator**

```ts
import { Component } from '@angular/core';

import {
  InfiniteTable,
  DataSource,
  defaultFilterTypes,
} from '@infinite-table/infinite-angular';
import type {
  DataSourceData,
  InfiniteTablePropColumns,
} from '@infinite-table/infinite-angular';

type Developer = {
  id: number;
  firstName: string;
  lastName: string;

  currency: string;
  preferredLanguage: string;
  stack: string;
  canDesign: 'yes' | 'no';

  salary: number;
};

defaultFilterTypes.string.operators.push({
  name: 'Not contains',
  label: 'Not Contains',
  fn: ({ currentValue, filterValue, emptyValues }) => {
    if (
      emptyValues.includes(currentValue) ||
      emptyValues.includes(filterValue)
    ) {
      return true;
    }
    return (
      typeof currentValue === 'string' &&
      typeof filterValue == 'string' &&
      !currentValue.toLowerCase().includes(filterValue.toLowerCase())
    );
  },
});

const data: DataSourceData<Developer> = () => {
  return fetch(process.env.NEXT_PUBLIC_BASE_URL + `/developers1k-sql?`)
    .then((r) => r.json())
    .then((data: Developer[]) => data);
};

const columns: InfiniteTablePropColumns<Developer> = {
  id: {
    field: 'id',
    type: 'number',
    defaultWidth: 100,
  },

  firstName: {
    field: 'firstName',
  },
  stack: { field: 'stack' },

  salary: {
    field: 'salary',
    type: 'number',
  },
  currency: { field: 'currency', defaultFilterable: false },
};

const domProps = {
  style: {
    height: '100%',
  },
};

@Component({
  selector: 'demo-root',
  imports: [DataSource, InfiniteTable],
  template: `
    <infinite-data-source
      [data]="data"
      primaryKey="id"
      [defaultFilterValue]="[]"
      [filterDelay]="0"
    >
      <infinite-table
        debugId="customised-default-filter-types-example"
        [domProps]="domProps"
        [columnDefaultWidth]="150"
        [columnMinWidth]="50"
        [columns]="columns"
      />
    </infinite-data-source>
  `,
})
export default class App {
  protected readonly data = data;
  protected readonly domProps = domProps;
  protected readonly columns = columns;
}
```

When you specify new [`filterTypes`](https://infinite-table.com/angular/docs/reference/datasource-props/index.md#filterTypes), the default filter types of `string` and `number` are still available - unless the new object contains those keys and override them explicitly.

## Using a Filter Delay

In order to save some resources, filtering is batched by default. This is controlled by the [`filterDelay`](https://infinite-table.com/angular/docs/reference/datasource-props/index.md#filterDelay) prop, which, if not specified, defaults to `200` milliseconds. This means, any changes to the column filters, that happen inside a 200ms window (or the current value of [`filterDelay`](https://infinite-table.com/angular/docs/reference/datasource-props/index.md#filterDelay)), will be debounced and only the last value will be used to trigger a filter.

If you want to prevent debouncing/batching filter values, you can set [`filterDelay`](https://infinite-table.com/angular/docs/reference/datasource-props/index.md#filterDelay) to `0`.

API calls to [`setColumnFilter`](https://infinite-table.com/docs/reference/api/index.md#setColumnFilter) or [`clearColumnFilter`](https://infinite-table.com/docs/reference/api/index.md#clearColumnFilter) are not batched.

## Using a Filter Function Instead of the Column Filters

With a [`filterFunction`](https://infinite-table.com/angular/docs/reference/datasource-props/index.md#filterFunction), you decide how the rows are filtered client-side. It is called for every row, and the param it receives also has the current column filters (`filterValue` and `filterTypes`), so you can filter in any way you want:

- call `defaultFilterFunction`, which Infinite Table exports, to filter by the column filters the same way the built-in filtering does;
- write your own logic;
- or combine the two.

```ts
import { defaultFilterFunction } from '@infinite-table/infinite-angular';

// the built-in column filtering
// <infinite-data-source [filterFunction]="defaultFilterFunction" />

// your own logic only
const byId: DataSourcePropFilterFunction<Developer> = (param) =>
  param.data.id > 100;
// <infinite-data-source [filterFunction]="byId" />

// the column filters, and your own condition
const byColumnFiltersAndId: DataSourcePropFilterFunction<Developer> = (param) =>
  defaultFilterFunction(param) && param.data.id > 100;
// <infinite-data-source [filterFunction]="byColumnFiltersAndId" />
```

Loads data from remote location but will only show rows that have `id > 100`.

**Example: Custom filterFunction example**

```ts
import { Component } from '@angular/core';

import { DataSource, InfiniteTable } from '@infinite-table/infinite-angular';
import type {
  DataSourceData,
  DataSourcePropFilterFunction,
  InfiniteTablePropColumns,
} from '@infinite-table/infinite-angular';

type Developer = {
  id: number;
  firstName: string;
  lastName: string;

  currency: string;
  preferredLanguage: string;
  stack: string;
  canDesign: 'yes' | 'no';

  salary: number;
};

const data: DataSourceData<Developer> = () => {
  return fetch(process.env.NEXT_PUBLIC_BASE_URL + `/developers1k-sql?`)
    .then((r) => r.json())
    .then((data: Developer[]) => data);
};

const columns: InfiniteTablePropColumns<Developer> = {
  id: {
    field: 'id',
    type: 'number',
    defaultWidth: 100,
  },
  salary: {
    defaultFilterable: true,
    field: 'salary',
    type: 'number',
    filterType: 'salary',
  },

  firstName: {
    field: 'firstName',
  },
  stack: { field: 'stack' },
  currency: { field: 'currency', defaultFilterable: false },
};

const filterFunction: DataSourcePropFilterFunction<Developer> = ({ data }) => {
  return data.id > 100;
};

const domProps = {
  style: {
    height: '100%',
  },
};

@Component({
  selector: 'demo-root',
  imports: [DataSource, InfiniteTable],
  template: `
    <infinite-data-source [data]="data" primaryKey="id" [filterFunction]="filterFunction">
      <infinite-table
        debugId="filter-function-example"
        [domProps]="domProps"
        [columnDefaultWidth]="150"
        [columnMinWidth]="50"
        [columns]="columns"
      />
    </infinite-data-source>
  `,
})
export default class App {
  protected readonly data = data;
  protected readonly filterFunction = filterFunction;
  protected readonly domProps = domProps;
  protected readonly columns = columns;
}
```
