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

Update Data Method being called too early

4 Answers 41 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dan
Top achievements
Rank 1
Veteran
Dan asked on 07 Jan 2021, 02:53 PM

I have a grid that has the following code as the update action in its datasource.

This is being called before the grid is rendered so I get an error that the datasource is null. Meaning I dont think the grid has initialized on the front end so the '#Grid' isnt there for the returnDirtyFields method to get. Why is this update action data method being run when the grid is not updating?

The .Data() method I want with the update action below it.

function returnDirtyFields(gridId) {
      var id = '#' + gridId;
      var dataSource;
      var data;
      var changedModels;
 
      dataSource = $(id).data("kendoGrid").dataSource;
      data = dataSource.data();
 
          if (dataSource.hasChanges()) {
          for (var i = 0; i < data.length; i++) {
 
              if (data[i].dirty) {
                  changedModels = data[i].dirtyFields
              }
              }
           }
 
      var temp = $.extend(true, {}, { dirtyFields: JSON.stringify(changedModels) });
      return temp;
  }

 

.Update(update => update.Action("Entry_Update","Entry").Data("returnDirtyFields('Grid')"))

4 Answers, 1 is accepted

Sort by
0
Anton Mironov
Telerik team
answered on 12 Jan 2021, 10:28 AM

Hi Dan,

Thank you for the provided code snippets and details.

You are totally correct. The reason for the faulty behavior is that when the Kendo UI Grid is initializing, the Update method in the DataSource is finding the "returnDirtyFields" function. This function is IIFE(Immediately Invoked Function Expression) and executes at the same moment. The DataSource is still not available at this moment and an exception is thrown.

In this case, I would recommend using Batch editing. This functionality provides the opportunity to keep all the changes(dirty fields) at the client(in the browser) and send them to the Update method when you decide.

Here is a demo that represents the implementation and behavior of the Batch editing:

Give the approach above a try and let me know if further assistance is needed.

 

Kind Regards,
Anton Mironov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Dan
Top achievements
Rank 1
Veteran
answered on 13 Jan 2021, 02:05 PM

Anton,

Thank you for the response. I did not try to implement batch editing. Instead I decided to opt for logging (which is what my question context was) the whole object instead of just dirty values. Thank you for the response anyway!

0
Anton Mironov
Telerik team
answered on 13 Jan 2021, 02:12 PM

Hi Dan,

The solution you had chosen is a good one. Is this achieved all the needed requirements for the application??

Let me know if I could assist you with anything else, or this thread should be closed.

 

Best Regards,
Anton Mironov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Dan
Top achievements
Rank 1
Veteran
answered on 13 Jan 2021, 02:14 PM

Anton,

Thank you. Yes you can close this thread. 

Tags
Grid
Asked by
Dan
Top achievements
Rank 1
Veteran
Answers by
Anton Mironov
Telerik team
Dan
Top achievements
Rank 1
Veteran
Share this question
or