When using a local dataSource with the Kendo UI Grid component, the cancelRow() method causes an issue in certain cases. If the Grid has a dataSource that contains 1 item, for example, and you then add 1 more item to the dataSource by using the DataSource.add() method or replacing it with a new data source using the DataSource.data() method, then edit a row and execute the cancelRow() method, the grid behaves as if none of the new items exist – any items that were part of the original dataSource remain in the grid after cancelRow(), but any other items are removed entirely. After digging a bit into the dataSource object, it seems that the _pristine array is not updated when adding items or replacing the root data array, and the _pristine array may be what is used during the cancelRow() process. Here’s a working example illustrating the issue:
$("#grid").kendoGrid({
editable: "inline",
columns: [
{
field: null,
title: "",
template: "<a href='\\#' class='edit'>Edit</a>, <a href='\\#' class='cancel'>Cancel</a>, <a href='\\#' class='update'>Update</a>"
},
{
field: "name",
title: "Name"
},
{
field: "other",
title: "Other"
}
],
dataSource: [
{
name: "name1",
other: "other1"
}
],
save: function() {
this.refresh();
},
dataBound: function() {
var $grid = $("#grid");
var grid = $grid.data("kendoGrid");
$grid.find("a.edit").click(function (e) {
e.preventDefault();
var index = $(this).closest("tr").index();
grid.editRow(grid.tbody.find(">tr:eq(" + index + ")"));
grid.tbody.find(">tr:eq(" + index + ") td:eq(1) input").focus();
});
$grid.find("a.cancel").click(function (e) {
e.preventDefault();
grid.cancelRow();
});
$grid.find("a.update").click(function (e) {
e.preventDefault();
grid.saveRow();
});
}
});
var $grid = $("#grid");
var grid = $grid.data("kendoGrid");
//fails with initial empty datasource
$("#grid").data("kendoGrid").dataSource.add(
{
name: "name2",
other: "other2"
}
);