[DataGrid] Enable/disable selection on certain rows with custom function
- [x] I have searched the issues of this repository and believe that this is not a duplicate.
Summary 💡
Enable/disable selection on certain rows with custom function. Returns true if not provided.
Examples 🌈
https://codesandbox.io/s/github/gregnb/mui-datatables?file=/examples/selectable-rows/index.js
Motivation 🔦
The first variation: in my table there are two types of entities, the first of them has the option of choosing the other not. The second option: when we select one entity, the other is automatically unavailable for selection.
Order id 💳
19652
@Neonin This issue description is not clear. Here is what I understand. There are multiple selection features in the grid, I have to assume you are talking about the checkbox selection.
So why is the controlled mode not enough? https://material-ui.com/components/data-grid/selection/#controlled-selection. You could decide to ignore the selection callback on some of the rows. If you control the selection, you have 100% control over it.
@oliviertassinari Sorry for the confusing explanation. I am developing a system for accounting for passengers on the bus. We have a seat with a passenger and the seat is empty. When choosing a passenger place for an action, for example, sending messages, I have to check the validity of the data every time (whether this space is occupied), and it turns out that the click should not work like this other object, it looks more like a bug. And if you immediately block another entity, it will be more convenient and understandable for the user.
@Neonin Ok, I see. Are you looking for something in this order?
A disabled checkbox or a hidden one? Did you consider rendering your own column with checkboxes?
One benchmark: https://ag-grid.com/react-grid/row-selection/#example-selectable-rows-with-header-checkbox
@oliviertassinari Yes 👌, I need to block a specific checkbox depending on the condition. Next time I will provide more information 😇.
Need the same feature. As a workarround for now, I just ignore the selection for rows that don't fall into the allowed criteria. However, from a UI perspective the user should know beforehand, that this is not possible. I believe an easy workaround would be a function which returns a component as an override for the predefined checkbox and pass the row params into it. I do the same already to highlight unselectable cells in a different format. So basically something along the line of:
<DataGrid
setCheckBoxRender={(rowParams, selectionModel)=>{
const necessaryInfos = rowHasNecessaryInfos(rowParams);
const isSelected = selectionModel.includes(rowParams.id);
return <Checkbox disabled={!necessaryInfos} checked={isSelected}/>
}
}
/>
We could add a prop isRowSelectable: (params)=> boolean as we did for isCellEditable, it seems like a pretty common usecase 🤔
Checkbox should be disabled, as well selection with keyboard, and click
In case this helps anyone else, if you want to "hide" the disabled checkboxes that this isRowSelectable selects, this sx worked well enough for my purposes.
const sx = {
'& .MuiDataGrid-row .MuiDataGrid-cellCheckbox .Mui-disabled ': {
// "hide" disabled checkboxes by making it translucent
color: 'rgba(0, 0, 0, 0.00)',
},
}
In case this helps anyone else, if you want to "hide" the disabled checkboxes that this
isRowSelectableselects, thissxworked well enough for my purposes.const sx = { '& .MuiDataGrid-row .MuiDataGrid-cellCheckbox .Mui-disabled ': { // "hide" disabled checkboxes by making it translucent color: 'rgba(0, 0, 0, 0.00)', }, }
This was very helpful for my case, although I used display: 'none' instead of color