Advanced Filtering in JavaScript Data Grid

The Syncfusion® Grid component provides advanced filtering functionality through the Query Builder interface, which enables the creation of complex filtering conditions with multiple criteria and logical operators.

Enable advanced filtering

Advanced filtering is enabled by setting the allowAdvancedFiltering property to true. This adds an Filter button to the toolbar, which opens the Query Builder dialog for creating advanced filter conditions.

To use advanced filtering, inject the AdvancedFilter module into the Grid along with the other required modules.

var grid = new ej.grids.Grid({
    dataSource: ticketdata,
    enableVirtualization: true,
    allowSorting: true,
    height: 400,
    pageSettings: { pageSize: 50 },
    allowAdvancedFiltering: true,
    toolbar: ['AdvancedFilter'],
    load: function (args) {
        if (args) {
            args.enableSeamlessScrolling = true;
        }
    },
    rowHeight: 45,
    columns: [
        { field: 'TicketID', headerText: 'Ticket ID', textAlign: 'Right', width: 120, isPrimaryKey: true },
        { field: 'Title', headerText: 'Title', width: 260 },
        { field: 'TypeofRequest', headerText: 'Type', width: 150 },
        { field: 'Assignee', headerText: 'Assignee', width: 150 },
        { field: 'Priority', headerText: 'Priority', width: 130 },
        { field: 'Status', headerText: 'Status', width: 130 },
        { field: 'CreatedDate', headerText: 'Created Date', width: 140, textAlign: 'Right', format: 'yMd' },
        { field: 'DueDate', headerText: 'Due Date', width: 140, textAlign: 'Right', format: 'yMd' },
    ]
});
grid.appendTo('#Grid');
<!DOCTYPE html><html lang="en"><head>
    <title>EJ2 Grid</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Typescript Grid Control">
    <meta name="author" content="Syncfusion">
    <link href="index.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/35.2.12/material3.css" rel="stylesheet" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/fontawesome.css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/fontawesome.min.css" />
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
</head>
<body> 
    <div id='container'>
        <div id='Grid'></div>
    </div>
<script>
var ele = document.getElementById('container');
if(ele) {
  ele.style.visibility = "visible";
}   
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>

Initial filter

Initial filter conditions can be set and applied automatically when the Grid loads. The advancedFilterSettings property with queryBuilderSettings is used to define the initial rule.

The following example demonstrates displaying only tickets with a priority of High and a status other than Done.

var initialAdvancedFilterRule = {
    condition: 'and',
    rules: [
        {
            field: 'Status',
            label: 'Status',
            type: 'string',
            operator: 'notequal',
            value: 'Done'
        },
        {
            field: 'Priority',
            label: 'Priority',
            type: 'string',
            operator: 'equal',
            value: 'High'
        }
    ]
};

var grid = new ej.grids.Grid({
    dataSource: ticketdata,
    enableVirtualization: true,
    allowSorting: true,
    height: 400,
    pageSettings: { pageSize: 50 },
    allowAdvancedFiltering: true,
    toolbar: ['AdvancedFilter'],
    advancedFilterSettings: {
        queryBuilderSettings: {
            rule: initialAdvancedFilterRule
        }
    },
    load: function (args) {
        if (args) {
            args.enableSeamlessScrolling = true;
        }
    },
    rowHeight: 45,
    columns: [
        { field: 'TicketID', headerText: 'Ticket ID', textAlign: 'Right', width: 120, isPrimaryKey: true },
        { field: 'Title', headerText: 'Title', width: 260 },
        { field: 'TypeofRequest', headerText: 'Type', width: 150 },
        { field: 'Assignee', headerText: 'Assignee', width: 150 },
        { field: 'Priority', headerText: 'Priority', width: 130 },
        { field: 'Status', headerText: 'Status', width: 130 },
        { field: 'CreatedDate', headerText: 'Created Date', width: 140, textAlign: 'Right', format: 'yMd' },
        { field: 'DueDate', headerText: 'Due Date', width: 140, textAlign: 'Right', format: 'yMd' },
    ]
});
grid.appendTo('#Grid');
<!DOCTYPE html>
<html lang="en">

<head>
    <title>EJ2 Grid</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Typescript Grid Control">
    <meta name="author" content="Syncfusion">
    <link href="index.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/35.2.12/material3.css" rel="stylesheet" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/fontawesome.css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/fontawesome.min.css" />
    <script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
    <script src="es5-datasource.js" type="text/javascript"></script>
</head>

<body>
    <div id='container'>
        <div id='Grid'></div>
    </div>
    <script>
        var ele = document.getElementById('container');
        if (ele) {
            ele.style.visibility = "visible";
        }   
    </script>
    <script src="index.js" type="text/javascript"></script>
</body>

</html>

By default, hidden columns are not included in the advanced filter builder. Setting advancedFilterSettings.includeHiddenColumns to true displays hidden columns in the Query Builder, enabling filter conditions to be created using their values.

Advanced filtering events

The Advanced Filter feature provides the following events for customizing the dialog and filter operations.

  1. The advancedFilterOpen event is triggered when the Advanced Filter dialog opens. The event arguments provide access to the dialog and Query Builder instances.
  2. The advancedFilterClose event is triggered when the Advanced Filter dialog closes. Set the cancel property to true to prevent the dialog from closing.
  3. The advancedFilterActionBegin event is triggered before a filter is applied or cleared. Set the cancel property to true to prevent the operation. The event arguments include the filter rule, dialog instance, and Query Builder instance.
  4. The advancedFilterActionComplete event is triggered after a filter is applied or cleared. Set the cancel property to true to keep the Advanced Filter dialog open after the operation.

Methods

The Advanced Filter feature provides the following methods for controlling the dialog and managing filter rules programmatically:

  1. The openAdvancedFilterDialog method opens the Advanced Filter dialog.
  2. The closeAdvancedFilterDialog method closes the Advanced Filter dialog.
  3. The applyAdvancedFilter method applies the specified filter rule to the Grid.
  4. The clearAdvancedFilter method clears the applied Advanced Filter and restores the original data view.
  5. The getAdvancedFilter method retrieves the currently configured filter rule.
  6. The setAdvancedFilter method sets the specified rule as the current rule configuration in the Advanced Filter Query Builder.
var initialAdvancedFilterRule = {
    condition: 'and',
    rules: [
        {
            field: 'Status',
            label: 'Status',
            type: 'string',
            operator: 'notequal',
            value: 'Done'
        },
        {
            field: 'Priority',
            label: 'Priority',
            type: 'string',
            operator: 'equal',
            value: 'High'
        }
    ]
};

var grid = new ej.grids.Grid({
    dataSource: ticketdata,
    enableVirtualization: true,
    allowSorting: true,
    height: 400,
    pageSettings: { pageSize: 50 },
    allowAdvancedFiltering: true,
    toolbar: ['AdvancedFilter'],
    advancedFilterSettings: {
        queryBuilderSettings: {
            rule: initialAdvancedFilterRule
        }
    },
    load: function (args) {
        if (args) {
            args.enableSeamlessScrolling = true;
        }
    },
    rowHeight: 45,
    columns: [
        { field: 'TicketID', headerText: 'Ticket ID', textAlign: 'Right', width: 120, isPrimaryKey: true },
        { field: 'Title', headerText: 'Title', width: 260 },
        { field: 'TypeofRequest', headerText: 'Type', width: 150 },
        { field: 'Assignee', headerText: 'Assignee', width: 150 },
        { field: 'Priority', headerText: 'Priority', width: 130 },
        { field: 'Status', headerText: 'Status', width: 130 },
        { field: 'CreatedDate', headerText: 'Created Date', width: 140, textAlign: 'Right', format: 'yMd' },
        { field: 'DueDate', headerText: 'Due Date', width: 140, textAlign: 'Right', format: 'yMd' },
    ]
});
grid.appendTo('#Grid');

document.getElementById('open').onclick = function () { return grid.openAdvancedFilterDialog(); };
document.getElementById('apply').onclick = function () { return grid.applyAdvancedFilter(initialAdvancedFilterRule); };
document.getElementById('set').onclick = function () { return grid.setAdvancedFilter(initialAdvancedFilterRule); };
document.getElementById('get').onclick = function () { return console.log(grid.getAdvancedFilter()); };
document.getElementById('clear').onclick = function () { return grid.clearAdvancedFilter(); };
<!DOCTYPE html>
<html lang="en">

<head>
    <title>EJ2 Grid</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Typescript Grid Control">
    <meta name="author" content="Syncfusion">
    <link href="index.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/35.2.12/material3.css" rel="stylesheet" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/fontawesome.css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/fontawesome.min.css" />
    <script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
    <script src="es5-datasource.js" type="text/javascript"></script>
</head>

<body>
    <div id='container'>
        <button id="open">Open dialog</button>
        <button id="apply">Apply rule</button>
        <button id="set">Set rule</button>
        <button id="get">Get rule</button>
        <button id="clear">Clear filter</button>
        <div id='Grid'></div>
    </div>
    <script>
        var ele = document.getElementById('container');
        if (ele) {
            ele.style.visibility = "visible";
        }   
    </script>
    <script src="index.js" type="text/javascript"></script>
</body>

</html>