pesde goatsoft / lucy

Table

Rows and columns: a leaderboard, a friends list, an inventory, match history. Table draws the rows you give it in the order you give them. For headers the player can press to sort, use DataGrid.

Table exampleTable example
Lucy.Table.new({
    rows = {
        { rank = 1, name = "Nova", hype = 9840 },
        { rank = 2, name = "DJ Lucy", hype = 9125 },
        { rank = 3, name = "Kai", hype = 8410 },
        { rank = 4, name = "Juno", hype = 7360 },
    },
    rowId = function(dancer) return dancer.name end,
    columns = {
        { key = "rank", header = "#", width = 56, align = "center" },
        { key = "name", header = "Dancer", width = { fill = 2 } },
        { key = "hype", header = "Hype", width = { fill = 1 }, align = "right" },
    },
    highlight = function(dancer) return dancer.name == app.state:get("player.name") end,
    maxHeight = 360,
    Parent = parent,
})

Props

PropTypeDefaultState keyWhat it does
columnstableThe columns, see Columns
rowsarray, ref, computed or getterThe rows, any tables you like
rowIdfunctionthe positionGives each row an id, so updates change cells instead of rebuilding rows
rowHeightnumbercontrol heightEach row's height in pixels
highlightfunctionReturn true for rows to pick out, such as the player's own
stripedbooleanfalseShades every other row
onRowClickfunctionCalled with (row, index). Rows become buttons the player can select
headerCellfunctionDraws a header cell in place of its text: called with (column, align), returns a component, an Instance or text. DataGrid draws its sort buttons this way
emptystringShown when there are no rows
maxHeightnumberThe body scrolls past this many pixels
virtualbooleanfalseBuilds only the rows near the view, see Long lists
onEndReachedfunctionCalled when the player scrolls near the end, see Long lists
sizeUDim2full widthWith a height, the body fills what the header leaves
visiblebooleantrueyesShows or hides it

It also takes the props every component takes.

Columns

FieldWhat it does
keyThe field to show, and the column's id
idThe column's id, when it has no key
getA function from the row to the value, instead of key
headerThe header text
widthPixels, or { fill = n } for a share of the width the fixed columns leave. { fill = 1 } when left out
alignleft, center or right
cellA function that draws the cell, see below

A cell shows the value as text. For anything else, give the column a cell function. It gets ctx and returns an instance, a component or text:

  • ctx.row(): the row, kept up to date as your data changes
  • ctx.value(): the column's value for that row, also kept up to date
  • ctx.column: the column
{
    key = "userId",
    header = "",
    width = 48,
    cell = function(ctx)
        return Lucy.Avatar.new({ userId = ctx.value, size = "sm" })
    end,
}

Pass ctx.value or ctx.row themselves (not their results) to a component's props, and the cell follows the data.

Updating rows

Give new data whenever it changes: a new array, a ref you set, or a getter. Rows are matched by rowId, so a changed score changes one cell, and a row that moves up the board moves rather than being rebuilt. The header stays put while the body scrolls.

Long lists

With virtual = true and a rowHeight, the body is as tall as every row, but only the rows on screen and a few either side exist. They are built as they come near the view and removed as they leave, so a board of 10,000 players costs about what twenty cost.

onEndReached is called when the player scrolls within a few rows of the end, once for each row count. Load the next rows and add them to your data, and it is ready to fire again:

local dancers = Lucy.ref({})

Lucy.Table.new({
    rows = dancers,
    rowId = function(dancer) return dancer.userId end,
    columns = columns,
    virtual = true,
    rowHeight = 40,
    maxHeight = 400,
    onEndReached = function()
        local more = fetchNextDancers(#dancers())
        local all = table.clone(dancers())
        table.move(more, 1, #more, #all + 1, all)
        dancers(all)
    end,
})

Gamepad

With onRowClick, each row can be selected and pressed. Without it, rows are not selectable, and the body itself can be selected so the player can scroll it.

Preview

In Studio, from the command bar: Lucy.Preview.show("Table").