Environment

Product Version3.15.0
ProductProgress® KendoReact

Description

How to make a stateful and reusable Grid?

Solution

Configuring Stateful Grids

Stateful components are class components that have a state which gets initialized in the constructor.

You can configure a stateful KendoReact Data Grid to perform paging, sorting, filtering, grouping, and editing, and also reuse it in multiple applications and pages by setting its columns and desired data operations.

Data Operations

The stateful Grid performs its data operations by using the process method of the DataQuery library. To apply the changes and to save the current state of the grid data when a data operation is performed, handle the onDataStateChange event.

    onDataStateChange={(e) => {
        this.setState({
            result: process(this.state.unprocessedData ? this.state.unprocessedData : this.props.data, e.data),
            dataState:e.data});
        }}

Editing

To configure the stateful Grid for editing:

  1. Set two different data collections. The first data collection will be displayed in the Grid after the data operations are applied. The second data collection will be used to edit and add items to the collection.

    If the stateful Grid is not intended to be groupable, you can use only one data collection.

        this.state = {
            result: process(this.props.data,{skip:0}),
            dataState: {skip: 0},
            unprocessedData: this.props.data };
  2. Handle the onItemChange event which will fire each time the user updates any of the editors.

        itemChange = (event) => {
            var data = this.state.unprocessedData
            var currentDataState = data;
    
            if (event.field === this.props.editField && event.value === 'delete') {
                data.splice(data.findIndex(d => d === event.dataItem), 1);
            } else {
                event.dataItem[event.field] = event.value;
            }
            this.setState({ result: process(data,this.state.dataState), unprocessedData: data });
        };
  3. Add items to the collection by using a function which you can call from a button click either inside the Grid toolbar or outside the Grid.

        addNew = () => {
            var data = this.state.unprocessedData
            data.unshift({[this.props.editField]: true, Discontinued: false, ProductID: 0})
            this.setState({
                result: process(data, this.props.pageable ? {group:this.state.dataState.group, take: this.state.dataState.take,filter: this.state.dataState.filter , skip: 0, sort:this.state.dataState.sort } : this.state.dataState),
                unprocessedData: data,
                dataState: this.props.pageable ? {group:this.state.dataState.group, take: this.state.dataState.take,filter: this.state.dataState.filter skip: 0, sort:this.state.dataState.sort } : this.state.dataState
            });
        };

Local Data Operations with HOC

You can sort, filter, or page the local data to which the KendoReact Data Grid is bound by using the Kendo UI Data Query component.

The following example demonstrates how to create a higher-order component (HOC) which uses the process() Data Query method to manage the local data operations. The HOC has its own state and adds the filter, sort, total, and skip props to the Grid to handle its onDataStateChange event. The HOC function is then applied for binding two Grids to different sets of data without the need for you to write any logic for the filtering, sorting, and paging operations.

Example
View Source
Edit In Stackblitz  
Change Theme: