Looping through grid's dataitems

4 posts, 0 answers
  1. Paul
    Paul avatar
    6 posts
    Member since:
    Jun 2012

    Posted 29 Jun 2012 Link to this post

    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?
  2. Jerry T.
    Jerry T. avatar
    118 posts
    Member since:
    Jul 2012

    Posted 29 Jun 2012 Link to this post

    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
  3. Kendo UI is VS 2017 Ready
  4. Paul
    Paul avatar
    6 posts
    Member since:
    Jun 2012

    Posted 09 Jul 2012 Link to this post

    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?
  5. Jerry T.
    Jerry T. avatar
    118 posts
    Member since:
    Jul 2012

    Posted 10 Jul 2012 Link to this post

    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
Back to Top
Kendo UI is VS 2017 Ready