0%

Table

A table is a widget that allows you to display a slice of structs to users as a table and have them edit it (core.Table). Users can use context menus and drag-and-drop to add, remove, and move rows. Also, users can sort the table by clicking on columns.

For a slice of non-structs, use a list instead. For a single struct, use a form. For a map, use a keyed list. For a nested tree, use a tree.

Properties

You can make a table from any pointer to a slice of structs:

type language struct {
    Name   string
    Rating int
}
core.NewTable(b).SetSlice(&[]language{{"Go", 10}, {"Python", 5}})

You can prevent users from editing a table:

type language struct {
    Name   string
    Rating int
}
core.NewTable(b).SetSlice(&[]language{{"Go", 10}, {"Python", 5}}).SetReadOnly(true)

Events

You can detect when a user changes the value of a table:

type language struct {
    Name   string
    Rating int
}
sl := []language{{"Go", 10}, {"Python", 5}}
core.NewTable(b).SetSlice(&sl).OnChange(func(e events.Event) {
    core.MessageSnackbar(b, fmt.Sprintf("Languages: %v", sl))
})

Struct tags

See struct tags for a concise overview of all struct tags.

You can hide certain columns from the user:

type language struct {
    Name   string
    Rating int `display:"-"`
}
core.NewTable(b).SetSlice(&[]language{{"Go", 10}, {"Python", 5}})

You can also use the table struct tag, which overrides the display struct tag. This allows you to have a struct field displayed in a form but not a table, or vice versa:

type language struct {
    Name   string
    Rating int `display:"-" table:"+"`
}
core.NewTable(b).SetSlice(&[]language{{"Go", 10}, {"Python", 5}})

You can prevent the user from editing certain columns:

type language struct {
    Name   string `edit:"-"`
    Rating int
}
core.NewTable(b).SetSlice(&[]language{{"Go", 10}, {"Python", 5}})

You can change the label of a column:

type language struct {
    Name   string
    Rating int `label:"Score"`
}
core.NewTable(b).SetSlice(&[]language{{"Go", 10}, {"Python", 5}})

List button

You can make a button that opens a dialog with a table:

type language struct {
    Name   string
    Rating int
}
core.NewListButton(b).SetSlice(&[]language{{"Go", 10}, {"Python", 5}})

Generate

You can make it so that the documentation comments for struct fields are used as tooltips for the column headers and value widgets of a table by adding the type to generate#types and running generate:

// Add this once per package:
//go:generate core generate

// Add types:add for every type you want the documentation for:
type language struct { //types:add

    // This comment will be displayed in the tooltip for this field
    Name string
}