Is there a way to cancel the changes of one field in one item in the DataSource?
Let us say that the user is modifying in the grid one item and he wants to cancel the changes of this item (one specific field) and not all his changes in the grid, can this be done?
6 Answers, 1 is accepted
The required functionality is not available out of the box, however, you may be able to implement it with some custom coding and utilising the save event of the Kendo UI Grid as well as a custom column editor. Below are the steps I took to make this implementation possible:
- create the required input field with a cancel button by adding the columns editor setting:
- add a boolean value whether the button was clicked via mousedown
- attach an event handler function to the save event of the Kendo UI Grid and call the preventDefault() method if the cancel button mousedown event call evaluates to true
- remember to change the boolean value within the handler function
For your convenience, a runnable demo is available at:
Telerik by Progress
Not to revive a dead thread but I would be copying the same title, I can create a new post if that is preferred - the dojo example linked here can cancel the changes of the currently entered text, but can't cancel a cell that has already been changed and has the dirty flag. This is easily seen in that example if you change the value and press enter then open up the editor again and click cancel.
Is the functionality I described possible? It seems that cancelling changes on a single cell basis should be common enough to be supported.
The desired functionality is still not available out of the box but there are other ways to achieve the desired behaviour.
For example, we can call on the grid cancelRow() method which cancels the changes of the currently edited row. (the entire model)
Since the requirement seems to be to cancel only a certain field, we could clone the current model, cancel the row and then set the old values back to it, except the one which should remain cancelled:
Here is the runnable example:
*if you need to support IE, you would need to use an alternative for Object.assign().
Alternatively, the pristine data items are kept in an internal array that may be used. It is contained in the _pristineData data source property.
Finally, if this functionality is important to you and you feel that the Kendo UI DataSource should offer it out of the box, please log a Feature request in our portal:
The popular items get selected for future implementation.
Thank you for the quick reply! That was exactly what I needed. For my case we did not want to have an edit button cluttering the grid so I used a context menu and had to grab the values differently due to that. My grid is also dynamic so grabbing the field names required looking at the column index. I've pasted my code below in case someone else has the same needs.
Thank you for clarifying the UI, it is something that has been missing from the scenario.
Based on this new knowledge and the provided code, I have a couple of recommendations:
1) Since the grid is not in edit mode, you may use the grid data source cancelChanges method instead of having to put the cell in edit mode:
2) Use the public API to determine the cell index as it should work in all scenarios (reorderable, hidden columns, etc.)
For this to work with the current implementation the filter of the context menu should be modified to use the cell, let me know if this causes any issues. Here is the logic on the select event of the context menu:
Thank you for your response, this is a much cleaner approach - I'm definitely still learning the ins and outs of kendo. I've implemented this change and it's working great!