Class

TableWalker (table)

@ckeditor/ckeditor5-table/src/tablewalker

class

The table iterator class. It allows to iterate over table cells. For each cell the iterator yields TableSlot with proper table cell attributes.

Filtering

Properties

  • _cellIndex : Number

    protected

    The cell index in a parent row. For spanned cells when _includeAllSlots is set to true, this represents the index of the next table cell.

  • _column : Number

    protected

    The current column index.

  • _row : Number

    protected

    The current row index.

  • _table : Element

    protected readonly

    The walker's table element.

  • _endColumn : Number

    private readonly

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

  • _endRow : Number

    private readonly

    A row index at which this iterator will end.

  • _includeAllSlots : Boolean

    private readonly

    Enables output of spanned cells that are normally not yielded.

  • _nextCellAtColumn : Number

    private

    Index of the next column where a cell is anchored.

  • _skipRows : Set.<Number>

    private readonly

    Row indexes to skip from the iteration.

  • _spannedCells : Map.<Number, Number, Object>>

    private readonly

    Holds a map of spanned cells in a table.

  • _startColumn : Number

    private readonly

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

  • _startRow : Number

    private readonly

    A row index from which this iterator will start.

Methods

  • constructor( table, [ options ] = { [options.row], [options.startRow], [options.endRow], [options.column], [options.startColumn], [options.endColumn], [options.includeAllSlots] } )

    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 what values are returned by the table walker.

    To iterate over a given row:

    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:

    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).

    Parameters

    table : Element

    A table over which the walker iterates.

    [ options ] : Object

    An object with configuration.

    Properties
    [ options.row ] : Number

    A row index for which this iterator will output cells. Can't be used together with startRow and endRow.

    [ options.startRow ] : Number

    A row index from which this iterator should start. Can't be used together with row.

    Defaults to 0

    [ options.endRow ] : Number

    A row index at which this iterator should end. Can't be used together with row.

    [ options.column ] : Number

    A column index for which this iterator will output cells. Can't be used together with startColumn and endColumn.

    [ options.startColumn ] : Number

    A column index from which this iterator should start. Can't be used together with column.

    Defaults to 0

    [ options.endColumn ] : Number

    A column index at which this iterator should end. Can't be used together with column.

    [ options.includeAllSlots ] : Boolean

    Also return values for spanned cells.

    Defaults to false

    Defaults to {}

  • Symbol.iterator() → Iterable.<TableSlot>

    Iterable interface.

    Returns

    Iterable.<TableSlot>
  • next() → TableSlot

    Gets the next table walker's value.

    Returns

    TableSlot

    The next table walker's value.

  • skipRow( row )

    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.

    Parameters

    row : Number

    The row index to skip.

  • _advanceToNextRow() → TableSlot

    private

    Advances internal cursor to the next row.

    Returns

    TableSlot
  • _formatOutValue( cell, [ anchorRow ], [ anchorColumn ] ) → Object

    private

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

    Parameters

    cell : Element

    The table cell to output.

    [ anchorRow ] : Number

    The row index of a cell anchor slot.

    [ anchorColumn ] : Number

    The column index of a cell anchor slot.

    Returns

    Object
  • _getSpanned() → Element

    private

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

    Returns

    Element
  • _isOverEndColumn() → Boolean

    private

    Checks if the current cell is over _endColumn

    Returns

    Boolean
  • _isOverEndRow() → Boolean

    private

    Checks if the current row is over _endRow.

    Returns

    Boolean
  • _markSpannedCell( row, column, data )

    private

    Marks the cell location as spanned by another cell.

    Parameters

    row : Number

    The row index of the cell location.

    column : Number

    The column index of the cell location.

    data : Object

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

  • _recordSpans( cell, rowspan, colspan )

    private

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

    Parameters

    cell : Element

    A cell that is spanned.

    rowspan : Number

    Cell height.

    colspan : Number

    Cell width.

  • _shouldSkipSlot() → Boolean

    private

    Checks if the current slot should be skipped.

    Returns

    Boolean