An action to clear selection
Is there a way to clear current selection without having to click on the checkbox in the table header ? I mean by implementing a button.
Hi @serotonyn ,
Unfortunately. It is controlled data by table.
Thank you @mbrn for your reply. In this case, I'm thinking about forcing component to update.
Yes ıt may work. Share result with us :)
I think these two tickets might be related? Just dropping the link here for others researching: https://github.com/mbrn/material-table/issues/221
You can pass in tableRef and using that set this tableRef.current.onAllSelected(false) which should clear active selection as well as selection header
how this can be done with Hooks?
@gatz2 I think this should explain how a hook is used, including example usage
import { useRef } from 'react'
...
const tableRef = useRef()
...
// Example usage
const clearSelection = () => {
// Should check that the element is available to prevent errors
if (tableRef.current) {
tableRef.current.onAllSelected(false)
}
}
...
<MaterialTable
tableRef={tableRef}
...
/>
<button onClick={clearSelection}>Clear selection</button>
A simple solution to this is:-
const Example = () => {
const table = useMaterialReactTable({
columns,
data,
<Button
onClick={() => {
// Get selected rows
const selectedRows = table.getSelectedRowModel().rows;
// Do some action
// Clear selected rows
table.resetRowSelection();
}}
>
Click me
</Button>
});
return <MaterialReactTable table={table} />;
};