Let's say I have a data like this:
[
{ID: 1, SomeForeignKeyID: 4, IsFkEnabled: true},
{ID: 2, SomeForeignKeyID: 9, IsFkEnabled: false}
]
Kendo Grid is using this data:
columns.Bound(m => m.ID);
columns.ForeignKey(p => p.SomeForeignKeyID, ViewBag.ForeignKeys as IEnumerable<object>, "Value", "Name");
Here's the problem: how to make ForeignKey column editable, but only in rows, where IsFkEnabled == true? Edit mode is InCell.
The simplest way is to use the dataBound event to conditionally apply one of the special CSS classes to the cells that the grid ignore for editing:
http://dojo.telerik.com/izOka
Please try with the below code snippet.
VIEW
MODEL
CONTROLLER
If you want to download demo then click here.
Another approach is to use your own "editor" function for column definition that provides either an input element or a plain div depending on your condition.
Notes:
Approach #1
Use the grid's edit event and then do something like this:
Demo here (updated for Q1 2014)
To use this via the MVC fluent syntax, simply give the anonymous
edit
function above a name (e.g.onEdit
):and reference it like this:
The disadvantage to this is that the editor gets created first before the edit event is triggered, which can sometimes have undesirable visual effects.
Approach #2
Extend the grid by overriding its
editCell
method with a variation that triggers abeforeEdit
event; for that to work with grid options, you'll also need to override the init method:then use it similar to #1:
The difference of this approach is that the editor won't get created (and focused) first. The
beforeEdit
method is using the sameisEditable
method from #1. See a demo for this approach here.If you want to use this approach with MVC wrappers but don't want / can't extend GridEventBuilder, you can still bind your event handler in JavaScript (place below the grid MVC initializer):
None of these approaches worked for me. A very simple implentation looks like this
Where edit is part of the kendo grid declaration and Name is the actual name of the field.