Search results

Filtering

Filtering allows you to view particular records based on filter criteria. To enable filtering in the Grid, set the allowFiltering to true. Filtering options can be configured through filterSettings.

To use filter, inject the Filter module in the grid.

Source
Preview
index.tsx
import * as React from "react";
import * as ReactDOM from "react-dom";
import { GridComponent, Inject, ColumnsDirective, ColumnDirective, Filter } from '@syncfusion/ej2-react-grids';
import { data } from './datasource';
class App extends React.Component<{}, {}>{
    render() {
        return <GridComponent dataSource={data} allowFiltering={true} height={273}>
            <ColumnsDirective>
                <ColumnDirective field='OrderID' width='100' textAlign="Right"></ColumnDirective>
                <ColumnDirective field='CustomerID' width='100'></ColumnDirective>
                <ColumnDirective field='EmployeeID' width='100' textAlign="Right"></ColumnDirective>
                <ColumnDirective field='Freight' width='100' format="C2" textAlign="Right"></ColumnDirective>
                <ColumnDirective field='ShipCountry' width='100'></ColumnDirective>
            </ColumnsDirective>
            <Inject services={[Filter]} />
        </GridComponent>
    }
};
ReactDOM.render(<App />, document.getElementById('grid'));

Initial filter

To apply the filter at initial rendering, set the filter predicate object in filterSettings.columns.

Source
Preview
index.tsx
import * as React from "react";
import * as ReactDOM from "react-dom";
import { GridComponent, Inject, ColumnsDirective, ColumnDirective, Filter, FilterSettingsModel } from '@syncfusion/ej2-react-grids';
import { data } from './datasource';
class App extends React.Component<{}, {}>{
    public FilterOptions: FilterSettingsModel = {
        columns: [{ field: 'ShipCity', matchCase: false, operator: 'startswith', predicate: 'and', value: 'reims' },
        { field: 'ShipName', matchCase: false, operator: 'startswith', predicate: 'and', value: 'Vins et alcools Chevalier' }]
    };
    render() {
        return <GridComponent dataSource={data} allowFiltering={true} filterSettings={this.FilterOptions} height={273}>
            <ColumnsDirective>
                <ColumnDirective field='OrderID' width='100' textAlign="Right"></ColumnDirective>
                <ColumnDirective field='CustomerID' width='100'></ColumnDirective>
                <ColumnDirective field='Freight' width='100' format="C2" textAlign="Right"></ColumnDirective>
                <ColumnDirective field='ShipCity' width='100'></ColumnDirective>
                <ColumnDirective field='ShipName' width='100'></ColumnDirective>
            </ColumnsDirective>
            <Inject services={[Filter]} />
        </GridComponent>
    }
};
ReactDOM.render(<App />, document.getElementById('grid'));

Filter operators

The filter operator for a column can be defined in filterSettings.columns.operator property.

The available operators and its supported data types are,

Operator  Description  Supported Types
startswith  Checks whether a value begins with the specified value.  String
endswith  Checks whether a value ends with specified value.  String
contains  Checks whether a value contains with specified value.  String
equal  Checks whether a value equal to specified value.  String | Number | Boolean | Date
notequal  Checks whether a value not equal to specified value.  String | Number | Boolean | Date
greaterthan  Checks whether a value is greater than with specified value.  Number | Date
greaterthanorequal Checks whether a value is greater than or equal to specified value.  Number | Date
lessthan  Checks whether a value is less than with specified value.  Number | Date
lessthanorequal  Checks whether a value is less than or equal to specified value.  Number | Date

Filter bar

By defining the allowFiltering to true, then filter bar row will be rendered next to header which allows you to filter data. You can filter the records with different expressions depending upon the column type.

Filter bar Expressions:

You can enter the following filter expressions(operators) manually in the filter bar.

Expression  Example  Description  Column Type
=value  equal  Numeric
!=  !=value  notequal  Numeric
>value  greaterthan  Numeric
<value  lessthan  Numeric
>=  >=value  greaterthanorequal  Numeric
<= <=value lessthanorequal  Numeric
*value  startswith  String
%value  endswith  String
N/A  N/A  Always equal operator will be used for Date filter  Date
N/A  N/A  Always equal operator will be used for Boolean filter  Boolean
Source
Preview
index.tsx
import * as React from "react";
import * as ReactDOM from "react-dom";
import { GridComponent, Inject, ColumnsDirective, ColumnDirective, Filter } from '@syncfusion/ej2-react-grids';
import { data } from './datasource';
class App extends React.Component<{}, {}>{
    render() {
        return <GridComponent dataSource={data} allowFiltering={true} height={273}>
            <ColumnsDirective>
                <ColumnDirective field='OrderID' width='100' textAlign="Right"></ColumnDirective>
                <ColumnDirective field='CustomerID' width='100'></ColumnDirective>
                <ColumnDirective field='EmployeeID' width='100' textAlign="Right"></ColumnDirective>
                <ColumnDirective field='Freight' width='100' format="C2" textAlign="Right"></ColumnDirective>
                <ColumnDirective field='ShipCountry' width='100'></ColumnDirective>
            </ColumnsDirective>
            <Inject services={[Filter]} />
        </GridComponent>
    }
};
ReactDOM.render(<App />, document.getElementById('grid'));

By default, the filterSettings.columns.operator value is equal.

Filter bar template with custom component

The filterBarTemplate is used to add a custom component for a particular column and this contains the following functions.

  • create – It is used for creating custom components.
  • write - It is used to wire events for custom components.

In the following sample dropdown is used as custom component in EmployeeID column.

Source
Preview
index.tsx
import * as React from "react";
import * as ReactDOM from "react-dom";
import { GridComponent, Inject, ColumnsDirective, ColumnDirective, Filter, IFilterUI } from '@syncfusion/ej2-react-grids';
import { data } from './datasource';
class App extends React.Component<{}, {}>{
    private grid: Grid;
    public templateOptions: IFilterUI = {
        create: (args: { element: Element, column: Column }) => {
            let dd: HTMLInputElement = document.createElement('select');
            dd.id = 'EmployeeID';
            let dataSource: string[]= ['All','1','3','4','5','6','8','9'];
            for(let i: number =0;i<dataSource.length;i++){
                let option: HTMLInputElement = document.createElement('option');
                option.value = dataSource[i];
                option.innerHTML = dataSource[i];
                dd.appendChild(option);
            }
            return dd;
        },
        write: (args: { element: Element, column: Column }) => {
            args.element.addEventListener('input', (args: { element: HTMLInputElement, columnIndex: number, column: Column }) => {
                if(args.currentTarget.value !== 'All') {
                    let value: number = +args.currentTarget.value;
                    this.grid.filterByColumn(args.currentTarget.id, 'equal', value);
                } else {
                    this.grid.removeFilteredColsByField(args.currentTarget.id);
                }
            });
        },
    };
    render() {
        return <GridComponent dataSource={data} allowFiltering={true} height={273} ref={g => this.grid = g}>
            <ColumnsDirective>
                <ColumnDirective field='OrderID' width='100' textAlign="Right"></ColumnDirective>
                <ColumnDirective field='CustomerID' width='100' ></ColumnDirective>
                <ColumnDirective field='EmployeeID' width='100' filterBarTemplate={this.templateOptions} textAlign="Right"></ColumnDirective>
                <ColumnDirective field='Freight' width='100' format="C2" textAlign="Right"></ColumnDirective>
                <ColumnDirective field='ShipCountry' width='100'></ColumnDirective>
            </ColumnsDirective>
            <Inject services={[Filter]} />
        </GridComponent>
    }
};
ReactDOM.render(<App />, document.getElementById('grid'));

Filter menu

You can enable filter menu by setting the filterSettings.type as Menu. The filter menu UI will be rendered based on its column type, which allows you to filter data. You can filter the records with different operators.

Source
Preview
index.tsx
import * as React from "react";
import * as ReactDOM from "react-dom";
import { GridComponent, Inject, ColumnsDirective, ColumnDirective, Filter, FilterSettingsModel } from '@syncfusion/ej2-react-grids';
import { data } from './datasource';
class App extends React.Component<{}, {}>{
 public FilterOptions: FilterSettingsModel = {
        type: 'Menu'
    };
    render() {
        return <GridComponent dataSource={data} filterSettings={this.FilterOptions} allowFiltering={true} height={273}>
            <ColumnsDirective>
                <ColumnDirective field='OrderID' width='100' textAlign="Right"></ColumnDirective>
                <ColumnDirective field='CustomerID' width='100'></ColumnDirective>
                <ColumnDirective field='EmployeeID' width='100' textAlign="Right"></ColumnDirective>
                <ColumnDirective field='Freight' width='100' format="C2" textAlign="Right"></ColumnDirective>
                <ColumnDirective field='ShipCountry' width='100'></ColumnDirective>
            </ColumnsDirective>
            <Inject services={[Filter]} />
        </GridComponent>
    }
};
ReactDOM.render(<App />, document.getElementById('grid'));

Custom component in filter menu

The column.filter.ui is used to add custom filter components to a particular column. To implement custom filter ui, define the following functions:

  • create: Creates custom component.
  • write: Wire events for custom component.
  • read: Read the filter value from custom component.

In the following sample, dropdown is used as custom component in the OrderID column.

Source
Preview
index.tsx
import * as React from "react";
import * as ReactDOM from "react-dom";
import { GridComponent, Inject, ColumnsDirective, ColumnDirective, Filter,
 FilterSettingsModel, IFilter } from '@syncfusion/ej2-react-grids';
import { DropDownList } from '@syncfusion/ej2-react-dropdowns';
import { createElement } from '@syncfusion/ej2-base';
import { DataManager } from '@syncfusion/ej2-data';
import { data } from './datasource';
class App extends React.Component<{}, {}>{
 public FilterOptions: FilterSettingsModel = {
        type: 'Menu'
    };
public Filter : IFilter = {
    ui: {
        create: (args: { target: Element, column: Object }) => {
            let db: Object = new DataManager(data);
            let flValInput: HTMLElement = createElement('input', { className: 'flm-input' });
            args.target.appendChild(flValInput);
            this.dropInstance = new DropDownList({
                dataSource: new DataManager(data),
                fields: { text: 'OrderID', value: 'OrderID' },
                placeholder: 'Select a value',
                popupHeight: '200px'
            });
            this.dropInstance.appendTo(flValInput);
         },
         write: (args: {
             column: Object, target: Element, parent: any,
             filteredValue: number | string }) => {
                 this.dropInstance.value = args.filteredValue;
                 },
         read: (args: { target: Element, column: any, operator: string, fltrObj: Filter }) => {
             args.fltrObj.filterByColumn(args.column.field, args.operator, this.dropInstance.value);
             }
        }
    }
    render() {
        return <GridComponent dataSource={data} filterSettings={this.FilterOptions} allowFiltering={true} height={273}>
            <ColumnsDirective>
                <ColumnDirective field='OrderID' filter={this.Filter} width='100' textAlign="Right"></ColumnDirective>
                <ColumnDirective field='CustomerID' width='100'></ColumnDirective>
                <ColumnDirective field='EmployeeID' width='100' textAlign="Right"></ColumnDirective>
                <ColumnDirective field='Freight' width='100' format="C2" textAlign="Right"></ColumnDirective>
                <ColumnDirective field='ShipCountry' width='100'></ColumnDirective>
            </ColumnsDirective>
            <Inject services={[Filter]} />
        </GridComponent>
    }
};
ReactDOM.render(<App />, document.getElementById('grid'));

Enable different filter for a column

You can use both Menu and CheckBox filter in a same Grid. To do so, set the column.filter.type as Menu or CheckBox.

In the following sample menu filter is enabled by default and checkbox filter is enabled for the CustomerID column using the column.filter.type.

Source
Preview
index.tsx
import * as React from "react";
import * as ReactDOM from "react-dom";
import { GridComponent, Inject, ColumnsDirective, ColumnDirective, Filter, FilterSettingsModel,
IFilter } from '@syncfusion/ej2-react-grids';
import { data } from './datasource';
class App extends React.Component<{}, {}>{
 public FilterOptions: FilterSettingsModel = {
        type: 'Menu'
    };
public Filter : IFilter = {
    type: 'CheckBox'
    }
    render() {
        return <GridComponent dataSource={data} filterSettings={this.FilterOptions} allowFiltering={true} height={273}>
            <ColumnsDirective>
                <ColumnDirective field='OrderID' width='100' textAlign="Right"></ColumnDirective>
                <ColumnDirective field='CustomerID' filter={this.Filter} width='100'></ColumnDirective>
                <ColumnDirective field='EmployeeID' width='100' textAlign="Right"></ColumnDirective>
                <ColumnDirective field='Freight' width='100' format="C2" textAlign="Right"></ColumnDirective>
                <ColumnDirective field='ShipCountry' width='100'></ColumnDirective>
            </ColumnsDirective>
            <Inject services={[Filter]} />
        </GridComponent>
    }
};
ReactDOM.render(<App />, document.getElementById('grid'));

Filter template

Filter template provides an option to use the custom filter UI for a particular column by using the columns.filterTemplate property. The custom filter UI provided by columns.filterTemplate can be used by the filter bar, menu, and advanced filter from an excel filter.

The columns.filterTemplate property value should be a React functional component.

Filter Bar

You can customize default filter bar component of a column by custom component using filter template.

The following example demonstrates the way to use filter template for a column when using filter bar. In the following example, the DropdownList component is used to filter Name column using filter template.

Source
Preview
index.tsx
import * as React from "react";
import * as ReactDOM from "react-dom";
import { DropDownListComponent } from "@syncfusion/ej2-react-dropdowns"
import { GridComponent, Inject, ColumnsDirective, ColumnDirective, Filter } from '@syncfusion/ej2-react-grids';
import { data } from './datasource';

class App extends React.Component<{}, {}>{
    public grid = GridComponent;
    public fields: object = { text: 'Name', value: 'Name' };
    public filterTemplate(props:any): any {
        return (<DropDownListComponent id={props.column.field} popupHeight="250px" fields={this.fields} change={this.onChange.bind(this)} dataSource={data} /> );
    }
     onChange(args: any): any{
       this.grid.filterByColumn('Name', 'equal', args.value);
     }
    render() {
        return <GridComponent ref={g => this.grid = g} dataSource={data} allowFiltering={true} >
            <ColumnsDirective>
                <ColumnDirective field='EmployeeID' width='140' textAlign="Right"></ColumnDirective>
                <ColumnDirective field='Name' filterTemplate={this.filterTemplate.bind(this)} width='140'></ColumnDirective>
                <ColumnDirective field='ShipName' width='170' textAlign="Right"></ColumnDirective>
                <ColumnDirective field='CustomerID' width='140' textAlign="Right"></ColumnDirective>
            </ColumnsDirective>
            <Inject services={[Filter]} />
        </GridComponent>
    }
}
ReactDOM.render(<App />, document.getElementById('grid'));

Filter Menu

You can customize default filter menu component of a column by custom component using filter template.

The following example demonstrates the way to use filter template for a column when using filter menu. In the following example, the DropdownList component is used to filter ShipName column using filter template.

Source
Preview
index.tsx
import * as React from "react";
import * as ReactDOM from "react-dom";
import { DropDownListComponent } from "@syncfusion/ej2-react-dropdowns"
import { GridComponent, Inject, ColumnsDirective, ColumnDirective, Filter, FilterSettingsModel } from '@syncfusion/ej2-react-grids';
import { data } from './datasource';

class App extends React.Component<{}, {}>{
    public grid = GridComponent;
    public fields: object = { text: 'ShipName', value: 'ShipName' };
    public filterTemplate(props:any): any {
        return (<DropDownListComponent id={props.column.field} popupHeight="250px" fields={this.fields} dataSource={data} /> );
    }
     public FilterOptions: FilterSettingsModel = {
        type: 'Menu'
    };
    render() {
        return <GridComponent ref={g => this.grid = g} dataSource={data} allowFiltering={true}  filterSettings={this.FilterOptions} >
            <ColumnsDirective>
                <ColumnDirective field='EmployeeID' width='140' textAlign="Right"></ColumnDirective>
                <ColumnDirective field='Name' width='140'></ColumnDirective>
                <ColumnDirective field='ShipName' filterTemplate={this.filterTemplate.bind(this)} width='170' textAlign="Right"></ColumnDirective>
                <ColumnDirective field='CustomerID' width='140' textAlign="Right"></ColumnDirective>
            </ColumnsDirective>
            <Inject services={[Filter]} />
        </GridComponent>
    }
}
ReactDOM.render(<App />, document.getElementById('grid'));

Excel Filter

You can use the columns.filterTemplate property to define custom component in advanced filter UI from excel filter for a particular column.

The following example demonstrates the way to use filter template for a column when using excel filter. In the following example, the DropdownList component is used to filter CustomerID column using filter template.

Source
Preview
index.tsx
import * as React from "react";
import * as ReactDOM from "react-dom";
import { DropDownListComponent } from "@syncfusion/ej2-react-dropdowns"
import { GridComponent, Inject, ColumnsDirective, ColumnDirective, Filter, FilterSettingsModel } from '@syncfusion/ej2-react-grids';
import { data } from './datasource';

class App extends React.Component<{}, {}>{
    public grid = GridComponent;
    public fields: object = { text: 'CustomerID', value: 'CustomerID' };
    public filterTemplate(props:any): any {
        return (<DropDownListComponent id={props.column.field} popupHeight="250px" fields={this.fields} dataSource={data} /> );
    };
     public FilterOptions: FilterSettingsModel = {
        type: 'Excel'
    };
    render() {
        return <GridComponent ref={g => this.grid = g} dataSource={data} allowFiltering={true}  filterSettings={this.FilterOptions} >
            <ColumnsDirective>
                <ColumnDirective field='EmployeeID' width='140' textAlign="Right"></ColumnDirective>
                <ColumnDirective field='Name' width='140'></ColumnDirective>
                <ColumnDirective field='CustomerID' filterTemplate={this.filterTemplate.bind(this)} width='140' textAlign="Right"></ColumnDirective>
                <ColumnDirective field='ShipName' width='170' textAlign="Right"></ColumnDirective>
            </ColumnsDirective>
            <Inject services={[Filter]} />
        </GridComponent>
    }
}
ReactDOM.render(<App />, document.getElementById('grid'));

Template Context

The filter template should be a React Component. You can access the column information inside the component.

The following properties will be available at the time of template execution.

Property Name Usage
column Get the current column information.

In the below code example, we have enabled the DropDownList which is used as filter UI for Name column based on the columns.allowFiltering property.

// The enabled attributes will be added based on the column property.
<DropDownListComponent id='dropdown' enabled={props.column.allowFiltering} popupHeight="250px" fields={this.fields} change={this.onChange.bind(this)} dataSource={data} />

Diacritics

By default, grid ignores diacritic characters while filtering. To include diacritic characters, set the filterSettings.ignoreAccent as true.

In the following sample, type aero in Name column to filter diacritic characters.

Source
Preview
index.tsx
import * as React from "react";
import * as ReactDOM from "react-dom";
import { GridComponent, Inject, ColumnsDirective, ColumnDirective, Filter, FilterSettingsModel } from '@syncfusion/ej2-react-grids';
import { data } from './datasource';

class App extends React.Component<{}, {}>{
 public FilterOptions: FilterSettingsModel = {
        ignoreAccent: true
    };
    render() {
        return <GridComponent dataSource={data} filterSettings={this.FilterOptions} allowFiltering={true} >
            <ColumnsDirective>
                <ColumnDirective field='EmployeeID' width='140' textAlign="Right"></ColumnDirective>
                <ColumnDirective field='Name' width='140'></ColumnDirective>
                <ColumnDirective field='ShipName' width='170' textAlign="Right"></ColumnDirective>
                <ColumnDirective field='CustomerID' width='140' textAlign="Right"></ColumnDirective>
            </ColumnsDirective>
            <Inject services={[Filter]} />
        </GridComponent>
    }
}
ReactDOM.render(<App />, document.getElementById('grid'));

See also