material-table icon indicating copy to clipboard operation
material-table copied to clipboard

Update checkbox state onRowClick

Open calebhypes opened this issue 7 years ago • 11 comments

I've written an onRowClick function to change the rows tableData.checked value when the row is click as seen in the answer here #300

I can see the checked state updating in a console log but the checkbox itself does not change visibly until I actually click another rows checkbox. It will then show all the checkboxes that had their tableData.checked values updated. I'd like to be able to have the checkbox actually display this change to the user onRowClick.

Here is my current code:

<MaterialTable
          onRowClick={(event, rowData) => {
            console.log(event.target, rowData);
            console.log(
              "Row Selection State Before: " + rowData.tableData.checked
            );
            rowData.tableData.checked = !rowData.tableData.checked;
            console.log(
              "Row Section State After: " + rowData.tableData.checked
            );
          }}
         options={{ selection: true}}
/>

This is the state of the UI on my first row click:

Screen Shot 2019-04-04 at 2 32 27 PM

Console Log on first row click:

Screen Shot 2019-04-04 at 2 32 46 PM

UI After selecting one checkbox (clicking directly on the checkbox of another row):

Screen Shot 2019-04-04 at 2 34 32 PM

Console Log after clicking the initial row again:

Screen Shot 2019-04-04 at 2 35 23 PM

Is there any way to make sure the UI updates for the MaterialTable component without resetting anything when the checked state is updated programmatically?

I also noticed that the header doesn't display a "N rows selected" as it does in the doc examples but thats an unrelated issue.

calebhypes avatar Apr 04 '19 18:04 calebhypes

Hi @CalebCox

You changed rowData but react does not know it. You should use setState to fire a render. You can find this rowData in your data and then update it with setState. Then it will be render.

mbrn avatar Apr 08 '19 19:04 mbrn

Ah, I hadn't thought of that at all, I had intended to create a current selection array for state but hadn't done it yet, after adding that and setting the checked items to the array in state it works!

Thank you as always!

calebhypes avatar Apr 08 '19 20:04 calebhypes

@mbrn I just noticed, when using onRowClick and updating the UI this way, it doesn't trigger the tables selection features (ie: select all checkbox set to indeterminate, table header switched to read for selected actions etc). Is there a function for this?

calebhypes avatar Apr 08 '19 20:04 calebhypes

Currently no. Because it calculates checked rows after a check event.

mbrn avatar Apr 08 '19 20:04 mbrn

@mbrn correct me if I'm wrong, from looking at the source it appears possible to modify m-table-header and material-table to update selectedCounton an onRowClick event. It should be possible to re-render these components with an updated selectedCount correct?

calebhypes avatar Apr 09 '19 14:04 calebhypes

but dataManager calculates selectedCount.

mbrn avatar Apr 09 '19 20:04 mbrn

@mbrn Thanks for that info! I'll try to get this working on my side, I feel I've gained a lot of help from you but haven't been able to contribute back. If I can get this working I'll submit a PR, I'm not very familiar with Typescript however so if I need to modify any of the TS files I'm not too terribly confident I can get this moving. 🤞

On another note, interestingly enough with 1.31.0, after you solved #436 if I click one of the items via the checkbox (normal selection) and then continue to click rows (onRowClick as mentioned above) the counter increments without issues.

calebhypes avatar Apr 11 '19 13:04 calebhypes

@mbrn

Re-visiting this finally, from what I can see, dataManager uses a function called changeRowSelected which is called by an onChange from m-table-body-row.js through a prop onRowSelected that's being passed by material-table.js, since this is getting triggered by an onChange function on the MUI <Checkbox /> what's stopping the onChange from firing when updating the checkbox components checked property?

calebhypes avatar Apr 23 '19 16:04 calebhypes

@mbrn It is a way to update checkbox state programatically without use the onRowClick event. Using the material-table in remote mode.

orestesparra avatar May 08 '19 15:05 orestesparra

You can call tableRef.onAllSelected(true)

mbrn avatar May 09 '19 04:05 mbrn

@mbrn, could you able to provide an example. TIA

KalanaPerera avatar Nov 13 '20 11:11 KalanaPerera