Descriptions 描述列表
成组展示多个只读字段,每个字段是「标签:内容」,按列排成网格,常见于详情页。
何时使用
- 展示一条记录的详情,例如订单信息、用户资料、资源配置。
- 字段较多、需要按栅格分列对齐时;也可以加边框做成表格式的详情。
- 需要多条记录逐行对比时,用 Table 表格(记录少字段多时用它的
Transposed()转置);字段需要编辑时用表单组件;需要按 Pro 规范的valueType自动格式化、行内编辑的详情,用ProDescriptions。
在线体验
切换场景可以看到基础用法、带边框、尺寸、垂直布局,以及随窗口断点变化的列数。
基本用法
下面代码放在应用的页面文件中;完整的 main 和窗口初始化见 打开自绘窗口。
import (
"github.com/tangoui/tango/ui"
)
var profile = []ui.DescItem{
{Label: "用户名", Value: "曲丽丽"},
{Label: "手机号", Value: "1810000000"},
{Label: "居住地", Value: "杭州市西湖区"},
{Label: "备注", Value: "无"},
{Label: "联系地址", Value: "浙江省杭州市西湖区万塘路 18 号", Span: 2},
}
func page(c *ui.Context) {
ui.Descriptions(c, profile).Title("用户信息")
}ui.Descriptions(c, items) 默认无边框,列数随窗口宽度变化,XS 为 1 列、SM 为 2 列、MD 起为 3 列。每个 DescItem 是一对 Label / Value,Span 让一项占多列,最后一行的末项会填满剩余列。内容在链式配置完成后构建。
常见用法
以下片段省略了 import。
带边框与标题区操作
Bordered() 画出表格式的边框,标签列带底色;Title 设置标题,Extra 在标题行右侧放操作按钮。
var order = []ui.DescItem{
{Label: "产品", Value: "云数据库"},
{Label: "计费模式", Value: "预付费"},
{Label: "自动续费", Value: "是"},
{Label: "下单时间", Value: "2018-04-24 18:00:00"},
{Label: "使用时间", Value: "2019-04-24 18:00:00", Span: 2},
}
var editOpen bool
func borderedDesc(c *ui.Context) {
ui.Descriptions(c, order).Title("订单详情").Bordered().Extra(func() {
if ui.Button(c, "编辑").Primary().Clicked() {
editOpen = true
}
})
}自定义内容
DescItem.Render 替代 Value,在内容区构建任意组件,例如状态徽标、标签或链接。
func customDesc(c *ui.Context) {
ui.Descriptions(c, []ui.DescItem{
{Label: "实例", Value: "db-prod-01"},
{Label: "状态", Render: func() { ui.Badge(c).Status("processing", "运行中") }},
{Label: "标签", Render: func() {
ui.Tag(c, "生产").Color("error")
ui.Tag(c, "华东 1").Color("processing")
}},
}).Title("实例信息").Bordered()
}尺寸、列数与垂直布局
Size(ui.Middle) / Size(ui.Small) 收紧间距(Ant Design 和 shadcn 默认 Large,Element Plus 默认 Middle);Column(n) 设置列数;Vertical() 把标签放在内容上方;NoColon() 去掉无边框模式下标签后的冒号;LabelWidth(px) 设置横向布局的标签宽度。
func compactDesc(c *ui.Context) {
ui.Descriptions(c, profile).Title("紧凑 · 4 列").Size(ui.Small).Column(4)
ui.Descriptions(c, profile[:4]).Title("垂直布局").Bordered().Vertical()
ui.Descriptions(c, profile).NoColon().LabelWidth(96)
}响应式列数
ColumnAt(bp, n) 从断点 bp 起使用 n 列,窗口宽度命中的最大断点生效,Column(n) 可设置兜底列数。Columns(map[ui.Breakpoint]int) 一次设置多个断点。单项可通过 DescItem.Spans 按断点设置跨度,或用 Filled: true 占满当前行的剩余列并结束该行。
func responsiveDesc(c *ui.Context) {
ui.Descriptions(c, profile).Title("响应式列数").Bordered().
ColumnAt(ui.XS, 1).ColumnAt(ui.SM, 2).ColumnAt(ui.LG, 3)
}主题与样式
| 风格 | 差异 |
|---|---|
| Ant Design | 标签为三级文字色;带边框时标签列为浅灰底(FillAlter),外框为大圆角。 |
| Element Plus | 对照 el-descriptions:标签底色更深(FillTertiary)、标签为次要文字色,外框直角,单元格内边距更紧凑,标签与内容之间留 16px。 |
| shadcn | 沿用 Ant Design 的结构与间距,颜色取 shadcn 主题的中性色。 |
组件令牌在 theme.Components.Descriptions(theme.DescriptionsToken):LabelBg、LabelColor、ContentColor、TitleColor、TitleFontSize、BorderColor、Radius、ItemPaddingBottom,以及带边框时三档尺寸的 CellPaddingBlock / CellPaddingInline(MD、SM 后缀对应 Middle 和 Small)。单个列表的标签底色也可以用 LabelBg(color) 直接覆盖。
ConfigProvider.Defaults("Descriptions", ...) 支持的键:"column"(int 或 map[ui.Breakpoint]int)、"bordered"、"size"、"layout"("vertical")、"colon"(bool)。
func descDefaults(c *ui.Context) {
ui.ConfigProvider(c).
Defaults("Descriptions", ui.Props{"bordered": true, "column": 2}).
Body(func() {
ui.Descriptions(c, profile).Title("全局默认带边框、2 列")
})
}注意事项
- 状态:描述列表是纯展示组件,不保存交互状态;
items可以每帧重新构造,也可以放在包级变量里复用。 - 列数与 Span:一项的
Span超过当前行剩余列数时整项换到下一行(Span大于列数时按列数算);列表最后一项会把Span补满剩余列;中间某行没占满时,该行各项按Span比例拉伸占满整行,列线就不再与其他行对齐,所以最好让每行的Span之和等于列数。 - 长内容:内容会在单元格内换行;很长的文本(如配置说明)建议单独占一整行(
Span等于列数)。 - RTL:在 RTL 布局中标签与内容的顺序、对齐自动镜像。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
Descriptions / List 列数随断点变化
gallery 演示 DescriptionsResponsive 的 Render 函数,源码 page_shad2b_variants.go#L63。
加载中…
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.Descriptions
func Descriptions(c *Context, items []DescItem) *DescriptionsWDescriptions adds a description list (no border). Without Column or a "column" default the column count follows antd's DEFAULT_COLUMN_MAP: 1 on xs, 2 on sm, 3 from md up. Modifiers apply until the end of the build; the content is built then.
ConfigProvider.Defaults("Descriptions", ...) keys: "column" (int or map[Breakpoint]int), "bordered", "size", "layout" ("vertical"), "colon" (bool).
返回 *DescriptionsW,可链式调用的方法见 DescriptionsW 方法。
DescriptionsW 方法
ui.DescriptionsW:DescriptionsW is a description list: title, extra, columns, bordered, sizes, vertical layout, label width, per-item span and custom render.
| 方法 | 说明 | 签名 |
|---|---|---|
Bordered | Bordered() *DescriptionsW | |
Column | Column(n int) *DescriptionsW | |
ColumnAt | ColumnAt sets the column count from breakpoint bp upwards (Ant column={{ xs: 1, sm: 2, md: 3 }}); the largest set breakpoint not above the window width wins, Column (default DEFAULT_COLUMN_MAP) is the fallback. | ColumnAt(bp Breakpoint, n int) *DescriptionsW |
Columns | Columns sets several breakpoint column counts at once. | Columns(m map[Breakpoint]int) *DescriptionsW |
Extra | Extra(fn func()) *DescriptionsW | |
LabelBg | LabelBg overrides the bordered label background. | LabelBg(col theme.Color) *DescriptionsW |
LabelWidth | LabelWidth(px float32) *DescriptionsW | |
Layout | Layout applies layout options to the list. | Layout(o ...layout.Option) *DescriptionsW |
NoColon | NoColon() *DescriptionsW | |
Size | Size(s ControlSize) *DescriptionsW | |
Title | Title(s string) *DescriptionsW | |
Vertical | Vertical() *DescriptionsW |
DescItem 参数类型
ui.DescItem:DescItem is one label/value pair; Render replaces Value with custom content, Span takes several columns.
| 字段 | 说明 | 类型 |
|---|---|---|
Label | string | |
Value | string | |
Span | int | |
Render | func() | |
Spans | Spans sets the span from a breakpoint upwards (antd span={{ xs: 1, md: 2 }}); the largest set breakpoint not above the window width wins, Span is the fallback. | map[Breakpoint]int |
Filled | Filled makes the item take the rest of its row and ends the row (antd span: 'filled'). | bool |