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

Checkboxes state is not persisting

Open NickToye opened this issue 8 years ago • 9 comments

On the latest beta version.

My checkboxes state is not persisting. See here in my sandbox:

https://codesandbox.io/s/3r0jx6kqnq

I believe it has something to do with me setting state within a function called from within the onRowsSelect function.

However, I can't figure out why. The checkbox becomes true and then quickly becomes false, and I'm wondering if it is refreshing the render and returning the component to its original state. Definitely feels like a bug though, but if its an error by me please let me know.

Possibly related is when you check the select all box it throws an error:

Cannot read property '1' of undefined

Tech Version
Material-UI Latest
MUI-datatables 2.0.0-beta-21
React 16.4.0
browser Chrome OSX 68.0.3440.75

NickToye avatar Aug 10 '18 12:08 NickToye

Ok thanks for sharing I’ll tru to take a look this weekend

gregnb avatar Aug 11 '18 19:08 gregnb

If it's going to be a controlled component then you need to pass in the selected rows as well otherwise it'll appear blank after the re-render.

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

gregnb avatar Aug 12 '18 02:08 gregnb

Ok the problem I had was that I want to access a column and apply that as a selected record value in state.

const data = [
      ['THC123789', '123456/1', '1', true, true, '2018-01-01', 0],
      ['THC123789', '159351/3', '1', true, false, null, 1],
      ['BVR9845', '789456/1', '1', true, true, '2018-02-05', 2],
      ['MBN2291', '693825/1', '1', false, false, '', 0],
    ];

So I I select a row I want to put in the 2nd value of the array, your code snippet just brings back the row id.

NickToye avatar Aug 13 '18 08:08 NickToye

I’m not seeing that in your code example? At minimum I think your first big issue was not passing the rowsSelected. Without that the table will never know which rows should be selected if it is a controlled component

Overall I’m not even sure I’m understanding what you’re trying to accomplish considering this is just something to identify selected rows and the callback returns the correct selected row.

gregnb avatar Aug 13 '18 11:08 gregnb

Ok, I've updated my example, sorry about that.

See in my example I have Name and Ref for example. When I check the rows, I want the reference numbers to go into state so I can pass them to other components.

Maybe if I explain the full scenario that I am going for here.

My data in the actual app will be server-side, and the function of the app is to be able to remove a booking date, and my employers want to do this as a bulk, so having the ability to check which ever rows they want to bulk edit and this will enable me to pass the state to a custom component that will update an API, and then return it on confirmation.

NickToye avatar Aug 13 '18 11:08 NickToye

See in my example I have Name and Ref for example. When I check the rows, I want the reference numbers to go into state so I can pass them to other components.

I'm still not seeing an issue? In the SS below I click on the first row and the last row the result gives me [0,3].

screen shot 2018-08-13 at 8 20 19 pm

I also see the proper values in the local state for that component.

Codesandbox: https://codesandbox.io/s/r44z758vy4

gregnb avatar Aug 14 '18 00:08 gregnb

Yeah I think we are good. I managed to get the value I was looking at from those rows with a bit of persevering.

NickToye avatar Aug 14 '18 08:08 NickToye

I have same issue when using setState (react-hook) inside onRowsSelect checkbox not working or state did not persistent

dionaditya avatar Jun 05 '20 07:06 dionaditya

optionTable have properties serverSide true.

dionaditya avatar Jun 05 '20 07:06 dionaditya