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

How to set row(s) as selected based on rowData value?

Open kristinhorton opened this issue 6 years ago • 3 comments

I'm using material-table for a work project, and am wondering if it's possible to set row(s) as selected automatically based on a particular column of the data?

In my application I have an edit screen with a material-table of servers with the selection feature enabled. One of the columns called 'Related' is either true or false. I need the rows in which Related === true to automatically be selected, and those in which Related === false to be un-selected.

I can't post my code because it's against company policy, but I don't really think it's necessary here.

Has anyone already done this? Or is it possible?

kristinhorton avatar Sep 14 '20 21:09 kristinhorton

Pass this options object to the table

  options={{
                    selection: true,
                    selectionProps: (rowData) => ({
                      checked: <your logic here>},
                    }),
               }}

s-kris avatar Sep 22 '20 07:09 s-kris

Thanks @s-kris!

That works for checking rows where Related === true, but it doesn't allow me to uncheck those rows afterwards. In this use case I also want allow them to uncheck the rows that are checked, which would then set Related === false.

kristinhorton avatar Oct 01 '20 20:10 kristinhorton

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. You can reopen it if it required.

stale[bot] avatar Dec 30 '20 20:12 stale[bot]