Skip to content

Cascader 级联选择 ​

从一组有层级关系的选项中逐级选择,例如省 / 市 / 区、商品类目。下拉菜单按层级分列展开,选中后显示完整路径。

何时使用 ​

  • 数据是一棵树、且需要沿着一条路径逐级选择时(地区、组织、类目)。
  • 选项层级较深,平铺到一个 Select 中会难以查找时。
  • 需要从树中选择任意节点、并更关心节点本身而不是路径时,用 TreeSelect 树选择;没有层级关系时用 Select 选择器。

在线体验 ​

Cascader 级联选择 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

下面代码放在应用的页面文件中;完整的 main 和窗口初始化见 打开自绘窗口。

go
import (
	"strings"

	"github.com/tangoui/tango/ui"
)

var regions = []ui.CascaderOption{
	{Label: "浙江", Value: "zj", Children: []ui.CascaderOption{
		{Label: "杭州", Value: "hz", Children: []ui.CascaderOption{{Label: "西湖区", Value: "xh"}, {Label: "滨江区", Value: "bj"}}},
		{Label: "宁波", Value: "nb", Children: []ui.CascaderOption{{Label: "海曙区", Value: "hs"}}},
		{Label: "温州", Value: "wz", Disabled: true},
	}},
	{Label: "江苏", Value: "js", Children: []ui.CascaderOption{
		{Label: "南京", Value: "nj", Children: []ui.CascaderOption{{Label: "玄武区", Value: "xw"}}},
	}},
}

var area []string

func page(c *ui.Context) {
	ui.Cascader(c, &area, regions).Placeholder("请选择地区").AllowClear()
	ui.Text(c, "值:"+strings.Join(area, " / "))
}

选项用 []ui.CascaderOption 描述(Label 显示、Value 存储、Children 下一级、Disabled 禁用)。值绑定到 *[]string,是从根到叶子的 Value 路径,例如 ["zj", "hz", "xh"];输入框默认显示各级标签用 “ / ” 连接。

常见用法 ​

选择任意一级与悬停展开 ​

默认只有选到叶子才算完成选择。ChangeOnSelect() 允许点击任意一级就选中(例如只选到“浙江 / 杭州”)。ExpandOnHover() 让鼠标悬停时也能展开下一级。

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

var (
	tree = []ui.CascaderOption{
		{Label: "电子产品", Value: "e", Children: []ui.CascaderOption{
			{Label: "手机", Value: "phone"}, {Label: "电脑", Value: "pc"},
		}},
	}
	category []string
)

func page(c *ui.Context) {
	ui.Cascader(c, &category, tree).ChangeOnSelect().ExpandOnHover().Placeholder("请选择类目")
}

自定义显示内容 ​

DisplayRender 自定义输入框里显示的文本(单选模式),函数拿到各级标签和对应的选项。下面只显示最后一级:

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

var (
	opts = []ui.CascaderOption{{Label: "浙江", Value: "zj", Children: []ui.CascaderOption{{Label: "杭州", Value: "hz"}}}}
	city []string
)

func page(c *ui.Context) {
	ui.Cascader(c, &city, opts).DisplayRender(func(labels []string, _ []ui.CascaderOption) string {
		return labels[len(labels)-1]
	})
}

搜索 ​

Searchable() 允许输入关键字,默认按完整标签路径(如“浙江 / 杭州 / 西湖区”)不区分大小写匹配,结果以路径列表显示。FilterMethod 可替换匹配规则,函数收到输入内容和候选路径。

go
import (
	"strings"

	"github.com/tangoui/tango/ui"
)

var (
	opts = []ui.CascaderOption{{Label: "浙江", Value: "zj", Children: []ui.CascaderOption{{Label: "杭州", Value: "hz"}}}}
	pick []string
)

func page(c *ui.Context) {
	ui.Cascader(c, &pick, opts).Searchable().Placeholder("搜索地区")
	ui.Cascader(c, &pick, opts).Searchable().FilterMethod(func(q string, path []ui.CascaderOption) bool {
		last := path[len(path)-1]
		return strings.HasPrefix(last.Value, q) // 按末级的值前缀匹配
	})
}

多选与勾选策略 ​

Multiple(&paths) 切换为多选:每个选项前出现复选框,父子联动,选中项以标签显示,MaxTagCount 折叠多余标签。此时第二个参数可以传 nil。

ShowCheckedStrategy 决定显示哪些标签:ui.ShowParent(默认,父节点全选时只显示父节点)、ui.ShowChild(显示每个选中的叶子)、ui.ShowAll(父子都显示)。默认绑定值始终保存选中的叶子路径,CheckedPaths() 返回按策略折叠后的路径;StrategyValue(true) 让绑定值本身也按策略保存(与 antd 的 value 行为一致)。

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

var (
	opts = []ui.CascaderOption{
		{Label: "浙江", Value: "zj", Children: []ui.CascaderOption{{Label: "杭州", Value: "hz"}, {Label: "宁波", Value: "nb"}}},
		{Label: "江苏", Value: "js", Children: []ui.CascaderOption{{Label: "南京", Value: "nj"}}},
	}
	checked [][]string
)

func page(c *ui.Context) {
	w := ui.Cascader(c, nil, opts).Multiple(&checked).MaxTagCount(3).ShowCheckedStrategy(ui.ShowParent)
	if w.Changed() {
		_ = w.CheckedPaths() // 例如 [["zj"], ["js", "nj"]]
	}
}

动态加载 ​

LoadData 开启懒加载:展开一个没有 Children 且不是 IsLeaf 的选项时调用函数,参数是该选项的值路径;在任意 goroutine 中调用 done(子选项) 即可,期间箭头位置显示加载图标;done(nil) 表示它是叶子。

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

var (
	roots = []ui.CascaderOption{{Label: "浙江", Value: "zj"}, {Label: "江苏", Value: "js"}}
	lazy  []string
)

func fetchChildren(path []string) []ui.CascaderOption {
	if len(path) >= 2 {
		return nil // 第三级没有子节点
	}
	return []ui.CascaderOption{{Label: path[len(path)-1] + "-1", Value: path[len(path)-1] + "1", IsLeaf: len(path) == 1}}
}

func page(c *ui.Context) {
	ui.Cascader(c, &lazy, roots).LoadData(func(path []string, done func([]ui.CascaderOption)) {
		go func() { done(fetchChildren(path)) }()
	})
}

内嵌面板 ​

CascaderPanel 把级联列直接嵌在页面中,支持 ChangeOnSelect、Multiple 和 LoadData。用 Height(px) 调整列高,默认取 Cascader 令牌的 DropdownHeight。

go
import (
	"strings"

	"github.com/tangoui/tango/ui"
)

var (
	opts  = []ui.CascaderOption{{Label: "浙江", Value: "zj", Children: []ui.CascaderOption{{Label: "杭州", Value: "hz"}}}}
	inner []string
)

func page(c *ui.Context) {
	ui.CascaderPanel(c, &inner, opts).ChangeOnSelect().A11yLabel("地区")
	ui.Text(c, "已选:"+strings.Join(inner, " / "))
}

主题与样式 ​

  • 输入框外观与 Select 一致,支持 Variant(ui.FieldFilled) 等外观、Size、Status("error" | "warning")。
  • 组件令牌:theme.Components.Cascader(CascaderToken),包括 OptionHeight、ColumnWidth、DropdownHeight、OptionHoverBg、OptionSelectedBg、ColumnDividerColor 等;三套风格的选中背景、列分隔线不同,切换风格无需改代码。
  • ConfigProvider.Defaults("Cascader", ui.Props{...}) 支持 size、status、allowClear、showSearch、placeholder、expandTrigger("hover")、changeOnSelect、maxTagCount、showCheckedStrategy、strategyValue、variant 和 placement。

注意事项 ​

  • 状态:值就是你绑定的路径切片;菜单展开状态、搜索词由组件按调用位置保存。选项树在代码中变化后,不再存在的值会以原始值显示,必要时自行清空。
  • 选项数据:同一层级中 Value 必须唯一。选项树很大时把它放在包级变量或缓存中,避免每帧重新构造。
  • 大列。一列多于 100 项时自动虚拟化,只构建可见行和展开路径上的行,保留完整列的尺寸与滚动范围。无障碍树只暴露已构建的选项。Cascader 和 CascaderPanel 都可用 Virtual(true) 强制开启,或用 Virtual(false) 构建所有行。
  • 原地更新选项。虚拟列会缓存宽度。修改同一选项切片且长度不变时,给 Cascader 或 CascaderPanel 传入递增的 DataVersion(version),或替换切片,下一帧会重新测量列宽。
  • 大量搜索结果。Cascader 的路径搜索在结果多于 100 项时也自动虚拟化,受 Virtual(bool) 控制,并在每帧重新匹配和测量结果宽度。
  • 表单校验:放进 Form 时用 ui.RulesOf 绑定路径切片,规则用 ui.RequiredItems[string](参数为提示文字)。
  • 无障碍与键盘。Enter 或 ↑ / ↓ 打开下拉框,未开启搜索时也可按空格打开。↑ / ↓ 在当前列移动,→ 展开下一级、← 返回上一级(RTL 下互换),Enter 可选中叶子。在表单项里自动以表单项标签命名,也可用 A11yLabel 显式命名。
  • 截图与测试:Open()、Expand(path...) 用于强制展开菜单,只在截图和测试中使用。

示例代码 ​

以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。

Cascader 级联选择

gallery 演示 Cascader 的 Render 函数,源码 page_entry_select.go#L205。

加载中…

Cascader / TreeSelect 懒加载、勾选策略与虚拟滚动

gallery 演示 CascaderAdvanced 的 Render 函数,源码 page_p2b_tree.go#L40。

加载中…

API ​

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

入口函数 ​

ui.Cascader ​

go
func Cascader(c *Context, val *[]string, opts []CascaderOption) *CascaderW

Cascader adds a cascading select bound to *val (the selected value path).

返回 *CascaderW,可链式调用的方法见 CascaderW 方法。

ui.CascaderPanel ​

go
func CascaderPanel(c *Context, val *[]string, opts []CascaderOption) *CascaderPanelW

CascaderPanel adds the column menus of a Cascader inline, bound to *val (the selected value path). Clicking a leaf selects it (the menus stay); the panel is one Tab stop whose arrow keys move through the columns (Right / Enter opens the next column, Left goes back; mirrored in RTL), Enter / Space on a leaf selects it.

ui.CascaderPanel(c, &path, options).ChangeOnSelect()

返回 *CascaderPanelW,可链式调用的方法见 CascaderPanelW 方法。

CascaderW 方法 ​

ui.CascaderW:CascaderW selects a path in a tree through cascading menus.

方法说明签名
A11yLabelA11yLabel sets the accessible name (inside a FormItem the item label is used; the placeholder is the last resort).A11yLabel(s string) *CascaderW
AllowClearAllowClear() *CascaderW
ChangeOnSelectChangeOnSelect() *CascaderW
ChangedChanged reports a new selection this frame.Changed() bool
CheckedPathsCheckedPaths returns the checked paths of multiple mode by the checked strategy (the paths shown as tags).CheckedPaths() [][]string
DataVersionDataVersion tells the cascader that the options were modified in place (a virtual column caches its width by slice identity, length and this number): after editing options of the same slice without changing its length, change v (or pass a new slice) so the widths are recomputed on the next frame.DataVersion(v int) *CascaderW
DisabledDisabled(on bool) *CascaderW
DisplayRenderDisplayRender sets the text shown for the selected path (single selection; default the labels joined by " / "): fn receives the labels and the options of the path, root first (Ant displayRender, Element show-all-levels / separator).DisplayRender(fn func(labels []string, path []CascaderOption) string) *CascaderW
ExpandExpand sets the expanded path of an open menu (screenshots).Expand(path ...string) *CascaderW
ExpandOnHoverExpandOnHover() *CascaderW
FilterMethodFilterMethod replaces the search match (by default the "a / b / c" label path containing the typed text, case-insensitively) with fn: it receives the typed text and the options along a candidate path, root first (Ant showSearch.filter, Element filter-method).FilterMethod(fn func(query string, path []CascaderOption) bool) *CascaderW
IsOpenIsOpen reports whether the menus are shown.IsOpen() bool
LayoutLayout(o ...layout.Option) *CascaderW
LoadDataLoadData enables lazy loading: expanding an option that has no Children and is not IsLeaf calls fn with its value path; fn (now or later, on any goroutine) calls done with the children.LoadData(fn func(path []string, done func([]CascaderOption))) *CascaderW
MaxTagCountMaxTagCount(n int) *CascaderW
MaxTagPlaceholderMaxTagPlaceholder builds the text of the collapsed tag of multiple mode from the hidden labels (Ant maxTagPlaceholder; default "+ N ...").MaxTagPlaceholder(fn func(omitted []string) string) *CascaderW
MultipleMultiple(vals *[][]string) *CascaderW
NotFoundContentNotFoundContent sets the text shown when a search finds nothing (Ant notFoundContent, Element empty slot).NotFoundContent(s string) *CascaderW
OpenOpen forces the menus open (screenshots, tests).Open() *CascaderW
PlaceholderPlaceholder(s string) *CascaderW
PlacementPlacement sets where the menus open: BottomStart (default), BottomEnd, TopStart or TopEnd (Ant placement, Element placement); they flip when there is no room.Placement(p Placement) *CascaderW
SearchableSearchable() *CascaderW
SeparatorSeparator sets the text between path labels in the selected value, tags and search results (Element separator; default " / " as Ant's displayRender).Separator(s string) *CascaderW
ShowCheckedStrategyShowCheckedStrategy chooses the tags of multiple mode (default ShowParent: a fully checked parent replaces its children; ShowChild: every checked leaf; ShowAll: every checked node).ShowCheckedStrategy(s CheckedStrategy) *CascaderW
SizeSize(s ControlSize) *CascaderW
StatusStatus(s string) *CascaderW
StrategyValueStrategyValue makes the bound value of multiple mode follow the checked strategy, as Ant's value does (ShowParent, the default, stores a fully checked parent path instead of its leaf paths).StrategyValue(on bool) *CascaderW
VariantVariant sets the frame look (FieldOutlined by default).Variant(v FieldVariant) *CascaderW
VirtualVirtual forces virtual columns on or off; by default a column (or a search result list) longer than 100 options is virtual: only its visible rows and the row on the expanded path are built.Virtual(on bool) *CascaderW
WidthWidth(px float32) *CascaderW

CascaderPanelW 方法 ​

ui.CascaderPanelW:CascaderPanelW is an inline cascader; chain options, read Changed.

方法说明签名
A11yLabelA11yLabel names the panel and its first column (default: the generic "please select").A11yLabel(s string) *CascaderPanelW
BorderBorder toggles the panel frame (default on).Border(on bool) *CascaderPanelW
ChangeOnSelectChangeOnSelect selects parents too (clicking a parent selects it and opens its column).ChangeOnSelect() *CascaderPanelW
ChangedChanged reports a new selection this frame.Changed() bool
CheckedPathsCheckedPaths returns the checked paths of multiple mode by the checked strategy.CheckedPaths() [][]string
DataVersionDataVersion tells the panel that the options were modified in place (see CascaderW.DataVersion): change v after editing options of the same slice without changing its length.DataVersion(v int) *CascaderPanelW
DisabledDisabled turns the panel off.Disabled(on bool) *CascaderPanelW
ExpandExpand sets the expanded path (by default it follows the value).Expand(path ...string) *CascaderPanelW
ExpandOnHoverExpandOnHover opens a parent's column on hover.ExpandOnHover() *CascaderPanelW
HeightHeight sets the column height (default: the Cascader token's DropdownHeight).Height(px float32) *CascaderPanelW
IDID returns the panel's ID.ID() ID
LoadDataLoadData loads children lazily (see CascaderW.LoadData).LoadData(fn func(path []string, done func([]CascaderOption))) *CascaderPanelW
MultipleMultiple switches to checkboxes bound to *vals (the checked leaf paths; see ShowCheckedStrategy and StrategyValue).Multiple(vals *[][]string) *CascaderPanelW
ShowCheckedStrategyShowCheckedStrategy sets the checked strategy of multiple mode (see CascaderW.ShowCheckedStrategy); CheckedPaths returns its paths.ShowCheckedStrategy(s CheckedStrategy) *CascaderPanelW
StrategyValueStrategyValue makes the bound value of multiple mode follow the checked strategy (see CascaderW.StrategyValue).StrategyValue(on bool) *CascaderPanelW
VirtualVirtual forces virtual columns on or off (see CascaderW.Virtual; by default columns longer than 100 options are virtual).Virtual(on bool) *CascaderPanelW

CascaderOption 参数类型 ​

ui.CascaderOption:CascaderOption is a node of a Cascader tree. With LoadData, a node without Children that is not IsLeaf loads its children when expanded.

字段说明类型
Labelstring
Valuestring
Disabledbool
Children[]CascaderOption
IsLeafnever has children (relevant with LoadData)bool

相关链接 ​

以 MIT 许可发布