|
| 1 | +import React, { Component } from 'react'; |
| 2 | +import { findDOMNode } from 'react-dom'; |
| 3 | +import TreeStore from 'xtree-store'; |
| 4 | +import NilTree from 'nil-tree'; |
| 5 | +import data from '../data.json'; |
| 6 | + |
| 7 | + |
| 8 | +/** |
| 9 | + * 需要自定义的属性: |
| 10 | + * rootComponent, renderNode |
| 11 | + */ |
| 12 | +export default class DEMO extends Component { |
| 13 | + |
| 14 | + dataStore = {} |
| 15 | + |
| 16 | + constructor(props) { |
| 17 | + super(props); |
| 18 | + |
| 19 | + this.store = new TreeStore(data, { |
| 20 | + simpleData: true |
| 21 | + }); |
| 22 | + |
| 23 | + } |
| 24 | + |
| 25 | + loadData = node => { |
| 26 | + |
| 27 | + if (this.dataStore[node.id]) return this.dataStore[node.id]; |
| 28 | + |
| 29 | + return new Promise(resolve => { |
| 30 | + const server = this.store; |
| 31 | + setTimeout(() => { |
| 32 | + const childs = server.getChildren(node.id); |
| 33 | + this.dataStore[node.id] = childs; |
| 34 | + resolve(childs) |
| 35 | + }, 500); |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + toggleExpand = (node, e, t) => { |
| 40 | + // node.expanded = !node.expanded; |
| 41 | + // this.forceUpdate(); |
| 42 | + // or |
| 43 | + t.toggleExpand() |
| 44 | + } |
| 45 | + |
| 46 | + renderNodeRow = (node, props, me) => { |
| 47 | + const { parentProps } = me.props; |
| 48 | + const { showIcon, showExpanderIcon, checkable } = parentProps |
| 49 | + |
| 50 | + return ( |
| 51 | + <tr> |
| 52 | + <td> |
| 53 | + <div {...me.getNodeProps()}> |
| 54 | + {me.renderIndentIcons()} |
| 55 | + { |
| 56 | + node.loading ? |
| 57 | + me.renderLoadingIcon() : |
| 58 | + showExpanderIcon ? |
| 59 | + me.renderExpanderIcon() : |
| 60 | + null |
| 61 | + } |
| 62 | + {showIcon ? me.renderIcon() : null} |
| 63 | + {checkable ? me.renderCheckbox() : null} |
| 64 | + {me.renderLabel()} |
| 65 | + {me.renderExtIcons()} |
| 66 | + </div> |
| 67 | + </td> |
| 68 | + <td align="center">{node.id}</td> |
| 69 | + <td align="center">{node.relativeDepth}</td> |
| 70 | + <td align="center">{node.leaf ? '叶子' : '父级'}</td> |
| 71 | + </tr> |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + render() { |
| 76 | + return ( |
| 77 | + <table className="table-tree" borderspacing="0"> |
| 78 | + <thead> |
| 79 | + <tr> |
| 80 | + <th>名称</th> |
| 81 | + <th width={100} align="center">ID</th> |
| 82 | + <th width={100} align="center">层级</th> |
| 83 | + <th width={100} align="center">类型</th> |
| 84 | + </tr> |
| 85 | + </thead> |
| 86 | + <NilTree |
| 87 | + onNodeClick={this.toggleExpand} |
| 88 | + loadData={this.loadData} |
| 89 | + rootComponent="tbody" |
| 90 | + loadingComponent={(props) => ( |
| 91 | + <tr> |
| 92 | + <td colSpan={4} {...props}></td> |
| 93 | + </tr> |
| 94 | + )} |
| 95 | + renderNode={this.renderNodeRow} |
| 96 | + /> |
| 97 | + </table> |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments