Iterating Over Data

In this chapter you'll find out how to implement iterators for dhtmlxGrid.

Iterating over rows

Standard for loop

The stated below technique isn't used in the smart rendering mode.

for (var i=0; i<mygrid.getRowsNum(); i++){
    mygrid.CellByIndex(i,0).setValue(i); // i - the index of a row in the grid
};
  • The loop variable i is the row index (don't confuse with the row id). Numbering is zero-based;
  • The order of iteration is the order of rows in the grid;
  • In the paging mode only the current page is iterated;
  • If you apply filtering to the grid, only the rows that meet filtering criteria will be iterated;

Method forEachRow()

The method forEachRow iterates over all rows in the grid.

mygrid.forEachRow(function(id) {
    mygrid.CellById(row_id,0).setValue(id); //id - the row id
});
  • The loop variable is the row id;
  • Basically, the order of iteration is the order in which rows were added to the grid, but not necessarily;
  • In the paging or smart rendering mode all the already parsed rows will be iterated;
  • If you apply filtering to the grid, all the rows will be iterated, no matter if they meet filtering criteria or not;

Related sample:  Multiline cells

The forEachRowInGroup() method for the grouped grid

When you apply grouping to the grid, you can use the forEachRowInGroup method to provide iteration over rows of a specific group.

mygrid.forEachRowInGroup(name,function(id){
    mygrid.CellById(row_id,col_ind).setValue(id); //id - the row id
});

where name is the key value of the group.

Iterating over cells

Standard for loop

The stated below technique isn't used in the smart rendering mode.

for (var i=0; i<mygrid.getColumnCount(); i++){
    alert(mygrid.CellById(row_id,col_ind).getValue()); //i-index of a column (zero-based numbering)
}
  • The loop variable is the column id;
  • The order of iteration is the order of columns in the grid;

Method forEachCell()

The method forEachCell iterates over all cells of the specified row.

mygrid.forEachCell(row_id,function(cellObj,col_index){
    cellObj.setValue(col_index)
);
  • The loop variable is the column id;
  • The order of iteration is the order of columns in the grid;
Back to top