How do I conditionally disable a checkbox in grid?

1 Answer 229 Views
Checkbox Grid
SK
Top achievements
Rank 2
Iron
Iron
SK asked on 04 Aug 2021, 02:54 PM

I have a checkbox in a grid that I want to conditionally disable. What I have below technically hides the checkbox (haven't figured out how to conditionally 'disable') but you can still click in the box and the checkbox will appear even if it does not meet the condition:

@(Html.Kendo().Grid<SomeUI.Areas.UVL.Models.SomePOModel> ().Name("SomePOgrid") .Columns(columns => { columns.Bound(p => p.forceClose).Title("Force Close") .ClientTemplate("#if(status == 'Pending'){# <input id='forceClose' value='#=forceClose#' class='k-checkbox' type='checkbox' checked='checked'/> #}#") .Width(170).HtmlAttributes(new { style = "text-align:center;" });

})

Wondering if you have a better or solution or if someone can explain what I am missing. Thanks.

1 Answer, 1 is accepted

Sort by
1
Accepted
Ivan Danchev
Telerik team
answered on 09 Aug 2021, 11:11 AM

Hello Sabri,

Assuming you use InCell editing, clicking on the cell will put it in edit mode. Then the checkbox that appears comes from the cell being in edit mode, so it is not related to the logic you have in the ClientTemplate.

You can the cell from going into edit mode by setting the column's Editable configuration. It is used to pass a function:

columns.Bound(p => p.forceClose).Title("Force Close").Editable("columnEditable")
                                .ClientTemplate("#if(status == 'Pending'){# <input id='forceClose' value='#=forceClose#' class='k-checkbox' type='checkbox' checked='checked'/> #}#")
                                .Width(170).HtmlAttributes(new { style = "text-align:center;" });

and in the function you can have your custom logic, e.g.,

function columnEditable(e) {
	if (!e.forceClose) {
		return false;
	}
}

This allows you to prevent editing of the forceClose field, based on its value, or another field value of the record (the record the user attempts to edit is accessed through e), or some other custom conditional logic.

As demonstrated above, editing for forceClose field will be disabled by returning false, if the value of forceClose is false.

Regards,
Ivan Danchev
Progress 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/.

SK
Top achievements
Rank 2
Iron
Iron
commented on 09 Aug 2021, 12:39 PM

I implemented your solution and it worked flawlessly. Thank you for explaining why it was happening and how to work around it! 
Tags
Checkbox Grid
Asked by
SK
Top achievements
Rank 2
Iron
Iron
Answers by
Ivan Danchev
Telerik team
Share this question
or