Articles in this section
Category / Section

How to search the records in grid on each keystroke?

5 mins read

Solution

In JavaScript 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


Conclusion

I 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 know 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 forumsDirect-Trac, 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 sign in to leave a comment
Access denied
Access denied