In a MUI DataGrid
it is super easy to add a checkbox selection via the checkboxSelection
attribute and listen to selection changes via onSelectionChange
:
<DataGrid
columns={columns}
rows={rows}
pageSize={10}
checkboxSelection
onSelectionChange={e => console.log(e.rows)}
/>
But is there also a way to initialize the checkbox selection with a set of checked items?
Currently the
DataGrid
doesn't have a way to set the defaultselectionModel
(something likedefaultSelectionModel
prop), so in order to set the default selected rows, you need to use controlled mode by addingselectionModel
/onSelectionModelChange
and pass the initial value inuseState
. It's an ID array of the rows you wish to select at the start.Live Demo