| DHTMLX Documentation

## Iterating through grid

The base element of grid is a row, so one of most common tasks - iteration through grid setting|getting row specific data.
There are two ways to iterate rows in the grid:

- Classic iterator;
- Built-in iterator.

### Classic iterator

> for (var i=0; i<grid.getRowsCount(); i++){
> // here i - index of row in grid
> do_something_with_row(index);
> }

This way of iteration has the following characteristics:

1. Used parameter - index (not ID), zero based position of a row in the grid;
2. The order of iteration equals the order of elements in the grid;
3. In case of paging mode - only current page is iterated;
4. In case of tree grid - only currently visible rows are iterated;
5. In case of smart rendering mode - not usable;
6. In case of applied filtering - only rows that accept filtering rules will be included in iteration.

### Built-in iterator

> grid.forEachRow(function(id){
> // here id - row ID
> do_something_with_row(id);
> })

This way of iteration has the following characteristics:

1. Used parameter - row ID;
2. Order of iteration is not equal to the current rows order in grid (basically it is equal to the adding order of rows to the grid, but it can be inconsistent);
3. In case of paging or smart rendering mode - all rows parsed at the current moment will be affected;
4. In case of tree grid - all rows will be processed, including those in closed branches;
5. In case of applied filtering - all rows in the grid (including filtered out ones) will be included in iteration.

The cells iteration is more simple task, but it also has two possible ways:

- Classic cell iteration;
- Built-in cell iteration.

### Classic cell iteration

> for (var i=0; i<grid.getColumnCount(); i++){
> // i - column index
> alert(grid.cells(id,i).getValue());
> }

### Built-in cell iteration

> grid.forEachCell(id,function(c){
> alert(c.getValue());
> });

There is no fundamental difference between two cell iteration approaches.

## Iteration through TreeGrid

You can loop from all child-rows of some row in treegrid as
grid._h2.forEachChild(parent_id,function(element){
//element.id - id of child row
//element.parent.id - parent id
do_something_with_row(element.id);
});
where parent_id - id of rows, against which functionality will be executed.

## Iteration through rows in grop ( grid after groupBy )

You can loop through all rows in some group by
mygrid.forEachRowInGroup(name,function(id){
do_something_with_row(id);
});
where name - key-value of group
© DHTMLX, 2008