4.4 KiB
4.4 KiB
Table
This is the table class. It provides a sortable data grid with customizable columns, row selection, and scrolling capabilities.
Extends: VisualElement
Properties
| Property | Type | Default | Description |
|---|---|---|---|
| columns | table | {} | List of column definitions with {name, width} properties |
| data | table | {} | The table data as array of row arrays |
| headerColor | color | blue | Color of the column headers |
| selectedColor | color | lightBlue | Background color of selected row |
| gridColor | color | gray | Color of grid lines |
| sortDirection | string | "asc" | Sort direction ("asc" or "desc") |
| scrollOffset | number | 0 | Current scroll position |
| customSortFunction | table | {} | Custom sort functions for columns |
Functions
| Method | Returns | Description |
|---|---|---|
| Table.new | Table | Creates a new Table instance |
| Table:init | Table | Initializes the Table instance |
| Table:addColumn | Table | Adds a new column to the table |
| Table:addData | Table | Adds a new row of data to the table |
| Table:setColumnSortFunction | Table | Sets a custom sort function for a column |
| Table:setFormattedData | Table | Adds formatted data with raw sort values |
| Table:setData | Table | Sets table data with optional column formatters |
| Table:calculateColumnWidths | table | Calculates column widths for rendering |
| Table:sortData | Table | Sorts the table data by the specified column |
| Table:mouse_click | boolean | Handles header clicks for sorting and row selection |
| Table:mouse_scroll | boolean | Handles scrolling through the table data |
| Table:render | - | Renders the table with headers, data and scrollbar |
Table.new()
Creates a new Table instance
Returns
TableselfThe newly created Table instance
Table:init(props, basalt)
Parameters
propstableThe properties to initialize the element withbasalttableThe basalt instance
Returns
TableselfThe initialized instance
Table:addColumn(name, width)
Adds a new column to the table
Parameters
namestringThe name of the columnwidthnumberThe width of the column
Returns
TableselfThe Table instance
Table:addData(any)
Adds a new row of data to the table
Parameters
anyThedata for the new row
Returns
TableselfThe Table instance
Table:setColumnSortFunction(columnIndex, sortFn)
Sets a custom sort function for a specific column
Parameters
columnIndexnumberThe index of the columnsortFnfunctionFunction that takes (rowA, rowB) and returns comparison result
Returns
TableselfThe Table instance
Table:setFormattedData(displayData, sortData)
Adds data with both display and sort values
Parameters
displayDatatableThe formatted data for displaysortDatatableThe raw data for sorting (optional)
Returns
TableselfThe Table instance
Table:setData(rawData, formatters)
Set data with automatic formatting
Parameters
rawDatatableThe raw data arrayformatterstableOptional formatter functions for columns {[2] = function(value) return value end}
Returns
TableselfThe Table instance
Table:calculateColumnWidths(columns, totalWidth)
Parameters
columnstableThe column definitionstotalWidthnumberThe total available width
Returns
tableThecolumns with calculated visibleWidth
Table:sortData(columnIndex, fn)
Sorts the table data by column
Parameters
columnIndexnumberThe index of the column to sort byfnfunction? Optional custom sorting function
Returns
TableselfThe Table instance
Table:mouse_click(button, x, y)
Parameters
buttonnumberThe button that was clickedxnumberThe x position of the clickynumberThe y position of the click
Returns
booleanhandledWhether the event was handled
Table:mouse_scroll(direction, x, y)
Parameters
directionnumberThe scroll direction (-1 up, 1 down)xnumberThe x position of the scrollynumberThe y position of the scroll
Returns
booleanhandledWhether the event was handled