is there a way to disable the checkbox in the datagrid component?

1 Answer 119 Views
Checkbox Grid
Wreeecks
Top achievements
Rank 2
Iron
Iron
Iron
Wreeecks asked on 05 May 2022, 06:38 AM

Hi,

I wanted to disable the checkbox based on it's value. Is there a proper way of disabling it?

I'm using the sample code on this page https://www.telerik.com/kendo-react-ui/components/grid/selection/#toc-customizing-the-selection

Thanks!

1 Answer, 1 is accepted

Sort by
0
Filip
Telerik team
answered on 08 May 2022, 04:18 PM

Hello, Rex,

In case you want to disable the checkbox in the header that can be achieved by setting the checkboxElement to disabled. I updated the example here:

https://stackblitz.com/edit/react-t1stu3?file=app/main.jsx


The relevant logic can be found inside the onHeaderSelectionChange handler function.

I hope this helps.

Regards, FilipProgress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Wreeecks
Top achievements
Rank 2
Iron
Iron
Iron
commented on 08 May 2022, 11:35 PM

Thank you for your reply, Filip.

What i wanted to do is disable the checkboxes on load or after the grid has been rendered. Is there any event listener or hook for me to inject this logic?  

Wreeecks
Top achievements
Rank 2
Iron
Iron
Iron
commented on 09 May 2022, 12:49 AM | edited

I tried using column's cell prop to customize the cell content. The code below works but the "checkbox selection" doesn't work. When the header checkbox is clicked it, the row's checkboxes are not selected.

Is there a way to return the default `<td>` element,  and just disable the checkbox in it? Thanks

const CustomCell = (props: GridCellProps) => {
      console.log(props);

      return (
        <td role="gridcell" >
          <input type="checkbox" className="k-checkbox" value={props.dataItem.memberId} />
            <label className="k-checkbox-label"></label>
        </td>
      )
    }

// Grid Component
<Grid>
<Column 
cell={CustomCell} 
...
/>

</Grid>


Filip
Telerik team
commented on 10 May 2022, 07:39 PM

Hi, Rex,

Using the cell property will allow the developer to render the default td with a custom input that can be disabled, as you have found out. 

In addition to that in order for the rendered checkboxes to be selected a custom selection column has to be made and the rendered checkbox has to be based on the item value. Here is an example that showcases this approach:

https://stackblitz.com/edit/react-igh2gv-1b7bg7?file=app/main.jsx

 

In case this is not enough for you to transfer to your real-time application, can you provide a runnable example that reproduces your behavior so that I can inspect further?

I hope this helps.

Regards,Filip

Wreeecks
Top achievements
Rank 2
Iron
Iron
Iron
commented on 16 May 2022, 12:47 AM

I was able to simplify the implementation by using the checkbox component. 

The code below checks if the data is available, in this case it's email. If the email is not available, it disables the the checkbox. 

The only issue with this approach is that the header checkbox will not be ticked if there's one record that doesn't have email. 


    const CustomCell = (props: GridCellProps) => {
      const field = props.field || "";
      const value = props.dataItem[field];
      const navigationAttributes = useTableKeyboardNavigation(props.id);
      
      const hasEmail = (props.dataItem.email && props.dataItem.email !== '')
      return (
        <td
          colSpan={props.colSpan}
          role={"gridcell"}
          aria-colindex={props.ariaColumnIndex}
          aria-selected={props.isSelected}
          {...{
            [GRID_COL_INDEX_ATTRIBUTE]: props.columnIndex,
          }}
          {...navigationAttributes}
        >
            
          <Checkbox value={(hasEmail) ? value : null} disabled={!hasEmail} />
        </td>
      
      )
    }

Filip
Telerik team
commented on 17 May 2022, 04:51 PM

Hello, Rex,

If the only issue is that the header checkbox is not ticked on render, I can advise using the checked property to mark it as checked.

I hope this helps.

Tags
Checkbox Grid
Asked by
Wreeecks
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Filip
Telerik team
Share this question
or