To implement various validation rules or business logic, it could be important to know which row in the grid user is currently editing. The available functions in grid are helpful but can be tweaked to get the behavior you may need during validation.
Here is a custom implementation of getting current row in the grid. Note that for this to work correctly, you need to have navigatable set to true and/or the selectable property set to {“single”, “multiple”, true}.
//returns current row element
kendo.ui.Grid.fn.currentRow = function() {
//this will only work if grid is navigatable
var cell = this.current();
if (cell) {
return cell.closest('tr')[0];
}
//following will only work if grid is selectable, it will get the 1st row only for multiple selection
if (this.options.selectable != false)
return this.select()[0];
return null;
}
Configure the grid as following:
var grid = $("#grid").kendoGrid({
…
selectable: true, //either one or
navigatable: true //this should be true
…
});
//How to use currentRow function
//in change or edit events, get the grid object and directly call
// .currentRow() for the grid object. It will return null if
// the selectable and navigatable both are false. Otherwise
// you will get the row element. Calling grid.dataItem(row) gives
// the data item associated with current row
change: function(e)
{
var grid = this;
var row = grid.currentRow();
if(row)
console.log(grid.dataItem(row));
},
edit: function(e)
{
var grid = this;
var row = grid.currentRow();
if(row)
console.log(grid.dataItem(row));
}
See a live working example on dojo here - http://dojo.telerik.com/IwoDA/2