Skip to content

Tree 树形控件

树形控件用于展示层次结构数据,支持展开折叠、复选框选择、右键菜单等功能。

引入

html
<script type="module">
  import "./node_modules/easy-component-ui/dist/components/ea-tree.js";
</script>
js
import "easy-component-ui/ea-tree";

自定义样式

移步到 CSS PartCSS Custom Properties

基础用法

最基本的树形控件用法,展示层次结构数据。

查看代码
html
<div class="demo">
  <ea-tree id="basicTree"></ea-tree>
</div>
js
const data = [
  {
    label: "Level one 1",
    children: [
      {
        label: "Level two 1-1",
        children: [
          {
            label: "Level three 1-1-1",
          },
        ],
      },
    ],
  },
  {
    label: "Level one 2",
    children: [
      {
        label: "Level two 2-1",
        children: [
          {
            label: "Level three 2-1-1",
          },
        ],
      },
      {
        label: "Level two 2-2",
        children: [
          {
            label: "Level three 2-2-1",
          },
        ],
      },
    ],
  },
];

const basicTree = document.querySelector("#basicTree");
basicTree.data = data;

可选择

通过设置 show-checkbox 属性启用复选框选择功能。

查看代码
html
<div class="demo">
  <ea-tree id="checkboxTree" show-checkbox></ea-tree>
</div>
js
const data = [
  {
    label: "Level one 1",
    children: [
      {
        label: "Level two 1-1",
        children: [
          {
            label: "Level three 1-1-1",
          },
        ],
      },
    ],
  },
  {
    label: "Level one 2",
    children: [
      {
        label: "Level two 2-1",
        children: [
          {
            label: "Level three 2-1-1",
          },
        ],
      },
      {
        label: "Level two 2-2",
        children: [
          {
            label: "Level three 2-2-1",
          },
        ],
      },
    ],
  },
];

const checkboxTree = document.querySelector("#checkboxTree");
checkboxTree.data = data;

禁用复选框

支持禁用特定节点,通过数据中的 disabled 属性控制。

查看代码
html
<div class="demo">
  <ea-tree id="disabledCheckboxTree" show-checkbox></ea-tree>
</div>
js
const idData = [
  {
    id: 1,
    label: "Level one 1",
    children: [
      {
        id: 3,
        label: "Level two 2-1",
        children: [
          {
            id: 4,
            label: "Level three 3-1-1",
          },
          {
            id: 5,
            label: "Level three 3-1-2",
            disabled: true,
          },
        ],
      },
      {
        id: 2,
        label: "Level two 2-2",
        disabled: true,
        children: [
          {
            id: 6,
            label: "Level three 3-2-1",
          },
          {
            id: 7,
            label: "Level three 3-2-2",
            disabled: true,
          },
        ],
      },
    ],
  },
];

const disabledCheckboxTree = document.querySelector("#disabledCheckboxTree");
disabledCheckboxTree.data = idData;

点击图标展开

通过设置 expand-on-icon-click 属性,可以只在点击节点图标时展开/收起子节点,点击节点文字时仅选中节点而不展开。

查看代码
html
<div class="demo">
  <ea-tree id="expandOnIconClickTree" expand-on-icon-click></ea-tree>
</div>
js
const data = [
  {
    label: "Level one 1",
    children: [
      {
        label: "Level two 1-1",
        children: [
          {
            label: "Level three 1-1-1",
          },
        ],
      },
    ],
  },
  {
    label: "Level one 2",
    children: [
      {
        label: "Level two 2-1",
        children: [
          {
            label: "Level three 2-1-1",
          },
        ],
      },
      {
        label: "Level two 2-2",
        children: [
          {
            label: "Level three 2-2-1",
          },
        ],
      },
    ],
  },
];

const expandOnIconClickTree = document.querySelector("#expandOnIconClickTree");
expandOnIconClickTree.data = data;

默认展开以及默认选中

通过 defaultExpandedKeysdefaultCheckedKeys 属性设置默认展开和选中的节点。需要配合 node-key 属性使用。

查看代码
html
<div class="demo">
  <ea-tree id="defaultExpandedTree" node-key="id" show-checkbox></ea-tree>
</div>
js
const idData = [
  {
    id: 1,
    label: "Level one 1",
    children: [
      {
        id: 3,
        label: "Level two 2-1",
        children: [
          {
            id: 4,
            label: "Level three 3-1-1",
          },
          {
            id: 5,
            label: "Level three 3-1-2",
            disabled: true,
          },
        ],
      },
      {
        id: 2,
        label: "Level two 2-2",
        disabled: true,
        children: [
          {
            id: 6,
            label: "Level three 3-2-1",
          },
          {
            id: 7,
            label: "Level three 3-2-2",
            disabled: true,
          },
        ],
      },
    ],
  },
];

const defaultExpandedTree = document.querySelector("#defaultExpandedTree");
defaultExpandedTree.defaultExpandedKeys = [1, 2];
defaultExpandedTree.defaultCheckedKeys = [3, 4];
defaultExpandedTree.data = idData;

Tree API

Tree Attributes

参数说明类型可选值默认值
show-checkbox是否显示复选框booleanfalse
check-strictly是否严格模式(不关联父子节点)booleanfalse
node-key节点唯一标识字段名string
expand-on-icon-click是否只在点击图标时展开/收起节点booleanfalse
data prop树形数据array[]
dataProps prop数据字段配置object{ children: "children", label: "label", disabled: "disabled" }
defaultExpandedKeys prop默认展开的节点键值数组array[]
defaultCheckedKeys prop默认选中的节点键值数组array[]

Tree Methods

方法名说明参数
getHalfCheckedNodes获取半选中节点数据() => Array<any>
getHalfCheckedKeys获取半选中节点键值() => Array<any>
getCurrentKey获取当前选中节点键值() => any
getCurrentNode获取当前选中节点数据() => any
updateKeyChildren更新节点键值的子节点数据(key: any, data: Array<any>) => boolean
getCheckedNodes获取选中节点数据(leafOnly?: boolean, includeHalfChecked?: boolean) => Array<any>
setCheckedNodes设置选中节点数据(nodes: Array<any>) => boolean
getCheckedKeys获取选中节点键值(leafOnly?: boolean) => Array<any>
setCheckedKeys设置选中节点键值(keys: Array<any>, leafOnly?: boolean) => boolean
setChecked设置节点选中状态(keyOrData: any, checked: boolean) => boolean
setCurrentKey设置当前选中节点键值(key: any, shouldAutoExpandParent?: boolean) => boolean
setCurrentNode设置当前选中节点数据(node: any, shouldAutoExpandParent?: boolean) => boolean
getNode获取节点信息(data: any) => any

Tree Events

事件名说明回调参数(event.detail)
ea-node-click节点点击事件{ data: any }
ea-node-contextmenu节点右键菜单事件{ data: any, node: any }
ea-node-select节点选中事件{ node: any, selected: boolean }
ea-check-change复选框状态变化事件{ data: any, checked: boolean, hasCheckedChildren: boolean }
ea-check复选框选中事件{ data: any, checkedState: { checkedNodes, checkedKeys, halfCheckedNodes, halfCheckedKeys } }
ea-current-change当前选中节点变化事件{ data: any, node: any }
ea-node-expand节点展开事件{ data: any, node: any, expanded: true }
ea-node-collapse节点收起事件{ data: any, node: any, expanded: false }

Tree CSS Part

用法可参考 MDN ::part()伪类

名称说明
container树容器

Tree Slots

名称说明
default默认插槽

Tree CSS Custom Properties

属性名说明默认值
--ea-tree-font-size字体大小var(--font-size-md)
--ea-tree-text-color文本颜色var(--grey-700)
--ea-tree-toggle-size展开图标尺寸16px
--ea-tree-cursor光标样式var(--cursor-pointer)
--ea-tree-padding-vertical垂直内边距var(--spacing-sm)
--ea-tree-padding-horizontal水平内边距var(--spacing-md)
--ea-tree-border-radius圆角大小var(--border-radius-sm)
--ea-tree-transition过渡动画时长var(--transition-normal)
--ea-tree-hover-bg悬停背景色var(--grey-100)
--ea-tree-selected-bg选中背景色var(--blue-100)
--ea-tree-selected-text-color选中文本色var(--blue-600)
--ea-tree-icon-color图标颜色var(--grey-500)
--ea-tree-icon-hover-color图标悬停颜色var(--blue-500)
--ea-tree-indent-size缩进大小16px