TableWalker (table)
@ckeditor/ckeditor5-table/src/tablewalker
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.
-
The walker's table element.
-
_cell : Number
private readonly
The cell index in a parent row. For spanned cells when
includeSpanned
is set totrue
, 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, Number>>
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.
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 | 05 | | +----+----+----+----+ | | 12 | 14 | 15 | | +----+----+----+----+ | | 22 | |----+----+ + | 31 | 32 | | +----+----+----+----+----+----+
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 cellInfo of tableWalker ) { console.log( 'Cell at ' + cellInfo.row + ' x ' + cellInfo.column + ' : ' + ( cellInfo.cell ? 'has data' : 'is spanned' ) ); }
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
{}
-
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, rowspan, colspan ) → Object
private
A common method for formatting the iterator's output value.
Parameters
cell : Element | undefined
The table cell to output. It might be undefined for spanned cell locations.
column : Number
Column index (use the cached value)
rowspan : Number
Rowspan of the current cell.
Defaults to
1
colspan : Number
Colspan of the current cell.
Defaults to
1
Returns
Object
-
_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 )
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.
-
_recordSpans( row, column, rowspan, colspan )
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.
-
_shouldSkipColumn( column, colspan ) → Boolean
private
Checks if the current column should be skipped.
Parameters
column : Number
colspan : Number
Returns
Boolean
-
_shouldSkipRow() → Boolean
private
Checks if the current row should be skipped.
Returns
Boolean