Table
This is the table class. It provides a sortable data grid with customizable columns, row selection, and scrolling capabilities. Built on Collection for consistent item management.
Extends: Collection
Usage
lua
local peopleTable = main:addTable()
:setPosition(1, 2)
:setSize(49, 10)
:setColumns({
{name = "Name", width = 15},
{name = "Age", width = 8},
{name = "Country", width = 12},
{name = "Score", width = 10}
})
:setBackground(colors.black)
:setForeground(colors.white)
peopleTable:addRow("Alice", 30, "USA", 95)
peopleTable:addRow("Bob", 25, "UK", 87)
peopleTable:addRow("Charlie", 35, "Germany", 92)
peopleTable:addRow("Diana", 28, "France", 88)
peopleTable:addRow("Eve", 32, "Spain", 90)
peopleTable:addRow("Frank", 27, "Italy", 85)
peopleTable:addRow("Grace", 29, "Canada", 93)
peopleTable:addRow("Heidi", 31, "Australia", 89)
peopleTable:addRow("Ivan", 26, "Russia", 91)
peopleTable:addRow("Judy", 33, "Brazil", 86)
peopleTable:addRow("Karl", 34, "Sweden", 84)
peopleTable:addRow("Laura", 24, "Norway", 82)
peopleTable:addRow("Mallory", 36, "Netherlands", 83)
peopleTable:addRow("Niaj", 23, "Switzerland", 81)
peopleTable:addRow("Olivia", 38, "Denmark", 80)Properties
| Property | Type | Default | Description |
|---|---|---|---|
| columns | table | {} | List of column definitions with {name, width} properties |
| headerColor | color | blue | Color of the column headers |
| gridColor | color | gray | Color of grid lines |
| sortDirection | string | "asc" | Sort direction ("asc" or "desc") |
| customSortFunction | table | {} | Custom sort functions for columns |
| offset | number | 0 | Scroll offset for vertical scrolling |
| showScrollBar | boolean | true | Whether to show the scrollbar when items exceed height |
| scrollBarSymbol | string | " | " Symbol used for the scrollbar handle |
| scrollBarBackground | string | "\127" | Symbol used for the scrollbar background |
| scrollBarColor | color | lightGray | Color of the scrollbar handle |
| scrollBarBackgroundColor | color | gray | Background color of the scrollbar |
Events
| Event | Parameters | Description |
|---|---|---|
| onRowSelect | rowIndex number, row table | Fired when a row is selected |
Functions
| Method | Returns | Description |
|---|---|---|
| Table:addRow | Table | Adds a new row with cell values |
| Table:removeRow | Table | Removes a row at the specified index |
| Table:getRow | row | Gets the row data at the specified index |
| Table:updateCell | Table | Updates a cell value at row and column |
| Table:getSelectedRow | row | Gets the currently selected row data |
| Table:clearData | Table | Removes all rows from the table |
| Table:addColumn | Table | Adds a new column to the table |
| Table:setColumnSortFunction | Table | Sets a custom sort function for a column |
| Table:setData | Table | Sets table data with optional column formatters |
| Table:getData | table | Gets all rows as array of cell arrays |
| Table:sortByColumn | Table | Sorts the table data by the specified column |
| Table:onRowSelect | Table | Registers a callback when a row is selected |
Table:addRow(any)
Adds a new row to the table
Parameters
anyThecell values for the new row
Returns
TableselfThe Table instance
Usage
lua
table:addRow("Alice", 30, "USA")Table:removeRow(rowIndex)
Removes a row by index
Parameters
rowIndexnumberThe index of the row to remove
Returns
TableselfThe Table instance
Table:getRow(rowIndex)
Gets a row by index
Parameters
rowIndexnumberThe index of the row
Returns
rowTherow data or nil
Table:updateCell(rowIndex, colIndex, value)
Updates a specific cell value
Parameters
rowIndexnumberThe row indexcolIndexnumberThe column indexvalueanyThe new value
Returns
TableselfThe Table instance
Table:getSelectedRow()
Gets the currently selected row
Returns
rowTheselected row or nil
Table:clearData()
Clears all table data
Returns
TableselfThe Table instance
Table:addColumn(name, width)
Adds a new column to the table
Parameters
namestringThe name of the columnwidthnumber|stringThe width of the column (number, "auto", or "30%")
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:setData(rawData, formatters)
Set data with automatic formatting
Parameters
rawDatatableThe raw data array (array of row arrays)formatterstable? Optional formatter functions for columns {[2] = function(value)return value end}
Returns
TableselfThe Table instance
Usage
lua
table:setData({{...}}, {[1] = tostring, [2] = function(age) return age.."y" end})Table:getData()
Gets all table data
Returns
tabledataArray of row cell arrays
Table:sortByColumn(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:onRowSelect(callback)
Registers callback for row selection
Parameters
callbackfunctionThe callback function(rowIndex, row)
Returns
TableselfThe Table instance