17

I'm using the edit event on a Kendo grid to show a couple of hidden columns. I'll then hide them again on the save event.

Problem I have is that there doesn't appear to be an event for cancelling edit mode, so the column get screwed up if the user clicks cancel.

Is there an undocumented event for cancel or do I need to find a workaround?

Mat
  • 1,668
  • 4
  • 23
  • 40

4 Answers4

20

Basically there is no such "Cancel" event, however you can attach click event on the "Cancel" button in the еdit event of the Grid. Please check the example below:

function onEdit(e) {
   e.container.find(".k-grid-cancel").bind("click", function () {
      //your code here
   })
 }

EDIT: From some time the Grid have "cancel" event which can be used instead of the above solution:

Vladimir Iliev
  • 1,871
  • 16
  • 26
  • This almost worked for us, however the class was `.k-grid-cancel-changes` rather than `.k-grid-cancel`. – RTPeat Jul 18 '18 at 16:26
18

I've been looking for an answer to the same question but this didn't work for me. I had a scenario where new and edited records within my grid are validated within my controller and error messages are added to the ModelState's ModelError collection. I had hooked up the grid's datasource error event which then displayed the error message within an alert, and then added the following which reset the changes:

$('#MyGrid').data("kendoGrid").cancelChanges();

It was a neat solution for me because I am using paging and the current page the user is viewing is preserved.

MarkSci
  • 181
  • 1
  • 3
  • 1
    In the error event you can also just use the this keyword to cancelChanges ie. this.CancelChanges() instead of specifying the whole path. – Richard Edwards Oct 17 '14 at 13:53
  • is it posible to use cancelChanges() for particular column? – ManirajSS Jun 11 '15 at 13:53
  • I noticed `cancelChanges()` was making all the items in my list duplicate (until I refreshed the page). This stopped happening when I added the specific item that was being added/edited to the method: `cancelChanges(e.model)` – DrCJones Nov 20 '18 at 19:52
10

Contrary to what the accepted answer states there is in fact a cancel event just like the edit event.

$("#grid").kendoGrid({
    ...
    edit: function(e) {
        alert("edit")
    },
    cancel: function(e) {
        alert("cancel");
    },
    ...
});
Spaceman Spiff
  • 934
  • 1
  • 14
  • 32
  • 3
    If you are using "incell" edit mode, the cancel event does not trigger. The documentation says that it only fires for "inline" or "popup" edit modes. – MarkMcDowell Dec 08 '16 at 14:06
0

Try this,

$("#grid").kendoGrid({
 columns: [
  { field: "name" },
  { field: "age" }
 ],
 dataSource: [
  { name: "Jane Doe", age: 30 },
  { name: "John Doe", age: 33 }
 ],
 dataBound: function(e) {
  $("#grid").on("mousedown", ".k-grid-cancel-changes", function (e) {
    //custom logic
  });
 }
});

In dataBound, wire click event for the kendo grid Toolbar cancel button. It will work.

Joee
  • 1,834
  • 18
  • 19