pesde goatsoft / lucy

DataGrid

A Table whose headers sort it: a shop, a server browser, a list of friends. Pressing a header sorts by that column, pressing it again flips the order, and a third press goes back to the order of your data. It works the same with a mouse, touch or a gamepad.

DataGrid exampleDataGrid example
Lucy.DataGrid.new({
    id = "shop",
    data = {
        { id = "moonwalk", name = "Moonwalk", price = 300, level = 10 },
        { id = "robot", name = "Robot", price = 150, level = 4 },
        { id = "shuffle", name = "Shuffle", price = 50, level = 1 },
        { id = "groove", name = "Groove", price = 500, level = 15 },
    },
    rowId = function(emote) return emote.id end,
    columns = {
        { key = "name", header = "Emote", width = { fill = 2 } },
        { key = "price", header = "Coins", width = 96, align = "right", descFirst = true },
        { key = "level", header = "Level", width = 88, align = "right" },
    },
    sorting = { id = "price", desc = true },
    onRowClick = function(emote) app.toast(`{emote.name}: {emote.price} coins`) end,
    Parent = parent,
})

Props

PropTypeDefaultState keyWhat it does
dataarray, ref, computed or getterThe rows, any tables you like
columnstableThe columns, see Columns
rowIdfunctionthe positionGives each row an id, so updates change cells instead of rebuilding rows
sortingtable or falsefalseyesThe sort, { id = "price", desc = true }; with multi, a list of them
multibooleanfalseSort by several columns: each header pressed breaks the ties of the ones before it
sortablebooleantruefalse turns sorting off
removablebooleantrueA third press on a header clears the sort
descFirstbooleanfalseEvery column sorts high to low on the first press
manualbooleanfalseYour data comes sorted (by your server): keep its order and only track the sort
onSortingChangefunctionCalled with (sorting, handle) when the sort changes
visiblebooleantrueyesShows or hides it

It also takes every prop of Table except rows: highlight, striped, onRowClick, empty, maxHeight, virtual, rowHeight, onEndReached and size. And it takes the props every component takes.

Columns

Columns take everything a Table column takes (key, id, get, header, width, align, cell), and how they sort:

FieldWhat it does
sortablefalse keeps this column's header plain. Columns with no key or get never sort
sortFn"auto", "number", "text" or a function (a, b) returning a negative number, 0 or a positive number
descFirstThe first press sorts high to low, for scores and prices
invertFor values where lower is better, such as a rank

Text sorts the way a person reads it: item2 before item10, and case does not count. Rows with no value for the column always go last, and rows with equal values keep the order of your data.

The sort in app state

The sort lives at ui.<id>.sorting, so you can save it with the player's settings and put it back, or change it from anywhere:

app.state:set("ui.shop.sorting", { id = "level", desc = true })

With multi, the sort is a list, first sort first:

app.state:set("ui.shop.sorting", { { id = "level", desc = true }, { id = "name", desc = false } })

The headers are SortButtons. A sorted header shows lucy:chevron-up or lucy:chevron-down, and while more than one column sorts, a small number for its place. To use your own icons, see Icons Lucy draws.

Methods and events

NameWhat it does
grid:toggle(columnId)What pressing that column's header does
grid.tableThe Table inside
grid.modelThe sorted rows and sort, from useTable
grid.sortButtonsThe header SortButtons, by column id

Pressing a header reports activate, for game sounds.

Examples

Sort by a custom order, such as emote rarity:

local order = { Common = 1, Rare = 2, Epic = 3, Legendary = 4 }

{
    key = "rarity",
    header = "Rarity",
    descFirst = true,
    sortFn = function(a, b) return order[a] - order[b] end,
}

A server sorted Top Dancers board that loads more as the player scrolls:

DataGrid exampleDataGrid example
Lucy.DataGrid.new({
    data = dancers,                    -- a ref you add pages to
    columns = columns,
    manual = true,                     -- the server sorts
    onSortingChange = function(sorting) reloadSortedBy(sorting) end,
    virtual = true,
    rowHeight = 40,
    maxHeight = 400,
    onEndReached = loadNextPage,
})

Preview

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