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

Update selected rows column throws Cannot read property 'set' of undefined

2 Answers 2071 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nong
Top achievements
Rank 1
Nong asked on 15 Apr 2021, 11:53 PM

When running the code below, the first selected row updates but the next one throws a "Cannot read property 'set' of undefined".

I have the datasource batch set to true and am using dataItem for each selected row and setting the value from there.

var grid = $("#grid").data("kendoGrid");
var selectedRows = grid.select();
if (selectedRows.length > 0 && unit != "") {
    $.each(selectedRows, function (i, row) {
        var data = grid.dataItem(row);
        data.set('UnitsInStock', unit);
    });
    grid.saveChanges();
}

 

Here is a dojo sample of it http://dojo.telerik.com/aFIwuYUs

2 Answers, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 20 Apr 2021, 02:33 PM

Hello,

The issue occurs because when you use the set method to change a field's value, the Grid is being refreshed and the selection is lost. There are several ways to change the example to behave as expected.

1. You can use the getByUid method of the dataSource instead of the dataItem method to get the corresponding model. Here is an example for reference.

2. You can use the code below to change the values of the Grid.

$("#get").click(function(){
        var unit = $("#unit").val();
        var grid = $("#grid").data("kendoGrid");
        var selectedRows = grid.select();
        if (selectedRows.length > 0 && unit != "") {
          for(let row of selectedRows){
            var data = grid.dataItem(row);
            data.UnitsInStock=unit;
          }
          grid.saveChanges();

        }
      })

Using the highlighted row will change the value of the field without refreshing the Grid. Then, after the loop is over, the saveChanges will refresh the Grid so that the updated values will be shown. Here is another example to demonstrate.

Let me know if you have any further questions.

Regards,
Martin
Progress Telerik

Тhe web is about to get a bit better! 

The Progress Hack-For-Good Challenge has started. Learn how to enter and make the web a worthier place: https://progress-worthyweb.devpost.com.

0
Nong
Top achievements
Rank 1
answered on 20 Apr 2021, 08:22 PM

Thanks that worked.  I also added data.dirty = true so the saveChanges can trigger the update.

 

$("#get").click(function(){
        var unit = $("#unit").val();
        var grid = $("#grid").data("kendoGrid");
        var selectedRows = grid.select();
        if (selectedRows.length > 0 && unit != "") {
          for(let row of selectedRows){
            var data = grid.dataItem(row);
            data.UnitsInStock=unit;
            data.dirty = true;
          }
          grid.saveChanges();
 
        }
      })
Tags
Grid
Asked by
Nong
Top achievements
Rank 1
Answers by
Martin
Telerik team
Nong
Top achievements
Rank 1
Share this question
or