Articles in this section
Category / Section

How to search the records in grid on each keystroke?

5 mins read

This article explains how to search the records in the grid on each keystroke.

Solution

In JavaScript Grid, the search action is handled by the Search item of the toolbar. By default, pressing the Enter key starts the search action.

To override the default behavior of the search action, we are implementing a search feature on keystrokes. On each keystroke (over the input element), a certain time interval (using debouncing) is applied to search the Grid. Using the 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, the 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 demohttps://stackblitz.com/edit/5g89yd-xhbewq?file=index.ts

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

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


Conclusion

We hope you enjoyed learning about how to search the records in grid on each keystroke.

You can refer to our JavaScript Grid feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our JavaScript Grid example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsBoldDesk Support, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied