Table
展示可排序和选择的结构化数据。
实时预览
正在加载预览…
参数配置
全部字段 8
TableItems
列表项 · 1
TableColumn[string]
TableOptions
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.ElementTableOptions
源码type TableOptions struct {
Label, EmptyText string
Height float32
Disabled bool
OnChange func()
Decoration
}| 字段 | 类型 | 说明 |
|---|---|---|
Label | stringGo 零值: | 控件标签或可访问名称。 |
EmptyText | stringGo 零值: | 没有可显示数据时的文案。 |
Height | float32Go 零值: | 高度,单位为 DIP。 |
Disabled | boolGo 零值: | 禁用组件及其交互。 |
OnChange | func()Go 零值: | 值发生变化时通知应用的回调。 |
UI | StylesGo 零值: | 以部件名称为键覆盖外观和间距。 |
Style | func(Slot, ui.Element)Go 零值: | 同步配置当前构建的部件;不要保留 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)
}| 字段 | 类型 | 说明 |
|---|---|---|
ID | stringGo 零值: | 唯一且稳定的业务标识。 |
Title | stringGo 零值: | 标题文字。 |
Width | float32Go 零值: | 宽度,单位为 DIP。 |
MinWidth | float32Go 零值: | 最小宽度,单位为 DIP。 |
MaxWidth | float32Go 零值: | 最大宽度,单位为 DIP。 |
Align | ui.AlignGo 零值: | 内容对齐方式。 |
Fixed | boolGo 零值: | 阻止列调整宽度或重新排序;并非冻结列。 |
Value | func(T) stringGo 零值: | 控件值、字段值或数据读取函数。 |
Compare | func(T, T) intGo 零值: | 用于排序的比较函数。 |
Cell | func(*ui.Context, Theme, T, bool)Go 零值: | 自定义单元格的同步构建函数。 |
TableState
源码type TableState[T any] struct {
List ui.ListState
Selection ui.Selection[string]
Sort ui.SortOrder
CellBuilds int
// contains filtered or unexported fields
}| 字段 | 类型 | 说明 |
|---|---|---|
List | ui.ListStateGo 零值: | 持久的列表滚动和交互状态。 |
Selection | ui.Selection[string]Go 零值: | 持久的选择状态。 |
Sort | ui.SortOrderGo 零值: | 持久的排序状态。 |
CellBuilds | intGo 零值: | 单元格构建次数,用于观测渲染工作量。 |