- Index
- »
- fan.haystack
- »
- Grid
Grid
@Js
const mixin Grid
Two dimensional tabular data structure composed of Cols and Rows. Grids may be created by factory methods on Etc or using GridBuilder. See docHaystack.
Get a column by its name
Return a new grid with additional column meta-data
Get a row by its index number or if index is out of bounds then return null
Get a column as a list of the cell values ordered by row
Return a new Grid which is the result of applying the given diffs to this grid
Replace every cell with the given from value with the to value
Return a new grid which finds matching the rows in this grid
Return a new Grid wich each col name mapped to its localized tag name if the col does not already have a display string
Return a new grid with all the columns removed except the given columns
Return a new grid with the given column renamed
Find one matching row or return null if no matches
Return a new grid which is a slice of the rows in this grid
Return if this grid contains the given column name
Join two grids by column name
Return a new Grid which is a copy of this grid with the rows reverse sorted by the given comparator function
Return true if the function returns true for all of the rows in the grid
Get the last row or return null if grid is empty
Return new grid with column meta-data replaced by given meta
Return if this is an error grid - meta has "err" tag
Return a new grid with an additional column
Return a new Grid which is a copy of this grid with the rows sorted by the given comparator function
Iterate the rows
Return a new grid which maps each of the rows to zero or more new Dicts
Return a new grid with multiple columns renamed
Return a new grid with the given column removed
Get the number of rows in the grid
Return a new grid by adding the given grid as a new set of columns to this grid
Meta-data for entire grid
Sort the given column in reverse
Return a new grid with only rows that define a unique key by the given key columns
Return a new grid with additional grid level meta-data
Sort using Etc.compareDis and Dict.dis
Return a new grid with grid level meta-data replaced by given meta
Convenience for sort which sorts the given column
Get a row by its index number
Return if this grid does not contains the given column name
Columns
Return a new grid which maps the rows to new Dict
Return a new grid with all the given columns removed
Convenience for size equal to zero
Return true if the function returns true for any of the rows in the grid
Return a new grid which finds matching rows based on the given filter
Find one matching row index or return null if no matches
Return if this grid conforms to the history grid shape
Map each row to a list of values
Return a new grid with the columns reordered
Iterate every row until the function returns non-null
Get the first row or return null if grid is empty
Grid addCol(Str name, Obj? meta, |Row,Int->Obj?| f)
Return a new grid with an additional column. The cells of the column are created by calling the mapping function for each row. The meta may be any value accepted by Etc.makeDict
Grid addColMeta(Obj col, Obj? meta)
Return a new grid with additional column meta-data. The new tags are merged according to Etc.dictMerge. The col parameter may be either a Col or column name. The meta may be any value accepted by Etc.makeDict. If column is not found then return this. Also see setColMeta.
Grid addCols(Grid x)
Return a new grid by adding the given grid as a new set of columns to this grid. If the given grid contains duplicate column names, then they are given auto-generated unique names. If the given grid contains fewer rows then this grid, then the missing cells are filled with null.
Grid addMeta(Obj? meta)
Return a new grid with additional grid level meta-data. The new tags are merged according to Etc.dictMerge. The meta may be any value accepted by Etc.makeDict Also see setMeta.
Bool all(|Row,Int->Bool| f)
Return true if the function returns true for all of the rows in the grid. If the grid is empty, return false.
Bool any(|Row,Int->Bool| f)
Return true if the function returns true for any of the rows in the grid. If the grid is empty, return false.
abstract Col? col(Str name, Bool checked)
Get a column by its name. If not resolved then return null or throw UnknownNameErr based on checked flag.
Str[] colDisNames()
Convenience for cols mapped to Col.dis. The resulting list is safe for mutating.
Str[] colNames()
Convenience for cols mapped to Col.name. The resulting list is safe for mutating.
Obj?[] colToList(Obj col, Type listOf)
Get a column as a list of the cell values ordered by row.
abstract Col[] cols()
Columns
Grid colsToLocale()
Return a new Grid wich each col name mapped to its localized tag name if the col does not already have a display string. See Etc.tagToLocale and hx.doc.skyspark::Localization#tags.
Grid commit(Grid diffs)
Return a new Grid which is the result of applying the given diffs to this grid. The diffs must have the same number of rows as this grid. Any cells in the diffs with a None.val are removed from this grid, otherwise they are updated/added.
abstract Void each(|Row,Int| f)
Iterate the rows
abstract Obj? eachWhile(|Row,Int->Obj?| f)
Iterate every row until the function returns non-null. If function returns non-null, then break the iteration and return the resulting object. Return null if the function returns null for every item
Grid filter(Filter filter, HaystackContext? cx)
Return a new grid which finds matching rows based on the given filter. Also see findAll.
Row? find(|Row,Int->Bool| f)
Find one matching row or return null if no matches. Also see findIndex and findAll.
Grid findAll(|Row,Int->Bool| f)
Return a new grid which finds matching the rows in this grid. The has the same meta and column definitions. Also see find and filter.
Int? findIndex(|Row,Int->Bool| f)
Find one matching row index or return null if no matches. Also see find.
abstract Row? first()
Get the first row or return null if grid is empty.
Grid flatMap(|Row,Int->Obj?| f)
Return a new grid which maps each of the rows to zero or more new Dicts. The grid meta and existing column meta are maintained. New columns have empty meta.
@Operator
abstract Row get(Int index)
Get a row by its index number. Throw UnsupportedErr is the grid doesn't support indexed based row access.
@Operator
Grid getRange(Range r)
Return a new grid which is a slice of the rows in this grid. Negative indexes may be used to access from the end of the grid. The has the same meta and column definitions.
abstract Row? getSafe(Int index)
Get a row by its index number or if index is out of bounds then return null. Throw UnsupportedErr is the grid doesn't support indexed based row access.
Bool has(Str name)
Return if this grid contains the given column name.
Bool isEmpty()
Convenience for size equal to zero.
Bool isErr()
Return if this is an error grid - meta has "err" tag.
Bool isHisGrid()
Return if this grid conforms to the history grid shape:
- has at least two columns
- first column is named "ts"
- has meta hisStart and hisEnd DateTime values
This method does not check timezones or the ts cells.
Grid join(Grid that, Obj joinCol)
Join two grids by column name. The joinCol parameter may be a Col or col name. Current implementation requires:
- grids cannot have conflicting col names (other than join col)
- each row in both grids must have a unique value for join col
- grid level meta is merged
- join column meta is merged
Grid keepCols(Obj[] toKeep)
Return a new grid with all the columns removed except the given columns. The toKeep columns can be Col instances or column names. Columns not found are silently ignored.
virtual Row? last()
Get the last row or return null if grid is empty. Throw UnsupportedErr is the grid doesn't support indexed based row access.
Grid map(|Row,Int->Obj?| f)
Return a new grid which maps the rows to new Dict. The grid meta and existing column meta are maintained. New columns have empty meta. If the mapping function returns null, then the row is removed.
Obj?[] mapToList(|Row,Int->Obj?| f)
Map each row to a list of values.
abstract Dict meta()
Meta-data for entire grid
Bool missing(Str name)
Return if this grid does not contains the given column name.
Grid removeCol(Obj col)
Return a new grid with the given column removed. The col parameter may be either a Col or column name. If column doesn't exist return this grid.
Grid removeCols(Obj[] toRemove)
Return a new grid with all the given columns removed. The toRemove columns can be Col instances or column names. Columns not found are silently ignored.
Grid renameCol(Obj oldCol, Str newName)
Return a new grid with the given column renamed. The oldCol parameter may be a Col or col name.
Grid renameCols(Obj:Str oldToNew)
Return a new grid with multiple columns renamed. The given map is keyed old column names and maps to new column names. Any column names not found are ignored.
Grid reorderCols(Obj[] cols)
Return a new grid with the columns reordered. The given list of names represents the new order and must contain the same current Col instances or column names. Any column names not found are ignored.
Grid replace(Obj? from, Obj? to)
Replace every cell with the given from value with the to value. The resulting grid has the same grid and col meta. Replacement comparison is by via Fantom equality via == operator, so it will only replace scalar values or null.
Grid setColMeta(Obj col, Obj? meta)
Return new grid with column meta-data replaced by given meta. The col parameter may be either a Col or column name. The meta may be any value accepted by Etc.makeDict If column is not found then return this. Also see addColMeta.
Grid setMeta(Obj? meta)
Return a new grid with grid level meta-data replaced by given meta. The meta may be any value accepted by Etc.makeDict. Also see addMeta.
abstract Int size()
Get the number of rows in the grid. Throw UnsupportedErr if the grid doesn't support a size.
Grid sort(|Row,Row->Int| f)
Return a new Grid which is a copy of this grid with the rows sorted by the given comparator function.
Grid sortCol(Obj col)
Convenience for sort which sorts the given column. The col parameter can be a Col or a str name. The sorting algorithm used is the same one used by the table UI based on the localized display string. If column is not found then return this.
Grid sortColr(Obj col)
Sort the given column in reverse. See sortCol
Grid sortDis()
Sort using Etc.compareDis and Dict.dis.
Grid sortr(|Row,Row->Int| f)
Return a new Grid which is a copy of this grid with the rows reverse sorted by the given comparator function.
Grid unique(Obj[] keyCols)
Return a new grid with only rows that define a unique key by the given key columns. If multiple rows have the same key cells, then the first row is returned and subsequent rows are removed. The keyCols can be Col instances or column names.