TreeSelect 树选择
在下拉框中以树形结构展示选项并进行选择,支持单选、多选、可勾选(父子联动)、搜索和动态加载。
何时使用
- 选项是一棵树(部门、目录、地区),并且需要选中树中的某个节点、而不必沿路径逐级选择时。
- 需要勾选多个节点并保持父子联动(选中父节点即选中全部子节点)时。
- 需要沿路径逐级选择、值为完整路径时,用 Cascader 级联选择;树需要常驻页面展示时,用 Tree 树形控件。
在线体验
基本用法
下面代码放在应用的页面文件中;完整的 main 和窗口初始化见 打开自绘窗口。
import (
"github.com/tangoui/tango/ui"
)
var depts = []ui.TreeSelectNode{
{Label: "总部", Value: "hq", Children: []ui.TreeSelectNode{
{Label: "研发部", Value: "rd", Children: []ui.TreeSelectNode{
{Label: "前端组", Value: "fe"}, {Label: "后端组", Value: "be"},
}},
{Label: "市场部", Value: "mkt"},
{Label: "法务部(停用)", Value: "legal", Disabled: true},
}},
}
var dept string
func page(c *ui.Context) {
ui.TreeSelect(c, &dept, depts).Placeholder("请选择部门").AllowClear().Width(260)
ui.Text(c, "部门:"+dept)
}节点用 []ui.TreeSelectNode 描述(Label、Value、Children、Disabled),整棵树中的 Value 必须唯一。单选时值绑定到 *string,任何一级节点都可以选中。
常见用法
多选
Multiple(&vals) 把选择绑定到 *[]string,每次点击切换一个节点,选中项以标签显示;MaxTagCount 折叠多余标签。多选时第二个参数可以传 nil。
import (
"github.com/tangoui/tango/ui"
)
var (
nodes = []ui.TreeSelectNode{{Label: "浙江", Value: "zj", Children: []ui.TreeSelectNode{
{Label: "杭州", Value: "hz"}, {Label: "宁波", Value: "nb"}, {Label: "温州", Value: "wz"},
}}}
cities []string
)
func page(c *ui.Context) {
ui.TreeSelect(c, nil, nodes).Multiple(&cities).MaxTagCount(2).Placeholder("请选择城市").Width(300)
}可勾选与勾选策略
Checkable() 在节点前显示复选框,父子联动(隐含多选)。ShowCheckedStrategy 决定以哪些节点显示标签:ui.ShowChild(默认,只显示选中的叶子)、ui.ShowParent(父节点全选时只显示父节点)、ui.ShowAll(父子都显示)。默认绑定值保存选中的叶子,Checked() 返回按策略折叠后的值;StrategyValue(true) 让绑定值本身也按策略保存(与 antd 的 value 行为一致)。
CheckStrictly() 取消父子联动:每个节点单独勾选,值就是勾选的节点本身,此时勾选策略不再生效。
import (
"strings"
"github.com/tangoui/tango/ui"
)
var (
nodes = []ui.TreeSelectNode{{Label: "浙江", Value: "zj", Children: []ui.TreeSelectNode{
{Label: "杭州", Value: "hz"}, {Label: "宁波", Value: "nb"},
}}}
scope, strict []string
)
func page(c *ui.Context) {
w := ui.TreeSelect(c, nil, nodes).Multiple(&scope).Checkable().
ShowCheckedStrategy(ui.ShowParent).DefaultExpandAll().Width(320)
ui.Text(c, "标签:"+strings.Join(w.Checked(), ", "))
ui.TreeSelect(c, nil, nodes).Multiple(&strict).CheckStrictly().Width(320)
}搜索
Searchable() 允许输入关键字过滤节点,默认按标签不区分大小写匹配,命中节点的祖先会保留并展开。FilterTreeNode 可替换匹配规则。
import (
"strings"
"github.com/tangoui/tango/ui"
)
var (
nodes = []ui.TreeSelectNode{{Label: "研发部", Value: "rd", Children: []ui.TreeSelectNode{{Label: "前端组", Value: "fe"}}}}
team string
)
func page(c *ui.Context) {
ui.TreeSelect(c, &team, nodes).Searchable().Placeholder("搜索部门")
ui.TreeSelect(c, &team, nodes).FilterTreeNode(func(q string, n ui.TreeSelectNode) bool {
return strings.HasPrefix(n.Value, q) // 按值前缀匹配
})
}展开控制
DefaultExpandAll() 首次打开时展开全部节点;ExpandedKeys(&keys) 把展开的节点绑定到 *[]string(受控),用户展开 / 收起后会写回,ExpandChanged() 报告本帧是否变化。
import (
"github.com/tangoui/tango/ui"
)
var (
nodes = []ui.TreeSelectNode{{Label: "浙江", Value: "zj", Children: []ui.TreeSelectNode{{Label: "杭州", Value: "hz"}}}}
expanded = []string{"zj"}
sel string
)
func page(c *ui.Context) {
ui.TreeSelect(c, &sel, nodes).ExpandedKeys(&expanded)
if ui.Button(c, "全部收起").Clicked() {
expanded = nil
}
}动态加载
LoadData 与 Tree 的约定相同:展开一个没有 Children 且不是 IsLeaf 的节点时,每帧调用一次函数,直到它返回 false(true 表示仍在加载,期间显示加载图标);应用在此期间把子节点追加到自己的节点切片里。
import (
"sync"
"github.com/tangoui/tango/ui"
)
var (
lazyNodes = []ui.TreeSelectNode{{Label: "浙江", Value: "zj"}, {Label: "江苏", Value: "js"}}
picked string
mu sync.Mutex
loading = map[string]bool{}
fetched = map[string][]ui.TreeSelectNode{}
)
func fetchChildren(v string) []ui.TreeSelectNode {
return []ui.TreeSelectNode{{Label: v + " 下级", Value: v + "-1", IsLeaf: true}}
}
func attach(ns []ui.TreeSelectNode, v string, kids []ui.TreeSelectNode) {
for i := range ns {
if ns[i].Value == v {
ns[i].Children = kids
return
}
attach(ns[i].Children, v, kids)
}
}
func page(c *ui.Context) {
ui.TreeSelect(c, &picked, lazyNodes).LoadData(func(v string) bool {
mu.Lock()
defer mu.Unlock()
if kids, ok := fetched[v]; ok {
attach(lazyNodes, v, kids)
delete(fetched, v)
return false // 加载完成
}
if !loading[v] {
loading[v] = true
go func() {
kids := fetchChildren(v) // 例如请求服务器
mu.Lock()
fetched[v] = kids
mu.Unlock()
}()
}
return true // 仍在加载
})
}大数据量
展开、搜索并展平后的节点超过 100 行时,下拉列表自动启用虚拟滚动,只构建视口附近的行。Virtual(true) 强制启用,Virtual(false) 关闭。ListHeight 设置虚拟下拉的最大高度,默认 256 逻辑像素。虚拟化减少行构建量,展开、搜索和勾选状态仍根据节点数据计算。
主题与样式
- 输入框外观与 Select 一致,支持
Variant(ui.FieldFilled)等外观、Size、Status("error" | "warning")。 - 下拉面板中的树节点使用
theme.Components.Tree(TreeToken)的令牌,与 Tree 组件保持一致;三套风格切换无需改代码。 ConfigProvider.Defaults("TreeSelect", ui.Props{...})支持size、status、allowClear、showSearch、placeholder、treeDefaultExpandAll、maxTagCount、virtual、listHeight、showCheckedStrategy、strategyValue。
注意事项
- 状态:选中值和(使用
ExpandedKeys时的)展开状态都在你的变量中;未受控时展开状态、搜索词由组件按调用位置保存。 - 节点数据:
Value在整棵树中必须唯一;大树请放在包级变量或缓存中,避免每帧重新构造。 - 表单校验:单选用
f.Item("部门").Rules(&dept, ui.Required("请选择部门"));多选用ui.RulesOf配合ui.RequiredItems[string]规则。 - 无障碍与键盘:Enter / 空格打开,↑ / ↓ 移动,→ 展开、← 收起,Home / End 跳到首尾,Enter / 空格选中或勾选,Backspace 删除最后一个标签;在表单项里自动以表单项标签命名。
- 截图与测试:
Open()强制展开下拉,只在截图和测试中使用。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
TreeSelect / Cascader 按勾选策略取值与受控展开
gallery 演示 TreeSelectStrategy 的 Render 函数,源码 page_shad2a_tree.go#L23。
加载中…
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.TreeSelect
func TreeSelect(c *Context, val *string, nodes []TreeSelectNode) *TreeSelectWTreeSelect adds a tree select bound to *val (single selection). Use Multiple / Checkable for multiple values.
返回 *TreeSelectW,可链式调用的方法见 TreeSelectW 方法。
TreeSelectW 方法
ui.TreeSelectW:TreeSelectW is a select whose options form a tree.
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel sets the accessible name (inside a FormItem the item label is used; the placeholder is the last resort). | A11yLabel(s string) *TreeSelectW |
AllowClear | AllowClear() *TreeSelectW | |
Changed | Changed reports a selection change this frame. | Changed() bool |
CheckStrictly | CheckStrictly unlinks parents and children in Checkable mode (Ant treeCheckStrictly, Element check-strictly; implies Checkable): every node, parent or leaf, is checked on its own, the value holds exactly the checked nodes and each one is a tag. | CheckStrictly() *TreeSelectW |
Checkable | Checkable shows checkboxes with parent/child linkage (implies Multiple; the value holds the checked leaves). | Checkable() *TreeSelectW |
Checked | Checked returns the selected values; in Checkable mode they follow the checked strategy (the values shown as tags). | Checked() []string |
DefaultExpandAll | DefaultExpandAll() *TreeSelectW | |
Disabled | Disabled disables the control. | Disabled(on bool) *TreeSelectW |
ExpandChanged | ExpandChanged reports that the bound ExpandedKeys changed this frame. | ExpandChanged() bool |
ExpandedKeys | ExpandedKeys binds the expanded nodes of the dropdown to *keys (controlled treeExpandedKeys): the tree shows exactly these nodes open, and expanding or collapsing (pointer, keyboard, opening on the current value, search) writes the new list back in tree order. | ExpandedKeys(keys *[]string) *TreeSelectW |
FilterTreeNode | FilterTreeNode replaces the search match (by default the label containing the typed text, case-insensitively) with fn, which receives the typed text and a node (Ant filterTreeNode, Element filter-node-method). | FilterTreeNode(fn func(query string, node TreeSelectNode) bool) *TreeSelectW |
IsOpen | IsOpen reports whether the dropdown is shown. | IsOpen() bool |
Layout | Layout(o ...layout.Option) *TreeSelectW | |
ListHeight | ListHeight sets the maximum dropdown height with or without virtual scrolling (Ant listHeight, 256). | ListHeight(px float32) *TreeSelectW |
LoadData | LoadData enables lazy loading with Tree's contract: expanding a node that has no Children and is not IsLeaf calls fn(value) every frame until it returns false (true = still loading); the application appends the children to its node slice meanwhile. | LoadData(fn func(value string) bool) *TreeSelectW |
MaxTagCount | MaxTagCount(n int) *TreeSelectW | |
MaxTagPlaceholder | MaxTagPlaceholder builds the text of the collapsed tag from the hidden labels (Ant maxTagPlaceholder; default "+ N ..."). | MaxTagPlaceholder(fn func(omitted []string) string) *TreeSelectW |
Multiple | Multiple binds the selection to *vals instead (each click toggles a node). | Multiple(vals *[]string) *TreeSelectW |
NotFoundContent | NotFoundContent sets the text of an empty dropdown (Ant notFoundContent, Element no-data-text). | NotFoundContent(s string) *TreeSelectW |
Open | Open forces the dropdown open (screenshots, tests). | Open() *TreeSelectW |
Placeholder | Placeholder(s string) *TreeSelectW | |
Placement | Placement sets where the dropdown opens: BottomStart (default), BottomEnd, TopStart or TopEnd (Ant placement, Element placement); it flips when there is no room. | Placement(p Placement) *TreeSelectW |
Searchable | Searchable() *TreeSelectW | |
ShowCheckedStrategy | ShowCheckedStrategy chooses the tags of Checkable mode (default ShowChild: checked leaves; ShowParent: a fully checked parent replaces its children; ShowAll: every checked node). | ShowCheckedStrategy(s CheckedStrategy) *TreeSelectW |
Size | Size(s ControlSize) *TreeSelectW | |
Status | Status(s string) *TreeSelectW | |
StrategyValue | StrategyValue makes the bound value of Checkable mode follow the checked strategy, as Ant's value does: with ShowParent a fully checked parent is stored instead of its leaves, with ShowAll parents and leaves are both stored. | StrategyValue(on bool) *TreeSelectW |
Variant | Variant sets the frame look (FieldOutlined by default). | Variant(v FieldVariant) *TreeSelectW |
Virtual | Virtual forces the virtual dropdown on or off (default: more than 100 visible rows). | Virtual(on bool) *TreeSelectW |
Width | Width(px float32) *TreeSelectW |
TreeSelectNode 参数类型
ui.TreeSelectNode:TreeSelectNode is a node of a TreeSelect tree. Values must be unique. With LoadData, a node without Children that is not IsLeaf loads its children when expanded.
| 字段 | 说明 | 类型 |
|---|---|---|
Label | string | |
Value | string | |
Disabled | bool | |
Children | []TreeSelectNode | |
IsLeaf | never expandable (relevant with LoadData) | bool |