Kendo grid batch editing - making a single call to save

匿名 (未验证) 提交于 2019-12-03 01:48:02

问题:

With Kendo grid batch editing turned on, I know that you can hook into the create, update and destroy commands where Kendo will send 3 separate commands to the server when you click on Save Changes.

I was wondering if there was any way to send all three sets of updates as a single call to the server -like a transaction. Or even send each in a specified order, with a check for success before sending the next .

The only way I could come up with was to have a custom Save Changes implementation which ,when invoked, would lookup the grid datasource to find out all rows that have been added (isNew() for added rows), deleted (_destroyed for deleted rows), updated (isDirty for updated rows) and then craft my own call to a server endpoint using ajax using the identified datasets.

回答1:

Telerik posted a work-around in their code library recently: http://www.kendoui.com/code-library/mvc/grid/save-all-changes-with-one-request.aspx. Unfortunately the work-around is rather bare-bones. It gives a good example of how to capture destroyed, dirty, and new records but finishes with some hand waving to handle any errors in the response and synchronizing the data source on success. Also note that there is no check to ensure there are destroyed, dirty, or new records before making the ajax request.

Here is the relevant code. Download the full example from the link above to see how the grid is setup and to ensure you have the latest version.

function sendData() {     var grid = $("#Grid").data("kendoGrid"),         parameterMap = grid.dataSource.transport.parameterMap;      //get the new and the updated records     var currentData = grid.dataSource.data();     var updatedRecords = [];     var newRecords = [];      for (var i = 0; i < currentData.length; i++) {         if (currentData[i].isNew()) {             //this record is new             newRecords.push(currentData[i].toJSON());         } else if(currentData[i].dirty) {                      updatedRecords.push(currentData[i].toJSON());         }     }      //this records are deleted     var deletedRecords = [];     for (var i = 0; i < grid.dataSource._destroyed.length; i++) {         deletedRecords.push(grid.dataSource._destroyed[i].toJSON());     }      var data = {};     $.extend(data, parameterMap({ updated: updatedRecords }), parameterMap({ deleted: deletedRecords }), parameterMap({ new: newRecords }));      $.ajax({         url: "/Home/UpdateCreateDelete",         data: data,         type: "POST",         error: function () {             //Handle the server errors using the approach from the previous example         },         success: function () {             alert("update on server is completed");              grid.dataSource._destroyed = [];             //refresh the grid - optional             grid.dataSource.read();         }     }) }


回答2:

Maybe you can enable the batch property of the Datasource

batch Boolean(default: false)

If set to true the data source will batch CRUD operation requests. For example updating two data items would cause one HTTP request instead of two. By default the data source makes a HTTP request for every CRUD operation.

Source : Datasource API



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!