TableWalker
The table iterator class. It allows to iterate over table cells. For each cell the iterator yields TableSlot with proper table cell attributes.
Properties
_cellIndex : numberinternalmodule:table/tablewalker~TableWalker#_cellIndexThe cell index in a parent row. For spanned cells when
_includeAllSlotsis set totrue, this represents the index of the next table cell._column : numberinternalmodule:table/tablewalker~TableWalker#_columnThe current column index.
_row : numberinternalmodule:table/tablewalker~TableWalker#_rowThe current row index.
_rowIndex : numberinternalmodule:table/tablewalker~TableWalker#_rowIndexThe index of the current row element in the table.
_table : ModelElementinternalreadonlymodule:table/tablewalker~TableWalker#_tableThe walker's table element.
_endColumn : number | undefinedprivatereadonlymodule:table/tablewalker~TableWalker#_endColumnIf set, the table walker will only output cells up to a given column.
_endRow : null | number | undefinedprivatereadonlymodule:table/tablewalker~TableWalker#_endRowA row index at which this iterator will end.
_includeAllSlots : booleanprivatereadonlymodule:table/tablewalker~TableWalker#_includeAllSlotsEnables output of spanned cells that are normally not yielded.
_jumpedToStartRow : booleanprivatemodule:table/tablewalker~TableWalker#_jumpedToStartRowIndicates whether the iterator jumped to (or close to) the start row, ignoring rows that don't need to be traversed.
_nextCellAtColumn : numberprivatemodule:table/tablewalker~TableWalker#_nextCellAtColumnIndex of the next column where a cell is anchored.
_skipRows : Set<number>privatereadonlymodule:table/tablewalker~TableWalker#_skipRowsRow indexes to skip from the iteration.
_spannedCells : Map<number, Map<number, CellData>>privatereadonlymodule:table/tablewalker~TableWalker#_spannedCellsHolds a map of spanned cells in a table.
_startColumn : numberprivatereadonlymodule:table/tablewalker~TableWalker#_startColumnIf set, the table walker will only output cells from a given column and following ones or cells that overlap them.
_startRow : null | numberprivatereadonlymodule:table/tablewalker~TableWalker#_startRowA row index from which this iterator will start.
Methods
constructor( table, options )module:table/tablewalker~TableWalker#constructorCreates 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
includeAllSlotsoption to the constructor.The most important values of the iterator are column and row indexes of a cell.
See
TableSlotwhat 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 ); }Copy codeFor 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' ); }Copy codewill 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
rowis a shortcut that sets bothstartRowandendRowto the same row. (Use eitherroworstartRowandendRowbut never together). Similarly thecolumnoption sets bothstartColumnandendColumnto the same column (Use eithercolumnorstartColumnandendColumnbut never together).Parameters
table : ModelElementA table over which the walker iterates.
options : TableWalkerOptionsAn object with configuration.
Defaults to
{}
Symbol.iterator() → IterableIterator<TableSlot>module:table/tablewalker~TableWalker#Symbol.iteratormodule:table/tablewalker~TableWalker#nextGets the next table walker's value.
Returns
IteratorResult<TableSlot, undefined>The next table walker's value.
skipRow( row ) → voidmodule:table/tablewalker~TableWalker#skipRowMarks 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 : numberThe row index to skip.
Returns
void
_advanceToNextRow() → IteratorResult<TableSlot, undefined>privatemodule:table/tablewalker~TableWalker#_advanceToNextRow_canJumpToStartRow() → booleanprivatemodule:table/tablewalker~TableWalker#_canJumpToStartRow_formatOutValue( cell, anchorRow, anchorColumn ) → IteratorYieldResult<TableSlot>privatemodule:table/tablewalker~TableWalker#_formatOutValueA common method for formatting the iterator's output value.
Parameters
cell : ModelElementThe table cell to output.
anchorRow : numberThe row index of a cell anchor slot.
Defaults to
...anchorColumn : numberThe column index of a cell anchor slot.
Defaults to
...
Returns
IteratorYieldResult<TableSlot>
_getRowLength( rowIndex ) → numberprivatemodule:table/tablewalker~TableWalker#_getRowLengthReturns a number of columns in a row taking
colspaninto consideration.Parameters
rowIndex : number
Returns
number
_getSpanned() → null | CellDataprivatemodule:table/tablewalker~TableWalker#_getSpannedReturns the cell element that is spanned over the current cell location.
Returns
null | CellData
_isOverEndColumn() → booleanprivatemodule:table/tablewalker~TableWalker#_isOverEndColumn_isOverEndRow() → booleanprivatemodule:table/tablewalker~TableWalker#_isOverEndRow_jumpToNonSpannedRowClosestToStartRow() → voidprivatemodule:table/tablewalker~TableWalker#_jumpToNonSpannedRowClosestToStartRowSets the current row to
this._startRowor 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 thethis._spannedCellsproperty.Returns
void
_markSpannedCell( row, column, data ) → voidprivatemodule:table/tablewalker~TableWalker#_markSpannedCellMarks the cell location as spanned by another cell.
Parameters
row : numberThe row index of the cell location.
column : numberThe column index of the cell location.
data : CellDataA spanned cell details (cell element, anchor row and column).
Returns
void
_recordSpans( cell, rowspan, colspan ) → voidprivatemodule:table/tablewalker~TableWalker#_recordSpansUpdates spanned cells map relative to the current cell location and its span dimensions.
Parameters
cell : ModelElementA cell that is spanned.
rowspan : numberCell height.
colspan : numberCell width.
Returns
void
_shouldSkipSlot() → booleanprivatemodule:table/tablewalker~TableWalker#_shouldSkipSlot