Update checkbox state onRowClick
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:

Console Log on first row click:

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

Console Log after clicking the initial row again:

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.
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.
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!
@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?
Currently no. Because it calculates checked rows after a check event.
@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?
but dataManager calculates selectedCount.
@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.
@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?
@mbrn It is a way to update checkbox state programatically without use the onRowClick event. Using the material-table in remote mode.
You can call tableRef.onAllSelected(true)
@mbrn, could you able to provide an example. TIA