DHTMLX Docs & Samples Explorer

Index
dhtmlxgrid

## Iterating through grid

### Iterator Types

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

- Iterating rows
  - Classic iterator;
  - Built-in iterator;
- Iterating cells
  - Classic cell iterator;
  - Built-in cell iterator.

In should be noted that there is no fundamental difference between two cell iteration approaches.

### Iterating Rows

#### Classic Iterator

This way of iteration has the following characteristics:

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

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

#### Built-in Iterator

This way of iteration has the following characteristics:

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

        <script>
            grid.forEachRow(function(id){ // function that gets id of the row as an incoming argument
                    // here id - id of the row
                 do_something_with_row(id);
            })
        </script>

### Iterating Cells

#### Classic Iterator

Classic cell iterator is the following:

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

#### Built-in Iterator

Built-in cell iterator has the following structure:

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

Method forEachCell() takes the following parameters:

- rowId - id of the row where the cell must be iterated;
- custom_code - function that gets eXcell object as an incoming argument.

### 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 Group ( 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