mui-datatables icon indicating copy to clipboard operation
mui-datatables copied to clipboard

function passed to onRowsSelect option gets called with strange results

Open asharafshahi opened this issue 8 years ago • 7 comments

When I pass any function other than console.log to onRowsSelect, the checkbox never gets checked and the behavior is very odd

Expected Behavior

I would expect the function to get called and the checkbox to appear checked after.

Current Behavior

function doesn't get called

Steps to Reproduce (for bugs)


> const options = {
>       filter: true,
>       print: false,
>       filterType: 'dropdown', 
>       responsive: 'stacked',
>       selectableRows: true,
>       customToolbar: () => { return <CustomToolbar history={this.props.history} /> },
>       onRowsSelect: (_, rowsSelected) => { 
>         console.log(rowsSelected)
>         this.setState({ rowsSelected })
>       } 
>     };

..
<MUIDataTable
            title={"Template list"}
		data={data}
		columns={['Name','Description','Public','Output','Created', 'Edit', 'Delete']}
            options={options}
          />

1. 2. 3. 4.

Your Environment

Tech Version
Material-UI latest
MUI-datatables latest
React
browser
etc

asharafshahi avatar Aug 09 '18 03:08 asharafshahi

It seems if you setState within the onRowsSelected function it doesn't allow you to check the checkboxes, or it does, but it goes from true to false before the check is highlighted.

NickToye avatar Aug 10 '18 08:08 NickToye

This ended up working for me, not pretty but got the job done: onRowSelect = (currSelected, _) => { const data = {...currSelected[0]}; const selected = [...this.state.rowsSelected]; var index = selected.indexOf(data.index); if (index > -1) selected.splice(index, 1) else selected.push(data.index); this.setState({ rowsSelected: selected, }); };

asharafshahi avatar Aug 10 '18 13:08 asharafshahi

My issue is getting the selected rows into state because I want to pass them.

NickToye avatar Aug 10 '18 13:08 NickToye

That's what I did with the code above. It gets the indexes of the rows into state and then I use those later when a button is pressed in the custom selected toolbar

asharafshahi avatar Aug 10 '18 14:08 asharafshahi

Ok, I've updated mine to be like what you have there and my checkboxes don't work.

You take the setState code out and the checkboxes work, also the select all checkboxes cause an error which I have raised as part of another issue here, #128

NickToye avatar Aug 10 '18 14:08 NickToye

Going to look into both these issues this weekend. Thanks for raising them guys

gregnb avatar Aug 12 '18 02:08 gregnb

Could do something like this:

      rowsSelected: this.state.rowsSelected,
      onRowsSelect: (rowsSelected, allRows) => {
        const selected = allRows.map(item => item.index);
        this.setState({
          rowsSelected: selected,
        });
      },

Anyway, going to close the issue since it is not an issue the callback function simply fires successfully

gregnb avatar Aug 12 '18 02:08 gregnb