We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to search the records in grid on each keystroke

Platform: JavaScript - EJ 2 |
Control: Grid

Solution

In Grid, search action has been been handled by Search item of the toolbar. By default, on pressing the Enter key, search action will be started.

To overcome the default behavior of the search action, we are searching the Grid in keystrokes. On each keystroke (over the input element), certain time interval (of debouncing) has been handled to search the Grid.  Using event delegation concept, you can handle the keydown event for the input textbox (Search item).

Define the Grid

Define the Grid with the load event where you can bind the keydown event for the Grid.

 
import { Grid, Page, Selection, Toolbar } from '@syncfusion/ej2-grids';
import { categoryData } from './data-source';
 
Grid.Inject(Page, Selection, Toolbar);
 
let grid: Grid = new Grid(
    {
        dataSource: categoryData,
        allowPaging: true,
        toolbar: ['Search'],
        load: onLoad,
        columns: [
            { field: 'CategoryName', headerText: 'Category Name', width: 160 },
            { field: 'ProductName', headerText: 'Product Name', width: 170 },
            { field: 'QuantityPerUnit', headerText: 'Quantity Per Unit', width: 170, textAlign: 'Right' },
            { field: 'UnitsInStock', headerText: 'Units In Stock', width: 170, textAlign: 'Right' },
            {
                field: 'Discontinued', headerText: 'Discontinued', width: 150,
                textAlign: 'Center', displayAsCheckBox: true, type: 'boolean'
            }
        ],
        pageSettings: { pageCount: 5 }
    });
grid.appendTo('#Grid');
 
 

 

In the Keydown listener, search action has been handled using the search method of the Grid.

// debouncing method

const debounce = (func, delay) => {

    let debounceTimer

    return function () {

        const context = this;

        const args = arguments;

        clearTimeout(debounceTimer);

        debounceTimer = setTimeout(() => func.apply(context, args), delay)

    }

}

 

const onLoad = () => {

    // event delegation concept

    // debouncing concept

    grid.element.addEventListener('keydown', debounce((e) => {

        if (e.target.getAttribute('id').indexOf('_searchbar') !== - 1) {

            grid.search((e.target as HTMLInputElement).value);

        }

    }, 250));

}

 

 

Output

Grid with searched rows

Figure 1: Grid with the searched records

Typescript demo: https://stackblitz.com/edit/5g89yd-xhbewq?file=index.ts

Angular Demo: https://stackblitz.com/edit/angular-yta9sj?file=app.component.ts

React Demo: https://stackblitz.com/edit/react-hhbfey?file=index.js

ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile