2016-11-10 21:46:55 +08:00
< script >
2016-11-16 15:35:46 +08:00
const data = [{
2016-11-10 21:46:55 +08:00
label: 'Level one 1',
children: [{
2017-01-14 16:02:52 +08:00
label: 'Level two 1-1',
children: [{
label: 'Level three 1-1-1'
}]
2016-11-10 21:46:55 +08:00
}]
}, {
label: 'Level one 2',
children: [{
2017-01-14 16:02:52 +08:00
label: 'Level two 2-1',
children: [{
label: 'Level three 2-1-1'
}]
2016-11-10 21:46:55 +08:00
}, {
2017-01-14 16:02:52 +08:00
label: 'Level two 2-2',
children: [{
label: 'Level three 2-2-1'
}]
2016-11-10 21:46:55 +08:00
}]
}, {
label: 'Level one 3',
children: [{
2017-01-14 16:02:52 +08:00
label: 'Level two 3-1',
children: [{
label: 'Level three 3-1-1'
}]
2016-11-10 21:46:55 +08:00
}, {
2017-01-14 16:02:52 +08:00
label: 'Level two 3-2',
children: [{
label: 'Level three 3-2-1'
}]
2016-11-10 21:46:55 +08:00
}]
}];
2016-11-16 15:35:46 +08:00
const regions = [{
2016-11-10 21:46:55 +08:00
'name': 'region1'
}, {
'name': 'region2'
}];
2016-11-16 15:35:46 +08:00
let count = 1;
2016-11-10 21:46:55 +08:00
2016-11-16 15:35:46 +08:00
const props = {
2016-11-10 21:46:55 +08:00
label: 'name',
2016-11-16 15:35:46 +08:00
children: 'zones'
2016-11-10 21:46:55 +08:00
};
var defaultProps = {
children: 'children',
label: 'label'
};
export default {
methods: {
2016-11-13 22:00:55 +08:00
handleCheckChange(data, checked, indeterminate) {
console.log(data, checked, indeterminate);
},
handleNodeClick(data) {
console.log(data);
},
2016-11-10 21:46:55 +08:00
loadNode(node, resolve) {
2016-11-16 15:35:46 +08:00
if (node.level === 0) {
return resolve([{ name: 'Root1' }, { name: 'Root2' }]);
2016-11-10 21:46:55 +08:00
}
2016-11-16 15:35:46 +08:00
if (node.level > 3) return resolve([]);
2016-11-10 21:46:55 +08:00
var hasChild;
2016-11-13 22:00:55 +08:00
if (node.data.name === 'region1') {
2016-11-10 21:46:55 +08:00
hasChild = true;
2016-11-13 22:00:55 +08:00
} else if (node.data.name === 'region2') {
2016-11-10 21:46:55 +08:00
hasChild = false;
} else {
hasChild = Math.random() > 0.5;
}
setTimeout(function() {
2016-11-16 15:35:46 +08:00
let data;
2016-11-10 21:46:55 +08:00
if (hasChild) {
data = [{
name: 'zone' + count++
}, {
name: 'zone' + count++
}];
} else {
data = [];
}
resolve(data);
}, 500);
}
},
data() {
return {
data,
regions,
defaultProps,
props
};
}
};
< / script >
## Tree
2016-11-13 22:00:55 +08:00
Display a set of data with hierarchies.
2016-11-10 21:46:55 +08:00
2016-11-13 22:00:55 +08:00
### Basic usage
2016-11-10 21:46:55 +08:00
2016-11-13 22:00:55 +08:00
Basic tree structure.
2016-11-10 21:46:55 +08:00
::: demo
```html
2016-11-13 22:00:55 +08:00
< el-tree :data = "data" :props = "defaultProps" @node -click = "handleNodeClick" ></ el-tree >
2016-11-10 21:46:55 +08:00
< script >
export default {
data() {
return {
data: [{
label: 'Level one 1',
children: [{
2017-01-14 16:02:52 +08:00
label: 'Level two 1-1',
children: [{
label: 'Level three 1-1-1'
}]
2016-11-10 21:46:55 +08:00
}]
}, {
label: 'Level one 2',
children: [{
2017-01-14 16:02:52 +08:00
label: 'Level two 2-1',
children: [{
label: 'Level three 2-1-1'
}]
2016-11-10 21:46:55 +08:00
}, {
2017-01-14 16:02:52 +08:00
label: 'Level two 2-2',
children: [{
label: 'Level three 2-2-1'
}]
2016-11-10 21:46:55 +08:00
}]
}, {
label: 'Level one 3',
children: [{
2017-01-14 16:02:52 +08:00
label: 'Level two 3-1',
children: [{
label: 'Level three 3-1-1'
}]
2016-11-10 21:46:55 +08:00
}, {
2017-01-14 16:02:52 +08:00
label: 'Level two 3-2',
children: [{
label: 'Level three 3-2-1'
}]
2016-11-10 21:46:55 +08:00
}]
}],
defaultProps: {
children: 'children',
label: 'label'
}
};
2016-11-13 22:00:55 +08:00
},
methods: {
handleNodeClick(data) {
console.log(data);
}
2016-11-10 21:46:55 +08:00
}
};
< / script >
```
:::
2016-11-13 22:00:55 +08:00
### Selectable
2016-11-10 21:46:55 +08:00
2016-11-13 22:00:55 +08:00
Used for node selection. In the following example, data for each layer is acquired after being clicked. If there is no child data, the expanding icon will disappear.
2016-11-10 21:46:55 +08:00
::: demo
```html
2016-11-13 22:00:55 +08:00
< el-tree
:data="regions"
:props="props"
:load="loadNode"
lazy
show-checkbox
@check -change="handleCheckChange">
< / el-tree >
2016-11-10 21:46:55 +08:00
< script >
export default {
data() {
return {
regions: [{
'name': 'region1'
}, {
'name': 'region2'
}],
props: {
label: 'name',
children: 'zones'
},
count: 1
};
},
methods: {
2016-11-13 22:00:55 +08:00
handleCheckChange(data, checked, indeterminate) {
console.log(data, checked, indeterminate);
},
handleNodeClick(data) {
console.log(data);
2016-11-10 21:46:55 +08:00
},
loadNode(node, resolve) {
2016-11-16 15:35:46 +08:00
if (node.level === 0) {
return resolve([{ name: 'Root1' }, { name: 'Root2' }]);
2016-11-10 21:46:55 +08:00
}
2016-11-16 15:35:46 +08:00
if (node.level > 3) return resolve([]);
2016-11-10 21:46:55 +08:00
2016-11-13 22:00:55 +08:00
var hasChild;
if (node.data.name === 'region1') {
hasChild = true;
} else if (node.data.name === 'region2') {
hasChild = false;
} else {
hasChild = Math.random() > 0.5;
}
2016-11-10 21:46:55 +08:00
setTimeout(() => {
var data;
if (hasChild) {
data = [{
name: 'zone' + this.count++
}, {
name: 'zone' + this.count++
}];
} else {
data = [];
}
resolve(data);
}, 500);
}
}
};
< / script >
```
:::
2017-01-14 16:02:52 +08:00
### Accordion
Only one node among the same level can be expanded at one time.
::: demo
```html
< el-tree :data = "data" :props = "defaultProps" @node -click = "handleNodeClick" ></ el-tree >
< script >
export default {
data() {
return {
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'
}]
}]
}, {
label: 'Level one 3',
children: [{
label: 'Level two 3-1',
children: [{
label: 'Level three 3-1-1'
}]
}, {
label: 'Level two 3-2',
children: [{
label: 'Level three 3-2-1'
}]
}]
}],
defaultProps: {
children: 'children',
label: 'label'
}
};
},
methods: {
handleNodeClick(data) {
console.log(data);
}
}
};
< / script >
```
:::
2016-11-10 21:46:55 +08:00
### Attributes
2016-11-13 22:00:55 +08:00
| Attribute | Description | Type | Accepted Values | Default |
2016-11-10 21:46:55 +08:00
|---------- |-------------- |---------- |-------------------------------- |-------- |
2016-11-13 22:00:55 +08:00
| data | tree data | array | — | — |
2016-11-16 16:36:01 +08:00
| empty-text | text displayed when data is void | string | — | — |
| node-key | unique identity key name for nodes, its value should be unique across the whole tree | string | — | — |
2016-11-13 22:00:55 +08:00
| props | configuration options, see the following table | object | — | — |
| load | method for loading subtree data | function(node, resolve) | — | — |
2016-12-29 20:53:33 +08:00
| render-content | render function for tree node | Function(h, { node } | — | — |
| highlight-current | whether current node is highlighted | boolean | — | false |
| current-node-key | key of current node, a set only prop | string, number | — | — |
| default-expand-all | whether to expand all nodes by default | boolean | — | false |
| expand-on-click-node | whether to expand or collapse node when clicking on the node, if false, then expand or collapse node only when clicking on the arrow icon. | — | true |
2016-11-16 16:36:01 +08:00
| auto-expand-parent | whether to expand father node when a child node is expanded | boolean | — | true |
2016-11-22 23:28:19 +08:00
| default-expanded-keys | array of keys of initially expanded nodes | array | — | — |
2016-11-16 16:36:01 +08:00
| show-checkbox | whether node is selectable | boolean | — | false |
| check-strictly | whether checked state of a node not affects its father and child nodes when `show-checkbox` is `true` | boolean | — | false |
| default-checked-keys | array of keys of initially checked nodes | array | — | — |
2016-12-29 20:53:33 +08:00
| filter-node-method | this function will be executed on each node when use filter method. if return `false` , tree node will be hidden. | Function(value, data, node) | — | — |
2017-01-14 16:02:52 +08:00
| accordion | whether only one node among the same level can be expanded at one time | boolean | — | false |
2016-11-10 21:46:55 +08:00
### props
2016-11-13 22:00:55 +08:00
| Attribute | Description | Type | Accepted Values | Default |
2016-11-10 21:46:55 +08:00
|---------- |-------------- |---------- |-------------------------------- |-------- |
2016-11-13 22:00:55 +08:00
| label | specify which key of node object is used as the node's label | string | — | — |
| children | specify which key of node object is used as the node's subtree | string | — | — |
2016-11-10 21:46:55 +08:00
### Method
`Tree` has the following method, which returns the currently selected array of nodes.
2016-11-13 22:00:55 +08:00
| Method | Description | Parameters |
2016-11-10 21:46:55 +08:00
|---------- |-------- |---------- |
2016-11-22 00:08:22 +08:00
| filter | filter all tree nodes, filtered nodes will be hidden | Accept a parameter which will be used as first parameter for filter-node-method |
2016-12-13 22:52:29 +08:00
| getCheckedNodes | If the node can be selected (`show-checkbox` is `true` ), it returns the currently selected array of nodes | Accept a boolean type parameter whose default value is `false` . If the parameter is `true` , it only returns the currently selected array of sub-nodes.|
2016-11-16 16:36:01 +08:00
| setCheckedNodes | set certain nodes to be checked, only works when `node-key` is assigned | an array of nodes to be checked |
2016-12-13 22:52:29 +08:00
| getCheckedKeys | If the node can be selected (`show-checkbox` is `true` ), it returns the currently selected array of node's keys | (leafOnly) Accept a boolean type parameter whose default value is `true` . If the parameter is `true` , it only returns the currently selected array of sub-nodes.|
| setCheckedKeys | set certain nodes to be checked, only works when `node-key` is assigned | (keys, leafOnly) Accept two parameters: 1. an array of node's keys to be checked 2. a boolean type parameter whose default value is `true` . If the parameter is `true` , it only returns the currently selected array of sub-nodes. |
2016-11-28 18:57:09 +08:00
| setChecked | set node to be checked or not, only works when `node-key` is assigned | (key/data, checked, deep) Accept three parameters: 1. node's key or data to be checked 2. a boolean typed parameter indicating checked or not. 3. a boolean typed parameter indicating deep or not. |
2016-11-10 21:46:55 +08:00
### Events
2016-11-13 22:00:55 +08:00
| Event Name | Description | Parameters |
2016-11-10 21:46:55 +08:00
|---------- |-------- |---------- |
2016-12-13 22:52:29 +08:00
| node-click | triggers when a node is clicked | three parameters: node object corresponding to the node clicked, `node` property of TreeNode, TreeNode itself |
| check-change | triggers when the selected state of the node changes | three parameters: node object corresponding to the node whose selected state is changed, whether the node is selected, whether node's subtree has selected nodes |
2016-12-17 14:09:05 +08:00
| current-change | triggers when current node changes | two parameters: node object corresponding to the current node, `node` property of TreeNode |
2016-11-10 21:46:55 +08:00