# TableWalker

class

The table iterator class. It allows to iterate over table cells. For each cell the iterator yields [`TableSlot`](module_table_tablewalker-TableSlot.md) with proper table cell attributes.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/tablewalker.ts#L64)

<a id="properties">

## Properties

<a id="member-_cellIndex">

### `_cellIndex: number` _(internal)_

The cell index in a parent row. For spanned cells when [`_includeAllSlots`](#member-_includeAllSlots) is set to `true`, this represents the index of the next table cell.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/tablewalker.ts#L129)

<a id="member-_column">

### `_column: number` _(internal)_

The current column index.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/tablewalker.ts#L121)

<a id="member-_row">

### `_row: number` _(internal)_

The current row index.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/tablewalker.ts#L107)

<a id="member-_rowIndex">

### `_rowIndex: number` _(internal)_

The index of the current row element in the table.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/tablewalker.ts#L114)

<a id="member-_table">

### `_table: ModelElement` _(internal)_

The walker's table element.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/tablewalker.ts#L70)

<a id="member-_endColumn">

### `_endColumn?: number` _(private)_

If set, the table walker will only output cells up to a given column.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/tablewalker.ts#L90)

<a id="member-_endRow">

### `_endRow?: number | null` _(private)_

A row index at which this iterator will end.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/tablewalker.ts#L80)

<a id="member-_includeAllSlots">

### `_includeAllSlots: boolean` _(private)_

Enables output of spanned cells that are normally not yielded.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/tablewalker.ts#L95)

<a id="member-_jumpedToStartRow">

### `_jumpedToStartRow: boolean` _(private)_

Indicates whether the iterator jumped to (or close to) the start row, ignoring rows that don't need to be traversed.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/tablewalker.ts#L144)

<a id="member-_nextCellAtColumn">

### `_nextCellAtColumn: number` _(private)_

Index of the next column where a cell is anchored.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/tablewalker.ts#L139)

<a id="member-_skipRows">

### `_skipRows: Set<number>` _(private)_

Row indexes to skip from the iteration.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/tablewalker.ts#L100)

<a id="member-_spannedCells">

### `_spannedCells: Map<number, Map<number, CellData>>` _(private)_

Holds a map of spanned cells in a table.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/tablewalker.ts#L134)

<a id="member-_startColumn">

### `_startColumn: number` _(private)_

If set, the table walker will only output cells from a given column and following ones or cells that overlap them.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/tablewalker.ts#L85)

<a id="member-_startRow">

### `_startRow: number | null` _(private)_

A row index from which this iterator will start.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/tablewalker.ts#L75)

<a id="methods">

## Methods

<a id="function-constructor">

### `constructor( table, options )`

Creates an instance of the table walker.

The table walker iterates internally by traversing the table from row index = 0 and column index = 0. It walks row by row and column by column in order to output values defined in the constructor. By default it will output only the locations that are occupied by a cell. To include also spanned rows and columns, pass the `includeAllSlots` option to the constructor.

The most important values of the iterator are column and row indexes of a cell.

See [`TableSlot`](module_table_tablewalker-TableSlot.md) what values are returned by the table walker.

To iterate over a given row:

```typescript
const tableWalker = new TableWalker( table, { startRow: 1, endRow: 2 } );

for ( const tableSlot of tableWalker ) {
  console.log( 'A cell at row', tableSlot.row, 'and column', tableSlot.column );
}
```

For instance the code above for the following table:

+----+----+----+----+----+----+ | 00 | 02 | 03 | 04 | 05 | | +----+----+----+----+ | | 12 | 14 | 15 | | +----+----+----+ + | | 22 | | |----+----+----+----+----+ + | 30 | 31 | 32 | 33 | 34 | | +----+----+----+----+----+----+

will log in the console:

'A cell at row 1 and column 2' 'A cell at row 1 and column 4' 'A cell at row 1 and column 5' 'A cell at row 2 and column 2'

To also iterate over spanned cells:

```typescript
const tableWalker = new TableWalker( table, { row: 1, includeAllSlots: true } );

for ( const tableSlot of tableWalker ) {
  console.log( 'Slot at', tableSlot.row, 'x', tableSlot.column, ':', tableSlot.isAnchor ? 'is anchored' : 'is spanned' );
}
```

will log in the console for the table from the previous example:

'Cell at 1 x 0 : is spanned' 'Cell at 1 x 1 : is spanned' 'Cell at 1 x 2 : is anchored' 'Cell at 1 x 3 : is spanned' 'Cell at 1 x 4 : is anchored' 'Cell at 1 x 5 : is anchored'

**Note**: Option `row` is a shortcut that sets both `startRow` and `endRow` to the same row. (Use either `row` or `startRow` and `endRow` but never together). Similarly the `column` option sets both `startColumn` and `endColumn` to the same column (Use either `column` or `startColumn` and `endColumn` but never together).

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/tablewalker.ts#L222)

#### Parameters

* `table: ModelElement`

  A table over which the walker iterates.

* `options: TableWalkerOptions`

  An object with configuration.

  Defaults to `{}`

<a id="function-Symbol.iterator">

### `Symbol.iterator() → IterableIterator<TableSlot>`

Iterable interface.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/tablewalker.ts#L241)

#### Returns

* `IterableIterator<TableSlot>`

<a id="function-next">

### `next() → IteratorResult<TableSlot, undefined>`

Gets the next table walker's value.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/tablewalker.ts#L250)

#### Returns

* `IteratorResult<TableSlot, undefined>`

  The next table walker's value.

<a id="function-skipRow">

### `skipRow( row ) → void`

Marks a row to skip in the next iteration. It will also skip cells from the current row if there are any cells from the current row to output.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/tablewalker.ts#L321)

#### Parameters

* `row: number`

  The row index to skip.

#### Returns

* `void`

<a id="function-_advanceToNextRow">

### `_advanceToNextRow() → IteratorResult<TableSlot, undefined>` _(private)_

Advances internal cursor to the next row.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/tablewalker.ts#L328)

#### Returns

* `IteratorResult<TableSlot, undefined>`

<a id="function-_canJumpToStartRow">

### `_canJumpToStartRow() → boolean` _(private)_

Checks if part of the table can be skipped.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/tablewalker.ts#L439)

#### Returns

* `boolean`

<a id="function-_formatOutValue">

### `_formatOutValue( cell, anchorRow, anchorColumn ) → IteratorYieldResult<TableSlot>` _(private)_

A common method for formatting the iterator's output value.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/tablewalker.ts#L361)

#### Parameters

* `cell: ModelElement`

  The table cell to output.

* `anchorRow: number`

  The row index of a cell anchor slot.

  Defaults to `...`

* `anchorColumn: number`

  The column index of a cell anchor slot.

  Defaults to `...`

#### Returns

* `IteratorYieldResult<TableSlot>`

<a id="function-_getRowLength">

### `_getRowLength( rowIndex ) → number` _(private)_

Returns a number of columns in a row taking `colspan` into consideration.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/tablewalker.ts#L486)

#### Parameters

* `rowIndex: number`

#### Returns

* `number`

<a id="function-_getSpanned">

### `_getSpanned() → CellData | null` _(private)_

Returns the cell element that is spanned over the current cell location.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/tablewalker.ts#L384)

#### Returns

* `CellData | null`

<a id="function-_isOverEndColumn">

### `_isOverEndColumn() → boolean` _(private)_

Checks if the current cell is over [`_endColumn`](#member-_endColumn)

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/tablewalker.ts#L349)

#### Returns

* `boolean`

<a id="function-_isOverEndRow">

### `_isOverEndRow() → boolean` _(private)_

Checks if the current row is over [`_endRow`](#member-_endRow).

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/tablewalker.ts#L341)

#### Returns

* `boolean`

<a id="function-_jumpToNonSpannedRowClosestToStartRow">

### `_jumpToNonSpannedRowClosestToStartRow() → void` _(private)_

Sets the current row to `this._startRow` or the first row before it that has the number of cells equal to the number of columns in the table.

Example: +----+----+----+ | 00 | 01 | 02 | |----+----+----+ | 10 | 12 | | +----+ | | 22 | | +----+ | | 32 | <--- Start row +----+----+----+ | 40 | 41 | 42 | +----+----+----+

If the 4th row is a `this._startRow`, this method will: 1.) Count the number of columns this table has based on the first row (3 columns in this case). 2.) Check if the 4th row contains 3 cells. It doesn't, so go to the row before it. 3.) Check if the 3rd row contains 3 cells. It doesn't, so go to the row before it. 4.) Check if the 2nd row contains 3 cells. It does, so set the current row to that row.

Setting the current row this way is necessary to let the `next()` method loop over the cells spanning multiple rows or columns and update the `this._spannedCells` property.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/tablewalker.ts#L471)

#### Returns

* `void`

<a id="function-_markSpannedCell">

### `_markSpannedCell( row, column, data ) → void` _(private)_

Marks the cell location as spanned by another cell.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/tablewalker.ts#L426)

#### Parameters

* `row: number`

  The row index of the cell location.

* `column: number`

  The column index of the cell location.

* `data: CellData`

  A spanned cell details (cell element, anchor row and column).

#### Returns

* `void`

<a id="function-_recordSpans">

### `_recordSpans( cell, rowspan, colspan ) → void` _(private)_

Updates spanned cells map relative to the current cell location and its span dimensions.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/tablewalker.ts#L403)

#### Parameters

* `cell: ModelElement`

  A cell that is spanned.

* `rowspan: number`

  Cell height.

* `colspan: number`

  Cell width.

#### Returns

* `void`

<a id="function-_shouldSkipSlot">

### `_shouldSkipSlot() → boolean` _(private)_

Checks if the current slot should be skipped.

[See source](https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/tablewalker.ts#L371)

#### Returns

* `boolean`

---

Full index of the CKEditor 5 API reference: [llms.txt](llms.txt)
