ant-design/components/tree/index.zh-CN.md

97 lines
5.5 KiB
Markdown
Raw Normal View History

2016-03-31 14:17:09 +08:00
---
category: Components
type: 数据展示
2016-07-21 10:15:00 +08:00
title: Tree
subtitle: 树形控件
2016-03-31 14:17:09 +08:00
---
2015-08-01 14:08:04 +08:00
多层次的结构列表。
2015-08-05 21:18:34 +08:00
## 何时使用
文件夹、组织架构、生物分类、国家地区等等,世间万物的大多数结构都是树形结构。使用 `树控件` 可以完整展现其中的层级关系,并具有展开收起选择等交互功能。
2015-08-06 09:17:40 +08:00
## API
2015-08-15 23:45:25 +08:00
### Tree props
| 参数 | 说明 | 类型 | 默认值 | 版本 |
| --- | --- | --- | --- | --- |
| autoExpandParent | 是否自动展开父节点 | boolean | true | |
| blockNode | 是否节点占据一行 | boolean | false | 3.15.0 |
| checkable | 节点前添加 Checkbox 复选框 | boolean | false | |
| checkedKeys | (受控)选中复选框的树节点(注意:父子节点有关联,如果传入父节点 key则子节点自动选中相应当子节点 key 都传入,父节点也自动选中。当设置`checkable`和`checkStrictly`,它是一个有`checked`和`halfChecked`属性的对象,并且父子节点的选中与否不再关联 | string\[] \| {checked: string\[], halfChecked: string\[]} | \[] | |
| checkStrictly | checkable 状态下节点选择完全受控(父子节点选中状态不再关联) | boolean | false | |
| defaultCheckedKeys | 默认选中复选框的树节点 | string\[] | \[] | |
| defaultExpandAll | 默认展开所有树节点 | boolean | false | |
| defaultExpandedKeys | 默认展开指定的树节点 | string\[] | \[] | |
| defaultExpandParent | 默认展开父节点 | bool | true | 3.4.0 |
| defaultSelectedKeys | 默认选中的树节点 | string\[] | \[] | |
| disabled | 将树禁用 | bool | false | 3.4.0 |
| draggable | 设置节点可拖拽IE>8 | boolean | false | |
| expandedKeys | (受控)展开指定的树节点 | string\[] | \[] | |
| filterTreeNode | 按需筛选树节点(高亮),返回 true | function(node) | - | |
| loadData | 异步加载数据 | function(node) | - | |
| loadedKeys | (受控)已经加载的节点,需要配合 `loadData` 使用 | string\[] | \[] | 3.7.0 |
| multiple | 支持点选多个节点(节点本身) | boolean | false | |
2019-09-09 18:24:20 +08:00
| selectable | 是否可选中 | boolean | true | |
| selectedKeys | (受控)设置选中的树节点 | string\[] | - | |
| showIcon | 是否展示 TreeNode title 前的图标,没有默认样式,如设置为 true需要自行定义图标相关样式 | boolean | false | |
| switcherIcon | 自定义树节点的展开/折叠图标 | React.ReactElement | - | 3.12.0 |
| showLine | 是否展示连接线 | boolean | false | |
| onCheck | 点击复选框触发 | function(checkedKeys, e:{checked: bool, checkedNodes, node, event}) | - | |
| onDragEnd | dragend 触发时调用 | function({event, node}) | - | |
| onDragEnter | dragenter 触发时调用 | function({event, node, expandedKeys}) | - | |
| onDragLeave | dragleave 触发时调用 | function({event, node}) | - | |
| onDragOver | dragover 触发时调用 | function({event, node}) | - | |
| onDragStart | 开始拖拽时调用 | function({event, node}) | - | |
| onDrop | drop 触发时调用 | function({event, node, dragNode, dragNodesKeys}) | - | |
| onExpand | 展开/收起节点时触发 | function(expandedKeys, {expanded: bool, node}) | - | |
| onLoad | 节点加载完毕时触发 | function(loadedKeys, {event, node}) | - | 3.7.0 |
| onRightClick | 响应右键点击 | function({event, node}) | - | |
| onSelect | 点击树节点触发 | function(selectedKeys, e:{selected: bool, selectedNodes, node, event}) | - | |
| treeData | treeNodes 数据,如果设置则不需要手动构造 TreeNode 节点key 在整个树范围内唯一) | array\<{key, title, children, \[disabled, selectable]}> | - | 3.19.8 |
2015-08-15 23:45:25 +08:00
### TreeNode props
meger feature to master (#16421) * use ul in list * update snapshot * update comment * feat: TreeSelect support `showSearch` in multiple mode (#15933) * update rc-tree-select * typo * update desc & snapshot * update desc & snapshot * check default showSearch * feat: table customizing variable (#15971) * feat: added table selected row color variable * fix: @table-selected-row-color default is inherit * feat: Upload support customize previewFile (#15984) * support preview file * use promise * dealy load * use canvas of render * use domHook of test * update demo * add snapshot * update types * update testcase * feat: form customizing variables (#15954) * fix: added styling form input background-color * feat: added '@form-warning-input-bg' variable * feat: added '@form-error-input-bg' variable * use li wrap with comment * feat: Support append theme less file with less-variable (#16118) * add override * add override support * update doc * feat: dropdown support set right icon * docs: update doc of dropdown component * style: format dropdown-button.md * test: update updateSnapshot * style: format dropdown-button.md * test: update updateSnapshot * test: update updateSnapshot * style: change style of dropdown-button demo * fix: fix document table order * feat: Support SkeletonAvatarProps.size accept number (#16078) (#16128) * chore:update style of demo * feat: Notification functions accept top, bottom and getContainer as arguments * drawer: add afterVisibleChange * rm onVisibleChange * update * feat: 🇭🇷 hr_HR locale (#16258) * Added Croatian locale * fixed lint error * :white_check_mark: Add test cases for hr_HR * :memo: update i18n documentation * feat: add `htmlFor` in Form.Item (#16278) * add htmlFor in Form.Item * update doc * feat: Button support `link` type (#16289) close #15892 * feat: Add Timeline.Item.position (#16148) (#16193) * fix: Timeline.pendingDot interface documentation there is a small problem (#16177) * feat: Add Timeline.Item.position (#16148) * doc: add version infomation for Timeline.Item.position * refactor: Update Tree & TreeSelect deps (#16330) * use CSSMotion * update snapshot * feat: Collapse support `expandIconPosition` (#16365) * update doc * support expandIconPosition * update snapshot * feat: Breadcrumb support DropDown (#16315) * breadcrumbs support drop down menu * update doc * add require less * fix test * fix md doc * less code * fix style warning * update snap * add children render test * feat: TreeNode support checkable * feat: add optional to support top and left slick dots (#16186) (#16225) * add optional to support top and left slick dots * update carousel snapshot * Update doc, add placement demo * update carousel placement demo snapshots * rename dots placement to position * update vertical as deprecated * rename dotsPosition to dotPosition * refine code * add warning testcase for vertical * remove unused warning * update expression * Additional test case for dotPosition * refactor: Upgrade `rc-tree-select` to support pure React motion (#16402) * upgrade `rc-tree-select` * update snapshot * 3.17.0 changelog * fix warning * fix review warning
2019-05-06 12:04:39 +08:00
| 参数 | 说明 | 类型 | 默认值 | 版本 |
| --- | --- | --- | --- | --- |
| checkable | 当树为 checkable 时,设置独立节点是否展示 Checkbox | boolean | - | 3.17.0 |
2019-05-07 14:57:32 +08:00
| disableCheckbox | 禁掉 checkbox | boolean | false | |
| disabled | 禁掉响应 | boolean | false | |
| icon | 自定义图标。可接收组件props 为当前节点 props | ReactNode/Function(props):ReactNode | - | 3.4.0 |
2019-05-07 14:57:32 +08:00
| isLeaf | 设置为叶子节点(设置了`loadData`时有效) | boolean | false | |
| key | 被树的 (default)ExpandedKeys / (default)CheckedKeys / (default)SelectedKeys 属性所用。注意:整个树范围内的所有节点的 key 值不能重复! | string | 内部计算出的节点位置 | |
| selectable | 设置节点是否可被选中 | boolean | true | |
| title | 标题 | string\|ReactNode | '---' | |
2016-07-25 12:15:59 +08:00
### DirectoryTree props
| 参数 | 说明 | 类型 | 默认值 | 版本 |
| ------------ | ---------------------------------------------------- | ------ | ------ | ----- |
| expandAction | 目录展开逻辑,可选 `false` `'click'` `'doubleClick'` | string | click | 3.7.0 |
2016-07-25 12:15:59 +08:00
## 注意
2019-05-07 14:57:32 +08:00
`3.4.0` 之前:树节点可以有很多,但在设置`checkable`时,将会花费更多的计算时间,因此我们缓存了一些计算结果(`this.treeNodesStates`)来复用,避免多次重复计算,以此提高性能。但这也带来了一些限制,当你异步加载树节点时,你需要这样渲染树:
2017-01-06 10:46:25 +08:00
```jsx
2019-05-07 14:57:32 +08:00
{
this.state.treeData.length ? (
<Tree>
{this.state.treeData.map(data => (
<TreeNode />
))}
</Tree>
) : (
'loading tree'
);
}
2017-01-06 10:46:25 +08:00
```
2019-01-26 19:46:30 +08:00
## FAQ
### 在 showLine 时,如何隐藏子节点图标?
文件图标通过 switcherIcon 来实现如果不需要你可以覆盖对应的样式https://codesandbox.io/s/883vo47xp8