1

I want to write some custom logic when the "select all" checkbox is checked in the react-data-grid. So how do i get a handle of that "select all" checkbox when the user clicks on it?

Here is the link to some of the react-data-grid examples http://adazzle.github.io/react-data-grid/#/ and the link to the repo https://github.com/adazzle/react-data-grid

In my render(), i have the react-data-grid defined as shown below.
The table when it is rendered looks like this react data grid demo

let dataGrid = <ReactDataGrid
            ref={node => this.grid = node}
            onGridSort={this.handleGridSort}
            enableCellSelect={true}
            columns={this._columns}
            rowGetter={this.rowGetter}
            rowsCount={this.getSize()}
            minWidth={this.state.width}
            minHeight={this.state.height}
            rowKey="id"
            rowHeight={90}
            headerRowHeight={35}
            rowSelection={{
                showCheckbox: true,
                enableShiftSelect: true,
                onRowsSelected: this.onRowsSelected,
                onRowsDeselected: this.onRowsDeselected,
                selectBy: {
                    indexes: this.state.selectedIndexes
                }
            }
            }
            emptyRowsView={EmptyRowsView}
        >
        </ReactDataGrid>;
roV
  • 11
  • 1
  • 4

2 Answers2

0

You can achieve this by calculating how many is selected.

Since onRowsSelected: this.onRowsSelected calls how many are rows are selected, you can create your logic in the onRowsSelected() function.

And your function should look like this:

onRowsSelected = (rows) => {
    this.setState({selectedIndexes: this.state.selectedIndexes.concat(rows.map(r => r.rowIdx))});

    let rowIndexes = rows.map(r => r.rowIdx);

    let totalSelected = rowIndexes.length + this.state.selectedIndexes.length;

    if(this.state.rows.length == totalSelected){
      console.log('All rows have been selected');
      //You argument here when SelectAll
    }
  };
SGhaleb
  • 979
  • 1
  • 12
  • 30
  • Yeah this should work. But if you look at the SelectAll.js code it has a onChange() https://github.com/adazzle/react-data-grid/blob/master/packages/react-data-grid/src/formatters/SelectAll.js. So is there a way we can listen to the event getting fired directly through refs or something? – roV Aug 24 '18 at 18:43
0

ReactDataGrid exposes a ref which we can access in our custom component. Through that we can access the "select all" checkbox.

<ReactDataGrid
            ref={node => this.grid = node}
            ... >
</ReactDataGrid>;

In your custom component, we can access the "select all" checkbox as

this.grid.selectAllCheckbox.checked; //returns either true/false based on checked/un-checked

roV
  • 11
  • 1
  • 4