1
votes

My requirement is to loop through all selected rows of a grid and set a field on the datasource with a given value. I have the following code and iterate through selected rows of a kendo ui grid.

$('#grid').data("kendoGrid").select().each(function () {
                if($('#grid').data().kendoGrid.dataSource.data()[$(this).index()] != null ){

                    var myItem = $('#grid').data().kendoGrid.dataSource.data()[$(this).index()];
                    myItem.set(myFiled, myValue);
                    leg.push(myItem);

                }
            });

The problem is that after my code reaches on the line myItem.set(myFiled, myValue); the index gets the value -1 and it keeps this value even on my next iteration.

When removing the .set method i see that i do not have the same behaviour and my index keeps the correct value. Why is is this happening ?

Furthermore, what is the difference between the line:

i) $('#grid').data("kendoGrid").dataItem($(this)).set(myField, myValue);

ii) $('#grid').data().kendoGrid.dataSource.data()[$(this).index()].set(myField, myValue);

I know that the first has to do with the values on the grid. Unfortunately i never managed to set the values as shown on (i) . The code shown on line (i) does not seem to work with the set function at all.

I took my examples from here Refresh a single Kendo grid row and Kendo-UI grid Set Value in grid with Javascript for the cases (i) and (ii) .

1
Remember that you cannot remove / insert elements in an array / object that you use for iterating in a jQuery.each. Depending on you DataSource definition, I think that you can alter the DataSource when doing the sets.OnaBai
Thank you for the reply. Well i do not intend to insert/remove . As i said setting value in the datasource changes the index value to -1 . That is my main issue at the moment.Stephan
I have noticed also that once the code reaches the .set method, it loses the selection on the gridStephan

1 Answers

1
votes

I found a solution/workaround. Although i still do not understand why a kendo grid index has this behaviour.

Instead of using inside my iteration the myItem.set(myField, myValue); i changed my code to myItem[myField] = myValue;

With this method the value was changed, the change event was not triggered (as required) and the index was keeping it's value as also required.