This is a migrated thread and some comments may be shown as answers.

Reg. Save all changes with one request

1 Answer 37 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Aldo
Top achievements
Rank 1
Aldo asked on 17 Apr 2014, 04:49 PM
Hi,

I am trying ‘Save all changes with one request’ (http://www.telerik.com/support/code-library/save-all-changes-with-one-request) solution. I modified UpdateCreateDelete() method to return  modified data as response to the request. Now I want to make datasource and grid update
themselves smoothly without the additional roundtrip caused by calling read().

Please suggest how I may do this.

public ActionResult UpdateCreateDelete([DataSourceRequest] DataSourceRequest request,
            [Bind(Prefix = "updated")]List<Order> updatedOrders,
            [Bind(Prefix = "added")]List<Order> newOrders,
            [Bind(Prefix = "deleted")]List<Order> deletedOrders)
        {
            if (updatedOrders != null && updatedOrders.Count > 0)
            {
                for (int i = 0; i < updatedOrders.Count; i++)
                {
                    var target = orderList.Where(o => o.OrderID == updatedOrders[i].OrderID).FirstOrDefault();
  
                    if (target != null)
                    {
                        int targetIndex = orderList.IndexOf(target);
                        orderList[targetIndex].OrderDate = updatedOrders[i].OrderDate;
                        orderList[targetIndex].EmployeeID = updatedOrders[i].EmployeeID;
                        orderList[targetIndex].OrderDescription = updatedOrders[i].OrderDescription;
                    }
                }
            }
  
            if (newOrders != null && newOrders.Count > 0)
            {
                for (int i = 0; i < newOrders.Count; i++)
                {
                    newOrders[i].OrderID = orderList[orderList.Count - 1].OrderID + 1;
                    orderList.Add(newOrders[i]);
                }
                
            }
  
            if (deletedOrders != null && deletedOrders.Count > 0)
            {
                for (int i = 0; i < deletedOrders.Count; i++)
                {
                    var target = orderList.Where(o => o.OrderID == deletedOrders[i].OrderID).FirstOrDefault();
  
                    if (target != null)
                    {
                        orderList.Remove(target);
                    }
                }
            }
  
            List<Order> responseOrders = new List<Order>();
            responseOrders.AddRange(updatedOrders);
            responseOrders.AddRange(newOrders);
            responseOrders.AddRange(deletedOrders);
  
            return Json(responseOrders.ToDataSourceResult(request, ModelState));
  
            //return Json("Success!");
        }
  
<script>  
    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({ added: newRecords }));
  
        $.ajax({
            url: "/Home/UpdateCreateDelete",
            data: data,
            type: "POST",
            error: function() {
                //Handle the server errors using the approach from the previous example
            },
            success: function (result) {
  
                // How to make datasource and grid update themselves smoothly without the additional roundtrip caused by calling read()              
                 
            }
        });
    }
</script>

 

1 Answer, 1 is accepted

Sort by
0
Alexander Popov
Telerik team
answered on 21 Apr 2014, 01:50 PM
Hello Aldo,

I am afraid this behavior is not supported out of the box and implementing it would require a custom solution that is beyond the scope of our standard support services.

Regards,
Alexander Popov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Grid
Asked by
Aldo
Top achievements
Rank 1
Answers by
Alexander Popov
Telerik team
Share this question
or