Looping through grid's dataitems

1 Answer 6196 Views
Grid
Paul
Top achievements
Rank 1
Paul asked on 29 Jun 2012, 02:19 PM
I'm trying to loop through the dataitems held by my grid but get an error that the field I am  trying to look at in every data item is undefined. This is the code I'm using:

grid = $("#grid").data("kendoGrid");
 
for (var i = 0; i < 50; i ++) {
    if ( grid.dataItem(i).FirstName == reference) {
        var item = grid.dataItem(i);
        break;
    }
}

What I'm trying to do is get the data item whose FirstName field matches the reference variable. Does anyone have any ideas?
Jerry T.
Top achievements
Rank 1
commented on 29 Jun 2012, 06:05 PM

Paul,

You need to pass a row, not a rowindex, to dataItem.

You could try something like this, though:

$("#grid").data("kendoGrid")._data[i].FirstName


Or, you could define a custom data attribute in a template for that cell.
<td data-first-name="# FirstName #">
    #= FirstName #
</td>

And, in your code, look for a matching value like this:
var rowindex;
var cell = $("#grid").data("kendoGrid").tbody.find("td[data-first-name='" + reference + "']");
 
if (cell) {
    rowindex = cell.parent()[0].rowIndex;
}


and that will give you the rowindex of the grid where the match was found.

Jerry
Paul
Top achievements
Rank 1
commented on 09 Jul 2012, 09:21 AM

Thanks for the reply Jerry. I didn't fully explain the problem when I posted this. The code you've posted is great for finding the row index of the row I want, however once I've done this I want to pass the dataitem for that row into a template for a window. How would I go about getting the data item from the index?

1 Answer, 1 is accepted

Sort by
0
Jerry T.
Top achievements
Rank 1
answered on 10 Jul 2012, 01:09 PM
Paul,

Try something like this:

var grid = $("#grid").data("kendoGrid");
var rowindex = null;
var cell = grid.tbody.find("td[data-first-name='" + reference + "']");
var dataRow = null;
  
if (cell) {
    rowindex = cell.parent()[0].rowIndex;
}
 
if (rowindex != null) {
    dataRow = grid.dataItem(grid.tbody.find("tr").eq(rowindex));
}
 
var someField = dataRow.someField;


Also, how is your datasource defined?

Jerry
Tags
Grid
Asked by
Paul
Top achievements
Rank 1
Answers by
Jerry T.
Top achievements
Rank 1
Share this question
or