Tree 树形控件

何时使用

文件夹、组织架构、生物分类、国家地区等等,世间万物的大多数结构都是树形结构。使用树控件可以完整展现其中的层级关系,并具有展开收起选择等交互功能。

代码演示

绑定数据源

绑定数据源。

Tree树形控件 - 图1

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { DataSet, Tree } from 'choerodon-ui/pro';
  4. function nodeRenderer({ record }) {
  5. return record.get('text');
  6. }
  7. class App extends React.Component {
  8. ds = new DataSet({
  9. primaryKey: 'id',
  10. queryUrl: '/tree.mock',
  11. autoQuery: true,
  12. parentField: 'parentId',
  13. expandField: 'expand',
  14. idField: 'id',
  15. fields: [
  16. { name: 'id', type: 'number' },
  17. { name: 'expand', type: 'boolean' },
  18. { name: 'parentId', type: 'number' },
  19. ],
  20. events: {
  21. select: ({ record, dataSet }) => console.log('select', record, dataSet),
  22. unSelect: ({ record, dataSet }) => console.log('unSelect', record, dataSet),
  23. },
  24. });
  25. render() {
  26. return (
  27. <Tree
  28. dataSet={this.ds}
  29. checkable
  30. renderer={nodeRenderer}
  31. />
  32. );
  33. }
  34. }
  35. ReactDOM.render(
  36. <App />,
  37. document.getElementById('container')

check和expand使用

expand 和 check 可以配合dataSet的使用方法

Tree树形控件 - 图2

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { DataSet, Tree } from 'choerodon-ui/pro';
  4. import { Row, Col } from 'choerodon-ui';
  5. function nodeRenderer({ record }) {
  6. return record.get('text');
  7. }
  8. class App extends React.Component {
  9. ds = new DataSet({
  10. primaryKey: 'id',
  11. queryUrl: '/tree-less.mock',
  12. autoQuery: true,
  13. expandField: 'expand',
  14. checkField: 'ischecked',
  15. parentField: 'parentId',
  16. idField: 'id',
  17. fields: [
  18. { name: 'id', type: 'number' },
  19. { name: 'expand', type: 'boolean' },
  20. { name: 'parentId', type: 'number' },
  21. ],
  22. events: {
  23. select: ({ record, dataSet }) => console.log('select', record, dataSet),
  24. unSelect: ({ record, dataSet }) =>
  25. console.log('unSelect', record, dataSet),
  26. },
  27. });
  28. dsDefault = new DataSet({
  29. primaryKey: 'id',
  30. queryUrl: '/tree-less.mock',
  31. autoQuery: true,
  32. parentField: 'parentId',
  33. idField: 'id',
  34. fields: [
  35. { name: 'id', type: 'number' },
  36. { name: 'expand', type: 'boolean' },
  37. { name: 'parentId', type: 'number' },
  38. ],
  39. events: {
  40. select: ({ record, dataSet }) => console.log('select', record, dataSet),
  41. unSelect: ({ record, dataSet }) =>

只使用check功能

dataSet selection 为 false , Tree checkable 为 true 可以实现整个treenode点击触发check 。注意:优先级高于record 里面的selectable

Tree树形控件 - 图3

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { DataSet, Tree } from 'choerodon-ui/pro';
  4. import { Row, Col } from 'choerodon-ui';
  5. function nodeRenderer({ record }) {
  6. return record.get('text');
  7. }
  8. class App extends React.Component {
  9. ds = new DataSet({
  10. primaryKey: 'id',
  11. queryUrl: '/tree-less.mock',
  12. autoQuery: true,
  13. expandField: 'expand',
  14. selection: false,
  15. checkField: 'ischecked',
  16. parentField: 'parentId',
  17. idField: 'id',
  18. fields: [
  19. { name: 'id', type: 'number' },
  20. { name: 'expand', type: 'boolean' },
  21. { name: 'parentId', type: 'number' },
  22. ],
  23. events: {
  24. select: ({ record, dataSet }) => console.log('select', record, dataSet),
  25. unSelect: ({ record, dataSet }) =>
  26. console.log('unSelect', record, dataSet),
  27. },
  28. });
  29. render() {
  30. return (
  31. <>
  32. <Row>
  33. <Col span={12}>
  34. <Tree
  35. dataSet={this.ds}
  36. checkable
  37. draggable
  38. showIcon
  39. renderer={nodeRenderer}
  40. />

异步加载

异步加载

Tree树形控件 - 图4

  1. import ReactDOM from 'react-dom';
  2. import { DataSet, Tree } from 'choerodon-ui/pro';
  3. import React, { useMemo } from 'react';
  4. import axios from 'axios';
  5. // 这里面可以控制node结点的判断来实现是否展示为叶结点
  6. function nodeCover({ record }) {
  7. const nodeProps = {
  8. title: record.get('text'),
  9. };
  10. if (record.get('text') === '岗位管理') {
  11. nodeProps.isLeaf = true;
  12. }
  13. return nodeProps;
  14. }
  15. const TreeDs = () => ({
  16. primaryKey: 'id',
  17. queryUrl: '/tree-async.mock',
  18. autoQuery: true,
  19. parentField: 'parentId',
  20. expandField: 'expand',
  21. idField: 'id',
  22. fields: [
  23. { name: 'id', type: 'number' },
  24. { name: 'expand', type: 'boolean' },
  25. { name: 'parentId', type: 'number' },
  26. ],
  27. events: {
  28. select: ({ record, dataSet }) => console.log('select', record, dataSet),
  29. unSelect: ({ record, dataSet }) => console.log('unSelect', record, dataSet),
  30. },
  31. });
  32. const App = () => {
  33. const dataSet = useMemo(() => new DataSet(TreeDs()), []);

API

Tree props

参数说明类型默认值
dataSet绑定的数据源DataSet
renderer节点 title 渲染钩子,只在绑定数据源时起效({ dataSet, record, text }) => ReactNode
defaultExpandAll默认展开所有树节点当 dataSet 绑定 expandField 时候忽略booleanfalse
defaultExpandedKeys默认展开指定的树节点当 dataSet 绑定 expandField 时候忽略string[][]
defaultCheckedKeys默认选中复选框的树节点当 dataSet 绑定 checkField 时候忽略string[][]
defaultSelectKeys默认选择节点当,默认绑定 dataSet 的 idFieldstring[][]
treeNodeRenderer对于 Treenode 结点的渲染覆盖(({ record, dataSet }) => TreeNodeRendererProps )() => {}

TreeNodeRenderer Props

参数说明类型默认值
checkable当树为 checkable 时,设置独立节点是否展示 Checkboxboolean
disableCheckbox禁掉 checkboxbooleanfalse
disabled禁掉响应booleanfalse
icon自定义图标。可接收组件,props 为当前节点 propsReactNode/Function(props):ReactNode
isLeaf设置为叶子节点(设置了loadData时有效)booleanfalse
title标题string|ReactNode
switcherIcon自定义树节点的展开/折叠图标React.ReactNode((props: TreeNodeProps) => React.ReactNode)
className子节点类名string
style子节点样式React.CSSProperties

DataSet 相关

参数说明类型默认值
titleField节点文字对应数据源的字段,只在绑定数据源时起效string
selectionselection 为 false Tree checkable 为 true 可以实现整个 treenode 点击触发 checkstring | boolean

dataSet 数据形式采用打平的结构,并且存在有 Id 和 parentId 的结构

更多案例和属性请参考 Tree