Skip to content

Empty 空状态 ​

在列表、表格或容器没有可展示内容时,提供明确的空状态说明和下一步操作。

何时使用 ​

  • 查询结果为空、用户尚未创建数据或数据暂时不可用时使用。
  • 需要引导用户创建第一条数据时,在 Body 中放置主要操作。
  • 正在加载时用 Skeleton 或 Spin;发生错误时用 Result。

在线体验 ​

Empty 空状态 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

go
import "github.com/tangoui/tango/ui"

func emptyPage(c *ui.Context) {
    ui.Empty(c).Description("暂无订单").Body(func() {
        ui.Button(c, "立即创建").Primary()
    })
}

默认显示空状态插画和本地化的无数据文案。Description("") 可隐藏说明文字。

常见用法 ​

紧凑空状态 ​

go
func emptySimple(c *ui.Context) {
    ui.Empty(c).Simple().Description("暂无筛选结果")
}

Simple() 使用紧凑插画和间距,适合 Select、Table、List 等嵌入空间。

自定义尺寸 ​

go
func emptySize(c *ui.Context) {
    ui.Empty(c).ImageSize(60).Description("暂无图片")
}

ImageSize 设置图片高度;Ant 默认插画约 100,Simple 约 40,Element 约 160。

自定义图片与操作 ​

go
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) 可提供补充信息。

放入布局 ​

go
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 入门。

Empty 空状态

gallery 演示 Empty 的 Render 函数,源码 page_data_misc.go#L49。

加载中…

Empty 标题与间距

gallery 演示 EmptyAlign 的 Render 函数,源码 page_b2g.go#L113。

加载中…

API ​

签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。

入口函数 ​

ui.Empty ​

go
func Empty(c *Context) *EmptyW

Empty 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 ​

go
func Title(c *Context, s string, level int) *Label

Title 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.

方法说明签名
BodyBody adds actions (buttons) below the description and builds the empty state.Body(fn func()) *Box
DescriptionDescription sets the text ("" hides it).Description(s string) *EmptyW
ImageImage replaces the picture with custom content.Image(fn func()) *EmptyW
ImageSizeImageSize sets the picture height (Ant default 100, simple 40, Element 160).ImageSize(px float32) *EmptyW
LayoutLayout applies layout options to the empty state.Layout(o ...layout.Option) *EmptyW
SimpleSimple uses the compact picture and spacing (Ant PRESENTED_IMAGE_SIMPLE; used inside selects, tables and lists in both design systems).Simple() *EmptyW
TitleTitle 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).

方法说明签名
AlignAlign aligns the text in its box.Align(a TextAlign) *Label
CodeCode() *Label
ColorColor sets the text color.Color(col theme.Color) *Label
CopiedCopied reports whether the copy icon was clicked within the last 3s.Copied() bool
CopyTextCopyText is Copyable with a different clipboard payload.CopyText(payload string) *Label
CopyableCopyable 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
DeleteDelete() *Label
DisabledDisabled greys the text out.Disabled() *Label
EditableEditable adds an edit icon; clicking it swaps the text for an input.Editable(val *string) *Label
EditingEditing reports whether the label is being edited.Editing() bool
EllipsisEllipsis keeps one line and truncates with "…".Ellipsis() *Label
ExpandableExpandable adds an "展开" link when the text is cut by Lines(n); open is toggled by the link ("收起" collapses when open).Expandable(open *bool) *Label
IDID returns the node ID.ID() ID
ItalicItalic slants the text (synthetic oblique).Italic() *Label
LayoutLayout applies layout options to the text node.Layout(opts ...layout.Option) *Label
LineHeightLineHeight overrides the line height in px.LineHeight(px float32) *Label
LinesLines limits wrapping to n lines with an ellipsis.Lines(n int) *Label
MarkMark() *Label
NoWrapNoWrap keeps the text on one line.NoWrap() *Label
SecondarySecondary uses the secondary text color.Secondary() *Label
SizeSize sets the font size (line height follows the theme).Size(px float32) *Label
StrongStrong uses the theme's strong weight.Strong() *Label
TertiaryTertiary uses the tertiary text color (descriptions, hints).Tertiary() *Label
TextDirTextDir sets the paragraph base direction explicitly.TextDir(d text.Direction) *Label
TypeType colors the text by status: "success", "warning", "danger"/"error".Type(status string) *Label
UnderlineUnderline / Delete / Code / Mark decorations.Underline() *Label
WeightWeight sets the font weight.Weight(w text.Weight) *Label

相关链接 ​

以 MIT 许可发布