Search results

Filtering in React TreeGrid component

30 Jan 2023 / 5 minutes to read

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

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

To get start quickly with filtering functionalities, you can check on this video:

Source
Preview
App.jsx
App.tsx
Copied to clipboard
import { ColumnDirective, ColumnsDirective, Filter, Inject, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import './App.css';
import { sampleData } from './datasource';
function App() {
    return <TreeGridComponent dataSource={sampleData} treeColumnIndex={1} childMapping='subtasks' height='275' allowFiltering={true}>
        <ColumnsDirective>
          <ColumnDirective field='taskID' headerText='Task ID' width='75' textAlign='Right'/>
          <ColumnDirective field='taskName' headerText='Task Name' width='180'/>
          <ColumnDirective field='startDate' headerText='Start Date' width='90' format='yMd' textAlign='Right' type='date'/>
          <ColumnDirective field='duration' headerText='Duration' width='80' textAlign='Right'/>
        </ColumnsDirective>
        <Inject services={[Filter]}/>
    </TreeGridComponent>;
}
;
export default App;
Copied to clipboard
import { ColumnDirective, ColumnsDirective, Filter, Inject, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import './App.css';
import { sampleData } from './datasource';

function App() {
    return <TreeGridComponent dataSource={sampleData} treeColumnIndex={1} childMapping='subtasks' height='275' allowFiltering={true}>
        <ColumnsDirective>
          <ColumnDirective field='taskID' headerText='Task ID' width='75' textAlign='Right'/>
          <ColumnDirective field='taskName' headerText='Task Name' width='180'/>
          <ColumnDirective field='startDate' headerText='Start Date' width='90' format='yMd' textAlign='Right' type='date' />
          <ColumnDirective field='duration' headerText='Duration' width='80' textAlign='Right' />
        </ColumnsDirective>
        <Inject services={[Filter]}/>
    </TreeGridComponent>
};
export default App;

Filter hierarchy modes

TreeGrid provides support for a set of filtering modes with filterSettings.filterHierarchyMode property. The below are the type of filter mode available in TreeGrid.

  • Parent : This is the default filter hierarchy mode in TreeGrid. The filtered records are displayed with its parent records, if the filtered records not have any parent record then the filtered records only displayed.
  • Child : The filtered records are displayed with its child record, if the filtered records not have any child record then the filtered records only displayed.
  • Both : The filtered records are displayed with its both parent and child record, if the filtered records not have any parent and child record then the filtered records only displayed.
  • None : The filtered records are only displayed.
Source
Preview
App.jsx
App.tsx
Copied to clipboard
import { DropDownListComponent } from "@syncfusion/ej2-react-dropdowns";
import { ColumnDirective, ColumnsDirective, Filter } from '@syncfusion/ej2-react-treegrid';
import { Inject, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import './App.css';
import { sampleData } from './datasource';
function App() {
    let treegrid;
    const data = ['Parent', 'Child', 'Both', 'None'];
    const onChange = (sel) => {
        const mode = sel.value.toString();
        if (treegrid) {
            treegrid.filterSettings.hierarchyMode = mode;
            treegrid.clearFiltering();
        }
    };
    return (<div><DropDownListComponent popupHeight="250px" dataSource={data} value="Parent" change={onChange} width="150px"/>
    <TreeGridComponent dataSource={sampleData} treeColumnIndex={1} childMapping='subtasks' height='225' allowFiltering={true} ref={g => treegrid = g}>
    <ColumnsDirective>
        <ColumnDirective field='taskID' headerText='Task ID' width='75' textAlign='Right'/>
        <ColumnDirective field='taskName' headerText='Task Name' width='180'/>
        <ColumnDirective field='startDate' headerText='Start Date' width='90' format='yMd' textAlign='Right' type='date'/>
        <ColumnDirective field='duration' headerText='Duration' width='80' textAlign='Right'/>
    </ColumnsDirective>
    <Inject services={[Filter]}/>
</TreeGridComponent></div>);
}
;
export default App;
Copied to clipboard
import { ChangeEventArgs, DropDownListComponent } from "@syncfusion/ej2-react-dropdowns";
import { ColumnDirective, ColumnsDirective, Filter, FilterHierarchyMode } from '@syncfusion/ej2-react-treegrid';
import { Inject, TreeGrid, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import './App.css';
import { sampleData } from './datasource';

function App() {
let treegrid: TreeGridComponent | null;
const data: string[] = ['Parent', 'Child', 'Both', 'None']
const onChange = (sel: ChangeEventArgs): void => {
    const mode: FilterHierarchyMode = sel.value.toString() as FilterHierarchyMode;
    if (treegrid) {
        treegrid.filterSettings.hierarchyMode = mode;
        treegrid.clearFiltering();
    }
}

return(<div><DropDownListComponent popupHeight="250px" dataSource={data} value="Parent" change={onChange} width="150px"/>
    <TreeGridComponent dataSource={sampleData} treeColumnIndex={1} childMapping='subtasks' height='225' allowFiltering={true} ref={g => treegrid = g}>
    <ColumnsDirective>
        <ColumnDirective field='taskID' headerText='Task ID' width='75' textAlign='Right'/>
        <ColumnDirective field='taskName' headerText='Task Name' width='180'/>
        <ColumnDirective field='startDate' headerText='Start Date' width='90' format='yMd' textAlign='Right' type='date' />
        <ColumnDirective field='duration' headerText='Duration' width='80' textAlign='Right' />
    </ColumnsDirective>
    <Inject services={[Filter]}/>
</TreeGridComponent></div>)
};
export default App;

Initial filter

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

Source
Preview
App.jsx
App.tsx
Copied to clipboard
import { ColumnDirective, ColumnsDirective, Filter } from '@syncfusion/ej2-react-treegrid';
import { Inject, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import './App.css';
import { sampleData } from './datasource';
function App() {
    const FilterOptions = {
        columns: [
            { field: 'taskName', matchCase: false, operator: 'startswith', predicate: 'and', value: 'plan' },
            { field: 'duration', matchCase: false, operator: 'equal', predicate: 'and', value: 5 }
        ]
    };
    return <TreeGridComponent dataSource={sampleData} treeColumnIndex={1} childMapping='subtasks' height='275' allowFiltering={true} filterSettings={FilterOptions}>
    <ColumnsDirective>
      <ColumnDirective field='taskID' headerText='Task ID' width='75' textAlign='Right'/>
      <ColumnDirective field='taskName' headerText='Task Name' width='180'/>
      <ColumnDirective field='startDate' headerText='Start Date' width='90' format='yMd' textAlign='Right' type='date'/>
      <ColumnDirective field='duration' headerText='Duration' width='80' textAlign='Right'/>
    </ColumnsDirective>
    <Inject services={[Filter]}/>
</TreeGridComponent>;
}
;
export default App;
Copied to clipboard
import { ColumnDirective, ColumnsDirective, Filter, FilterSettingsModel } from '@syncfusion/ej2-react-treegrid';
import { Inject, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import './App.css';
import { sampleData } from './datasource';

function App() {
const FilterOptions: FilterSettingsModel = {
    columns: [
        { field: 'taskName', matchCase: false, operator: 'startswith', predicate: 'and', value: 'plan' },
        { field: 'duration', matchCase: false, operator: 'equal', predicate: 'and', value: 5 }]
};

return <TreeGridComponent dataSource={sampleData} treeColumnIndex={1} childMapping='subtasks'
      height='275' allowFiltering={true} filterSettings={FilterOptions}>
    <ColumnsDirective>
      <ColumnDirective field='taskID' headerText='Task ID' width='75' textAlign='Right'/>
      <ColumnDirective field='taskName' headerText='Task Name' width='180'/>
      <ColumnDirective field='startDate' headerText='Start Date' width='90' format='yMd' textAlign='Right' type='date' />
      <ColumnDirective field='duration' headerText='Duration' width='80' textAlign='Right' />
    </ColumnsDirective>
    <Inject services={[Filter]}/>
</TreeGridComponent>
};
export default App;

Filter operators

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

The available operators and its supported data types are:

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

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

Diacritics

By default, treegrid 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
App.jsx
App.tsx
Copied to clipboard
import { ColumnDirective, ColumnsDirective, Filter } from '@syncfusion/ej2-react-treegrid';
import { Inject, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import './App.css';
import { diacritics } from './datasource';
function App() {
    const FilterOptions = {
        ignoreAccent: true
    };
    return <TreeGridComponent dataSource={diacritics} treeColumnIndex={0} childMapping='Children' height='275' allowFiltering={true} filterSettings={FilterOptions}>
    <ColumnsDirective>
      <ColumnDirective field='EmpID' headerText='Employee ID' width='95'/>
      <ColumnDirective field='Name' headerText='Name' width='180'/>
      <ColumnDirective field='DOB' headerText='DOB' width='90' format='yMd' textAlign='Right' type='date'/>
      <ColumnDirective field='Country' headerText='Country' width='100'/>
    </ColumnsDirective>
    <Inject services={[Filter]}/>
</TreeGridComponent>;
}
;
export default App;
Copied to clipboard
import { ColumnDirective, ColumnsDirective, Filter, FilterSettingsModel } from '@syncfusion/ej2-react-treegrid';
import { Inject, TreeGridComponent } from '@syncfusion/ej2-react-treegrid';
import * as React from 'react';
import './App.css';
import { diacritics } from './datasource';

function App() {
const FilterOptions: FilterSettingsModel = {
    ignoreAccent: true
};

return <TreeGridComponent dataSource={diacritics} treeColumnIndex={0} childMapping='Children'
    height='275' allowFiltering={true} filterSettings={FilterOptions}>
    <ColumnsDirective>
      <ColumnDirective field='EmpID' headerText='Employee ID' width='95'/>
      <ColumnDirective field='Name' headerText='Name' width='180'/>
      <ColumnDirective field='DOB' headerText='DOB' width='90' format='yMd' textAlign='Right' type='date' />
      <ColumnDirective field='Country' headerText='Country' width='100' />
    </ColumnsDirective>
    <Inject services={[Filter]}/>
</TreeGridComponent>
};
export default App;

You can refer to our React Tree Grid feature tour page for its groundbreaking feature representations. You can also explore our React Tree Grid example to knows how to present and manipulate data.