Empty 空状态
在列表、表格或容器没有可展示内容时,提供明确的空状态说明和下一步操作。
何时使用
在线体验
基本用法
import "github.com/tangoui/tango/ui"
func emptyPage(c *ui.Context) {
ui.Empty(c).Description("暂无订单").Body(func() {
ui.Button(c, "立即创建").Primary()
})
}默认显示空状态插画和本地化的无数据文案。Description("") 可隐藏说明文字。
常见用法
紧凑空状态
func emptySimple(c *ui.Context) {
ui.Empty(c).Simple().Description("暂无筛选结果")
}Simple() 使用紧凑插画和间距,适合 Select、Table、List 等嵌入空间。
自定义尺寸
func emptySize(c *ui.Context) {
ui.Empty(c).ImageSize(60).Description("暂无图片")
}ImageSize 设置图片高度;Ant 默认插画约 100,Simple 约 40,Element 约 160。
自定义图片与操作
func emptyCustom(c *ui.Context) {
ui.Empty(c).Image(func() {
ui.Text(c, "○")
}).Description("还没有项目").Body(func() {
ui.Button(c, "新建项目").Primary()
})
}Image 替换默认图片内容;Body 回调中构建按钮或其他轻量操作。
Title(s) 在说明文字上方显示主标题,Description(s) 可提供补充信息。
放入布局
import (
"github.com/tangoui/tango/ui"
"github.com/tangoui/tango/ui/layout"
)
func emptyLayout(c *ui.Context) {
ui.Empty(c).Layout(layout.Grow(1)).Description("暂无消息")
}Layout 让空状态参与外层布局;不要用固定高度模拟垂直居中。
主题与样式
Empty 的插画、颜色、间距和字体随主题切换。Simple 是跨 Ant Design、Element Plus、shadcn 的紧凑语义;如需自定义插画优先使用 Image,不要把主题颜色硬编码到页面。
注意事项
要区分“确实为空”“尚未加载”和“加载失败”,不要在三种状态间复用同一文案。查询条件变化时,空状态应由当前结果状态驱动。空状态操作应能通过键盘访问,并用文字说明动作;RTL 布局由主题和布局系统处理。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.Empty
func Empty(c *Context) *EmptyWEmpty adds the "no data" placeholder. Modifiers apply until the end of the build (or Body); the content is built then.
ui.Empty(c)
ui.Empty(c).Simple().Description("暂无订单")
ui.Empty(c).Body(func() { ui.Button(c, "立即创建").Primary() })
返回 *EmptyW,可链式调用的方法见 EmptyW 方法。
ui.Title
func Title(c *Context, s string, level int) *LabelTitle adds a heading; level 1..5 like Typography.Title.
返回 *Label,可链式调用的方法见 Label 方法。
EmptyW 方法
ui.EmptyW:EmptyW is an empty state: Ant's default/simple pictures or Element's illustration, custom image, size, description and actions.
| 方法 | 说明 | 签名 |
|---|---|---|
Body | Body adds actions (buttons) below the description and builds the empty state. | Body(fn func()) *Box |
Description | Description sets the text ("" hides it). | Description(s string) *EmptyW |
Image | Image replaces the picture with custom content. | Image(fn func()) *EmptyW |
ImageSize | ImageSize sets the picture height (Ant default 100, simple 40, Element 160). | ImageSize(px float32) *EmptyW |
Layout | Layout applies layout options to the empty state. | Layout(o ...layout.Option) *EmptyW |
Simple | Simple uses the compact picture and spacing (Ant PRESENTED_IMAGE_SIMPLE; used inside selects, tables and lists in both design systems). | Simple() *EmptyW |
Title | Title adds a heading between the picture and the description (shadcn EmptyTitle: text-lg font-medium). | Title(s string) *EmptyW |
Label 方法
ui.Label:Label is a run of text (Typography.Text).
| 方法 | 说明 | 签名 |
|---|---|---|
Align | Align aligns the text in its box. | Align(a TextAlign) *Label |
Code | Code() *Label | |
Color | Color sets the text color. | Color(col theme.Color) *Label |
Copied | Copied reports whether the copy icon was clicked within the last 3s. | Copied() bool |
CopyText | CopyText is Copyable with a different clipboard payload. | CopyText(payload string) *Label |
Copyable | Copyable adds a copy icon after the text; clicking it writes the text to the clipboard and shows a check mark for three seconds. | Copyable() *Label |
Delete | Delete() *Label | |
Disabled | Disabled greys the text out. | Disabled() *Label |
Editable | Editable adds an edit icon; clicking it swaps the text for an input. | Editable(val *string) *Label |
Editing | Editing reports whether the label is being edited. | Editing() bool |
Ellipsis | Ellipsis keeps one line and truncates with "…". | Ellipsis() *Label |
Expandable | Expandable adds an "展开" link when the text is cut by Lines(n); open is toggled by the link ("收起" collapses when open). | Expandable(open *bool) *Label |
ID | ID returns the node ID. | ID() ID |
Italic | Italic slants the text (synthetic oblique). | Italic() *Label |
Layout | Layout applies layout options to the text node. | Layout(opts ...layout.Option) *Label |
LineHeight | LineHeight overrides the line height in px. | LineHeight(px float32) *Label |
Lines | Lines limits wrapping to n lines with an ellipsis. | Lines(n int) *Label |
Mark | Mark() *Label | |
NoWrap | NoWrap keeps the text on one line. | NoWrap() *Label |
Secondary | Secondary uses the secondary text color. | Secondary() *Label |
Size | Size sets the font size (line height follows the theme). | Size(px float32) *Label |
Strong | Strong uses the theme's strong weight. | Strong() *Label |
Tertiary | Tertiary uses the tertiary text color (descriptions, hints). | Tertiary() *Label |
TextDir | TextDir sets the paragraph base direction explicitly. | TextDir(d text.Direction) *Label |
Type | Type colors the text by status: "success", "warning", "danger"/"error". | Type(status string) *Label |
Underline | Underline / Delete / Code / Mark decorations. | Underline() *Label |
Weight | Weight sets the font weight. | Weight(w text.Weight) *Label |