Skip to content

Tree 树形控件 ​

多层级的结构列表:展开收起、选择、勾选、搜索、拖拽和异步加载子节点。

何时使用 ​

  • 文件夹、组织架构、地区、分类目录等天然有层级的数据。
  • 需要在层级中选择或勾选节点(例如分配权限)时;勾选父节点会联动子节点。
  • 在表单里从树中选一个值、收起成下拉框,用 TreeSelect 树选择;层级数据同时有多列字段时,用 Table 表格 的 Tree 树形数据;只是两三层的导航菜单,用 Menu 导航菜单。

在线体验 ​

示例包含可勾选(含半选)、连接线与图标、搜索高亮与拖拽、异步加载,以及 1000 个一级节点(每个 5 个子节点)的虚拟滚动。

Tree 树形控件 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

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

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

var orgNodes = []ui.TreeNode{
    {Key: "hq", Title: "总部", Children: []ui.TreeNode{
        {Key: "rd", Title: "研发中心", Children: []ui.TreeNode{
            {Key: "fe", Title: "前端组"},
            {Key: "be", Title: "后端组"},
        }},
        {Key: "ops", Title: "运营中心", Children: []ui.TreeNode{
            {Key: "growth", Title: "增长组"},
        }},
    }},
}

var (
    orgExpanded = []string{"hq", "rd"}
    orgSelected []string
)

func page(c *ui.Context) {
    t := ui.Tree(c, orgNodes).Expanded(&orgExpanded).Selected(&orgSelected).Show()
    if t.SelectChanged() && len(orgSelected) > 0 {
        ui.Text(c, "选中:"+orgSelected[0])
    }
}

ui.Tree(c, nodes) 接收一棵 TreeNode 树,链式设置后调用 Show()。每个节点的 Key 在整棵树中必须唯一,展开、选择、勾选都按 Key 记录。Expanded、Selected 把对应的键列表绑定到应用变量;不绑定时树自己保存这些状态。SelectChanged()、ExpandChanged()、CheckChanged() 报告本帧的变化。

常见用法 ​

以下片段沿用基本用法里的 orgNodes,省略了 import。

可勾选 ​

Checkable(&checked) 显示复选框并绑定勾选的键:勾选父节点会勾选全部子节点,部分子节点勾选时父节点显示半选,HalfChecked() 在 Show 之后返回半选的键。CheckStrictly() 取消父子联动,每个节点独立勾选。节点的 Disabled 禁用整个节点,DisableCheckbox 只禁用复选框。

go
var checked = []string{"fe"}

func checkTree(c *ui.Context) {
    t := ui.Tree(c, orgNodes).Checkable(&checked).Show()
    if t.CheckChanged() {
        ui.Text(c, "半选:"+fmt.Sprint(t.HalfChecked())).Tertiary()
    }
}

多选、连接线与图标 ​

Multiple() 允许同时选中多个节点;ShowLine() 显示层级连接线;ShowIcon() 在标题前显示图标,节点 Icon 为 nil 时使用文件夹 / 文件图标;BlockNode() 让节点标题占满整行,便于点击。

go
var (
    multiSel  []string
    lineOpen  = []string{"hq", "rd", "ops"}
)

func lineTree(c *ui.Context) {
    ui.Tree(c, orgNodes).Expanded(&lineOpen).Selected(&multiSel).
        Multiple().ShowLine().ShowIcon().BlockNode().Show()
}

搜索与过滤 ​

Search(keyword) 高亮包含关键字的标题,关键字变化时自动展开所有匹配节点的父节点;再加 Filter() 会隐藏既不匹配、也不包含匹配子节点的节点(对应 Element Plus 的 filter-node-method)。

go
var (
    keyword    string
    searchOpen []string
)

func searchTree(c *ui.Context) {
    ui.Input(c, &keyword).Placeholder("搜索部门")
    ui.Tree(c, orgNodes).Expanded(&searchOpen).Search(keyword).Filter().Show()
}

拖拽排序 ​

Draggable() 允许拖动节点。树不会自己改数据:在 Dropped() 报告完成一次拖放后,用 ui.TreeMove 得到移动后的新树并写回。位置 pos 为 ui.TreeDropBefore、ui.TreeDropInside(成为第一个子节点)或 ui.TreeDropAfter。

go
func dragTree(c *ui.Context) {
    t := ui.Tree(c, orgNodes).Draggable().BlockNode().Show()
    if drag, drop, pos, ok := t.Dropped(); ok {
        orgNodes = ui.TreeMove(orgNodes, drag, drop, pos)
    }
}

异步加载 ​

LoadData(fn) 开启懒加载:展开一个没有 Children、也没有标记 IsLeaf 的节点时,树每帧调用 fn(key),返回 true 表示仍在加载(显示转圈),返回 false 表示结束。在 fn 里发起一次请求,数据到达后把子节点挂到对应节点上。

go
var (
    lazyNodes = []ui.TreeNode{
        {Key: "cn", Title: "中国"},
        {Key: "other", Title: "其他", IsLeaf: true},
    }
    pending = map[string]bool{}
    arrived = map[string][]ui.TreeNode{} // 后台请求写入,UI 线程读取
)

// requestChildren 在 goroutine 里请求子节点,结果经 Runner.Post 在窗口的 UI 线程上写入 arrived[key]。
func requestChildren(key string) {}

func lazyTree(c *ui.Context) {
    ui.Tree(c, lazyNodes).LoadData(func(key string) bool {
        if kids, ok := arrived[key]; ok {
            for i := range lazyNodes {
                if lazyNodes[i].Key == key {
                    lazyNodes[i].Children = kids
                }
            }
            return false
        }
        if !pending[key] {
            pending[key] = true
            requestChildren(key)
        }
        return true
    }).Show()
}

大数据量:虚拟滚动 ​

Height(px) 设置固定高度的滚动视口,只构建视口及邻近缓冲区内的行。Built() 返回本次 Show() 实际构建的行数。单独设置 Height 时,每帧仍会遍历节点并展平可见行。

同时设置 DataVersion(v) 后,树会缓存父节点表、Filter 的可见集合、展平后的可见行和勾选状态。数据和展开、搜索、勾选状态稳定时,每帧开销主要随视口大小变化。原地增删或替换子节点、修改键、改变 Filter 的匹配结果,或修改影响勾选联动的 Disabled / DisableCheckbox 时,请递增 v 或换用新的顶层切片。懒加载原地写回子节点也要更新版本。

go
var bigOpen []string

func bigTree(c *ui.Context, nodes []ui.TreeNode, version int) {
    ui.Tree(c, nodes).Expanded(&bigOpen).Height(320).DataVersion(version).A11yLabel("部门树").Show()
}

主题与样式 ​

风格差异
Ant Design行高 24、行间距 4、每级缩进 24;选中节点浅色底、小圆角;搜索命中为橙红色文字。
Element Plus对照 el-tree:行高 26 无间距、缩进 18;选中行主色浅底且为直角;默认点击标题即展开(ExpandOnClick)。
shadcn侧边栏文件树样式:行高 28、缩进 16;选中项加粗(500);搜索命中用浅色底标记;连接线为每级一条左边线。

组件令牌在 theme.Components.Tree(theme.TreeToken):TitleHeight、NodeGap、IndentSize、SwitcherSize、NodeBorderRadius、NodeHoverBg、NodeSelectedBg、NodeSelectedColor、LineColor、SearchHighlightColor、SearchHighlightBg、DropIndicatorColor 等。ExpandOnClick(on) 可以在任何风格下单独开关「点击标题展开」。

ConfigProvider.Defaults("Tree", ...) 的布尔键包括 showLine、showIcon、blockNode、checkable、multiple、draggable、selectable、defaultExpandAll、accordion、checkOnClickNode 和 checkOnClickLeaf。height 设置虚拟滚动视口高度,单位为逻辑像素。

go
func treeDefaults(c *ui.Context) {
    ui.ConfigProvider(c).Defaults("Tree", ui.Props{"showLine": true, "blockNode": true}).Body(func() {
        ui.Tree(c, orgNodes).Show()
    })
}

注意事项 ​

  • Key 唯一:节点 Key 在整棵树里必须唯一,同一棵树不要出现两个相同的键。
  • 状态放在哪:需要在别处读取或预设展开、选择、勾选时,用 Expanded、Selected、Checkable 绑定应用变量;不绑定时状态由树按自身的组件 ID 保存。条件分支或循环里渲染多棵树时,用 ui.Key(c, key, fn) 区分。
  • 数据不可变:拖拽、懒加载等改变结构的操作都由应用改 nodes;ui.TreeMove 返回新切片,不修改原树。
  • 性能:节点多于几百个时,可设置 Height 并按需提供 DataVersion。超大树需要自定义行内容、按键定位或查询构建范围时,可用 VirtualTree。
  • 键盘与无障碍:整棵树是一个 Tab 停靠点。↑ / ↓ 移动焦点行,Home / End 跳到首行 / 末行,→ 展开(或进入第一个子节点),← 收起(或回到父节点),Enter 选择,空格在可勾选时勾选、否则选择。用 A11yLabel 为树命名(放在 FormItem 里时自动使用表单项标签)。
  • RTL:缩进、展开箭头和连接线在 RTL 布局中自动镜像。

示例代码 ​

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

Tree 树形控件

gallery 演示 Tree 的 Render 函数,源码 page_data_tree.go#L60。

加载中…

Tree 虚拟滚动(Height)

gallery 演示 TreeVirtual 的 Render 函数,源码 page_shad2b_variants.go#L79。

加载中…

Tree 规范对齐

gallery 演示 TreeAlign 的 Render 函数,源码 page_b2f.go#L134。

加载中…

API ​

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

入口函数 ​

ui.Tree ​

go
func Tree(c *Context, nodes []TreeNode) *TreeW

Tree starts a tree over nodes; configure with modifiers and call Show.

The tree is one Tab stop. Keyboard: Up/Down move the focused row, Home/End jump to the first/last row, Right expands (or enters the first child), Left collapses (or returns to the parent), Enter selects, Space checks when Checkable and selects otherwise.

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

TreeW 方法 ​

ui.TreeW:TreeW is a hierarchical list with expand, select, check, drag and search.

方法说明签名
A11yLabelA11yLabel sets the tree's accessible name (inside a FormItem the item label is used).A11yLabel(s string) *TreeW
AccordionAccordion keeps one node expanded per level: expanding a node collapses its expanded siblings (Element accordion).Accordion() *TreeW
AllowDragAllowDrag decides which nodes can be dragged (antd draggable.nodeDraggable, Element allow-drag).AllowDrag(fn func(key string) bool) *TreeW
AllowDropAllowDrop decides where a dragged node may land (antd allowDrop, Element allow-drop); pos is TreeDropBefore / Inside / After.AllowDrop(fn func(drag, drop string, pos int) bool) *TreeW
BlockNodeBlockNode stretches titles to the full row width.BlockNode() *TreeW
BuiltBuilt returns how many rows the last Show built.Built() int
CheckChangedCheckChanged() bool
CheckOnClickCheckOnClick toggles the checkbox when a checkable node's title is clicked (Element check-on-click-node).CheckOnClick(on bool) *TreeW
CheckOnClickLeafCheckOnClickLeaf toggles the checkbox when a checkable leaf's title is clicked (Element check-on-click-leaf, on by default there; antd selects instead, so it is off by default here: turn it on per tree or with Defaults("Tree", Props{"checkOnClickLeaf": true})).CheckOnClickLeaf(on bool) *TreeW
CheckStrictlyCheckStrictly disables parent/child check cascading.CheckStrictly() *TreeW
CheckableCheckable shows checkboxes bound to checked keys.Checkable(checked *[]string) *TreeW
DataVersionDataVersion turns on the cache for a virtual scrolling tree and tells it that its nodes were modified in place.DataVersion(v int) *TreeW
DefaultExpandAllDefaultExpandAll expands every parent node the first time the tree is shown (antd defaultExpandAll, Element default-expand-all).DefaultExpandAll() *TreeW
DisabledDisabled disables the whole tree (antd disabled): no selection, expansion, checking, dragging or keyboard focus.Disabled(on bool) *TreeW
DraggableDraggable enables drag-to-reorder; read the result with Dropped.Draggable() *TreeW
DroppedDropped reports a completed drag in the last Show: the dragged key, the key it was dropped on and the position (TreeDropBefore/Inside/After).Dropped() (drag, drop string, pos int, ok bool)
EmptyTextEmptyText shows s when the tree has no rows to show (Element empty-text; antd shows nothing).EmptyText(s string) *TreeW
ExpandChangedExpandChanged() bool
ExpandOnClickExpandOnClick toggles expansion when the title is clicked (Element's expand-on-click-node, on by default with the Element theme).ExpandOnClick(on bool) *TreeW
ExpandedExpanded binds the expanded keys to the app.Expanded(keys *[]string) *TreeW
FilterFilter hides nodes that neither match the search keyword nor contain a match (Element's filter-node-method).Filter() *TreeW
FilterMethodFilterMethod replaces the title-contains match used by Search and Filter (antd filterTreeNode, Element filter-node-method).FilterMethod(fn func(keyword string, n *TreeNode) bool) *TreeW
HalfCheckedHalfChecked returns the keys drawn half-checked after the last Show.HalfChecked() []string
HeightHeight turns the tree into a scrolling viewport of px.Height(px float32) *TreeW
LayoutLayout applies layout options to the tree (after Show).Layout(o ...layout.Option) *TreeW
LoadDataLoadData enables lazy loading: expanding a non-leaf node without children calls fn(key) every frame until it returns false (still loading = true); a spinner replaces the switcher meanwhile.LoadData(fn func(key string) bool) *TreeW
MultipleMultiple allows selecting several nodes.Multiple() *TreeW
SearchSearch highlights titles containing keyword and, when the keyword changes, expands the parents of every match.Search(keyword string) *TreeW
SelectChangedSelectChanged / CheckChanged / ExpandChanged report changes in the last Show.SelectChanged() bool
SelectableSelectable turns selection on or off (antd selectable, default true).Selectable(on bool) *TreeW
SelectedSelected binds the selected keys to the app.Selected(keys *[]string) *TreeW
ShowShow builds the tree.Show() *TreeW
ShowIconShowIcon draws node icons before titles.ShowIcon() *TreeW
ShowLineShowLine draws connecting lines (Ant).ShowLine() *TreeW
SwitcherIconSwitcherIcon replaces the expand switcher (antd switcherIcon, Element icon).SwitcherIcon(ic *icon.Icon) *TreeW

TreeNode 参数类型 ​

ui.TreeNode:TreeNode is one node of a Tree. Keys must be unique in the whole tree.

字段说明类型
Keystring
Titlestring
Children[]TreeNode
IsLeafnever expandable (relevant with LoadData)bool
Disabledbool
DisableCheckboxbool
Iconshown with ShowIcon; nil uses folder/file*icon.Icon

相关链接 ​

以 MIT 许可发布