Table

展示可排序和选择的结构化数据。

实时预览

正在加载预览…

参数配置

全部字段 8

TableItems

列表项 · 1
TableColumn[string]

TableOptions

Styles · 0

Go 代码

Go
// Inside a view; c is *ui.Context, t is nui.Theme.
// Keep s (example state) outside the view. See the complete example.
nui.Table(c.Key("table"), t, &s.Table, []nui.TableColumn[string]{{ID: "name", Title: "Project", Width: 240, Value: func(v string) string { return v }, Compare: func(a, b string) int { return strings.Compare(a, b) }}}, nui.TableOptions{Label: "Projects", Height: float32(240), Disabled: false})

使用说明

点击 Project 列标题排序,并选择数据行。完整示例展示 TableState 的数据初始化、稳定行 ID 与 Compare 函数;高度参数决定可滚动表格区域。

API 参考

Table

源码
func Table[T any](c *ui.Context, t Theme, s *TableState[T], columns []TableColumn[T], o TableOptions) ui.Element

TableOptions

源码
type TableOptions struct {
	Label, EmptyText string
	Height           float32
	Disabled         bool
	OnChange         func()
	Decoration
}
字段类型说明
Labelstring

Go 零值: ""

控件标签或可访问名称。

EmptyTextstring

Go 零值: ""

没有可显示数据时的文案。

Heightfloat32

Go 零值: 0

高度,单位为 DIP。

Disabledbool

Go 零值: false

禁用组件及其交互。

OnChangefunc()

Go 零值: nil

值发生变化时通知应用的回调。

UIStyles

Go 零值: zero value

以部件名称为键覆盖外观和间距。

Stylefunc(Slot, ui.Element)

Go 零值: nil

同步配置当前构建的部件;不要保留 Element。

TableColumn

源码
type TableColumn[T any] struct {
	ID, Title                 string
	Width, MinWidth, MaxWidth float32
	Align                     ui.Align
	Fixed                     bool // Prevents resizing/reordering; it does not freeze a scrolling column.
	Value                     func(T) string
	Compare                   func(T, T) int
	Cell                      func(*ui.Context, Theme, T, bool)
}
字段类型说明
IDstring

Go 零值: ""

唯一且稳定的业务标识。

Titlestring

Go 零值: ""

标题文字。

Widthfloat32

Go 零值: 0

宽度,单位为 DIP。

MinWidthfloat32

Go 零值: 0

最小宽度,单位为 DIP。

MaxWidthfloat32

Go 零值: 0

最大宽度,单位为 DIP。

Alignui.Align

Go 零值: zero value

内容对齐方式。

Fixedbool

Go 零值: false

阻止列调整宽度或重新排序;并非冻结列。

Valuefunc(T) string

Go 零值: nil

控件值、字段值或数据读取函数。

Comparefunc(T, T) int

Go 零值: nil

用于排序的比较函数。

Cellfunc(*ui.Context, Theme, T, bool)

Go 零值: nil

自定义单元格的同步构建函数。

TableState

源码
type TableState[T any] struct {
	List      ui.ListState
	Selection ui.Selection[string]
	Sort      ui.SortOrder

	CellBuilds int
	// contains filtered or unexported fields
}
字段类型说明
Listui.ListState

Go 零值: zero value

持久的列表滚动和交互状态。

Selectionui.Selection[string]

Go 零值: zero value

持久的选择状态。

Sortui.SortOrder

Go 零值: zero value

持久的排序状态。

CellBuildsint

Go 零值: 0

单元格构建次数,用于观测渲染工作量。