Class

TableWalker (table)

@ckeditor/ckeditor5-table/src/tablewalker

class

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

Filtering

Properties

  • column : Number

    readonly

    If set, the table walker will only output cells of a given column or cells that overlap it.

  • endRow : Number

    readonly

    A row index on which this iterator will end.

  • includeSpanned : Boolean

    readonly

    Enables output of spanned cells that are normally not yielded.

  • startRow : Number

    readonly

    A row index on which this iterator will start.

  • table : Element

    readonly

    The walker's table element.

  • _cellIndex : Number

    private readonly

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

  • _column : Number

    private readonly

    The current column index.

  • _row : Number

    private readonly

    The current row index.

  • _skipRows : Set.<Number>

    private readonly

    Row indexes to skip from the iteration.

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

    private readonly

    Holds a map of spanned cells in a table.

Methods

  • constructor( table, [ options ] = { [options.column], [options.startRow], [options.endRow], [options.includeSpanned] } )

    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 those locations that are occupied by a cell. To include also spanned rows and columns, pass the includeSpanned option to the constructor.

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

    See TableWalkerValue 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 cellInfo of tableWalker ) {
        console.log( 'A cell at row ' + cellInfo.row + ' and column ' + cellInfo.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, { startRow: 1, endRow: 1, includeSpanned: true } );
    
    for ( const value of tableWalker ) {
        console.log( 'Cell at ' + value.row + ' x ' + value.column + ' : ' + ( value.isSpanned ? 'is spanned' : 'has data' ) );
    }

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

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

    Parameters

    table : Element

    A table over which the walker iterates.

    [ options ] : Object

    An object with configuration.

    Properties
    [ options.column ] : Number

    A column index for which this iterator will output cells.

    [ options.startRow ] : Number

    A row index for which this iterator should start.

    Defaults to 0

    [ options.endRow ] : Number

    A row index for which this iterator should end.

    [ options.includeSpanned ] : Boolean

    Also return values for spanned cells.

    Defaults to false

    Defaults to {}

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

    Iterable interface.

    Returns

    Iterable.<TableWalkerValue>
  • next() → TableWalkerValue

    Gets the next table walker's value.

    Returns

    TableWalkerValue

    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

    Row index to skip.

  • _formatOutValue( cell, column, isSpanned, rowspan, colspan ) → Object

    private

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

    Parameters

    cell : Element

    The table cell to output.

    column : Number

    Column index (use the cached value).

    isSpanned : Boolean

    Whether the value is returned for a spanned cell location or actual cell.

    rowspan : Number

    Rowspan of the current cell.

    Defaults to 1

    colspan : Number

    Colspan of the current cell.

    Defaults to 1

    Returns

    Object
  • _getSpanned( row, column ) → Element

    private

    Returns the cell element that is spanned over row x column location.

    Parameters

    row : Number

    Row index of the cell location.

    column : Number

    Column index of the cell location.

    Returns

    Element
  • _isOverEndRow() → Boolean

    private

    Checks if the current row is over endRow.

    Returns

    Boolean
  • _isSpanned( row, column ) → Boolean

    private

    Checks if the current cell location (row x column) is spanned by another cell.

    Parameters

    row : Number

    Row index of a cell location to check.

    column : Number

    Column index of a cell location to check.

    Returns

    Boolean
  • _markSpannedCell( row, column, cell )

    private

    Marks the cell location as spanned by another cell.

    Parameters

    row : Number

    Row index of the cell location.

    column : Number

    Column index of the cell location.

    cell : Element

    Cell that is spanned.

  • _recordSpans( row, column, rowspan, colspan, cell )

    private

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

    Parameters

    row : Number

    Row index of a cell.

    column : Number

    Column index of a cell.

    rowspan : Number

    Cell height.

    colspan : Number

    Cell width.

    cell : Element

    Cell that is spanned.

  • _shouldSkipColumn() → Boolean

    private

    Checks if the current column should be skipped.

    Returns

    Boolean
  • _shouldSkipRow() → Boolean

    private

    Checks if the current row should be skipped.

    Returns

    Boolean