mui-x icon indicating copy to clipboard operation
mui-x copied to clipboard

[DataGrid] Enable/disable selection on certain rows with custom function

Open Neonin opened this issue 5 years ago • 9 comments

  • [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 avatar Apr 19 '21 06:04 Neonin

@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 avatar Apr 19 '21 23:04 oliviertassinari

@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 avatar Apr 20 '21 08:04 Neonin

@Neonin Ok, I see. Are you looking for something in this order?

Screenshot 2021-04-20 at 16 26 22

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 avatar Apr 20 '21 14:04 oliviertassinari

@oliviertassinari Yes 👌, I need to block a specific checkbox depending on the condition. Next time I will provide more information 😇.

Neonin avatar Apr 21 '21 09:04 Neonin

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}/>
       }
  }
/>

Belisarith avatar May 08 '21 09:05 Belisarith

We could add a prop isRowSelectable: (params)=> boolean as we did for isCellEditable, it seems like a pretty common usecase 🤔

dtassone avatar May 10 '21 09:05 dtassone

Checkbox should be disabled, as well selection with keyboard, and click

dtassone avatar May 11 '21 13:05 dtassone

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)', 
  },
}

snekse avatar Jun 22 '23 16:06 snekse

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)', 
  },
}

This was very helpful for my case, although I used display: 'none' instead of color

matthew-h-wang avatar Nov 17 '23 13:11 matthew-h-wang