2017-05-18 12:28:45 +08:00
< style >
.demo-tree {
.leaf {
width: 20px;
background: #ddd ;
}
.folder {
width: 20px;
background: #888 ;
}
.buttons {
margin-top: 20px;
}
.filter-tree {
margin-top: 20px;
}
2018-02-06 16:45:27 +08:00
.custom-tree-container {
display: flex;
margin: -24px;
}
.block {
flex: 1;
padding: 8px 24px 24px;
& :first-child {
border-right: solid 1px #eff2f6 ;
}
> p {
text-align: center;
margin: 0;
line-height: 4;
}
}
.custom-tree-node {
flex: 1;
display: flex;
align-items: center;
justify-content: space-between;
font-size: 14px;
padding-right: 8px;
}
2017-05-18 12:28:45 +08:00
}
< / style >
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
}]
}];
2017-02-08 11:49:54 +08:00
const data2 = [{
id: 1,
label: 'Level one 1',
children: [{
id: 4,
label: 'Level two 1-1',
children: [{
id: 9,
label: 'Level three 1-1-1'
}, {
id: 10,
label: 'Level three 1-1-2'
}]
}]
}, {
id: 2,
label: 'Level one 2',
children: [{
id: 5,
label: 'Level two 2-1'
}, {
id: 6,
label: 'Level two 2-2'
}]
}, {
id: 3,
label: 'Level one 3',
children: [{
id: 7,
label: 'Level two 3-1'
}, {
id: 8,
label: 'Level two 3-2'
}]
}];
2017-07-19 18:36:26 +08:00
const data3 = [{
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
}]
}]
}];
2017-02-08 11:49:54 +08:00
let id = 1000;
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
};
2017-08-05 17:25:51 +08:00
const props1 = {
label: 'name',
children: 'zones',
isLeaf: 'leaf'
};
2017-02-08 11:49:54 +08:00
const defaultProps = {
2016-11-10 21:46:55 +08:00
children: 'children',
label: 'label'
};
export default {
2017-02-08 11:49:54 +08:00
watch: {
filterText(val) {
this.$refs.tree2.filter(val);
}
},
2016-11-10 21:46:55 +08:00
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') {
2017-05-18 12:28:45 +08:00
hasChild = false;
2016-11-10 21:46:55 +08:00
} else {
hasChild = Math.random() > 0.5;
}
2017-02-25 16:33:22 +08:00
2016-11-10 21:46:55 +08:00
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 = [];
}
2017-02-25 16:33:22 +08:00
2016-11-10 21:46:55 +08:00
resolve(data);
}, 500);
2017-02-08 11:49:54 +08:00
},
2017-08-05 17:25:51 +08:00
loadNode1(node, resolve) {
if (node.level === 0) {
return resolve([{ name: 'region' }]);
}
if (node.level > 1) return resolve([]);
setTimeout(() => {
const data = [{
name: 'leaf',
leaf: true
}, {
name: 'zone'
}];
resolve(data);
}, 500);
},
2017-02-08 11:49:54 +08:00
getCheckedNodes() {
console.log(this.$refs.tree.getCheckedNodes());
},
getCheckedKeys() {
console.log(this.$refs.tree.getCheckedKeys());
},
setCheckedNodes() {
this.$refs.tree.setCheckedNodes([
{
id: 5,
2018-02-06 16:45:27 +08:00
label: 'Level two 2-1'
2017-02-08 11:49:54 +08:00
},
{
id: 9,
2018-02-06 16:45:27 +08:00
label: 'Level three 1-1-1'
2017-02-08 11:49:54 +08:00
}
]);
},
setCheckedKeys() {
2017-02-28 16:30:55 +08:00
this.$refs.tree.setCheckedKeys([3]);
2017-02-08 11:49:54 +08:00
},
resetChecked() {
this.$refs.tree.setCheckedKeys([]);
},
2017-10-24 15:24:35 +08:00
append(data) {
2017-08-05 17:25:51 +08:00
const newChild = { id: id++, label: 'testtest', children: [] };
2017-10-24 15:24:35 +08:00
if (!data.children) {
this.$set(data, 'children', []);
}
2017-08-05 17:25:51 +08:00
data.children.push(newChild);
2017-02-08 11:49:54 +08:00
},
2017-02-25 16:33:22 +08:00
2017-10-24 15:24:35 +08:00
remove(node, data) {
2017-08-05 17:25:51 +08:00
const parent = node.parent;
2017-10-24 15:24:35 +08:00
const children = parent.data.children || parent.data;
const index = children.findIndex(d => d.id === data.id);
children.splice(index, 1);
2017-02-08 11:49:54 +08:00
},
2017-02-25 16:33:22 +08:00
2017-02-08 11:49:54 +08:00
renderContent(h, { node, data, store }) {
return (
2018-02-06 16:45:27 +08:00
< span class = "custom-tree-node" >
< span > {node.label}< / span >
2017-09-14 11:34:26 +08:00
< span >
2018-02-06 16:45:27 +08:00
< el-button size = "mini" type = "text" on-click = { ( ) = > this.append(data) }>Append< / el-button >
< el-button size = "mini" type = "text" on-click = { ( ) = > this.remove(node, data) }>Delete< / el-button >
2017-02-08 11:49:54 +08:00
< / span >
< / span > );
},
2017-02-25 16:33:22 +08:00
2017-02-08 11:49:54 +08:00
filterNode(value, data) {
if (!value) return true;
return data.label.indexOf(value) !== -1;
2016-11-10 21:46:55 +08:00
}
},
2017-02-25 16:33:22 +08:00
2016-11-10 21:46:55 +08:00
data() {
return {
data,
2017-02-08 11:49:54 +08:00
data2,
2017-07-19 18:36:26 +08:00
data3,
2017-10-24 15:24:35 +08:00
data4: JSON.parse(JSON.stringify(data2)),
2018-02-06 16:45:27 +08:00
data5: JSON.parse(JSON.stringify(data2)),
2016-11-10 21:46:55 +08:00
regions,
defaultProps,
2017-02-08 11:49:54 +08:00
props,
2017-08-05 17:25:51 +08:00
props1,
2017-02-08 11:49:54 +08:00
defaultCheckedKeys: [5],
defaultExpandedKeys: [2, 3],
filterText: ''
2016-11-10 21:46:55 +08:00
};
}
};
< / script >
2017-02-25 16:33:22 +08:00
## Tree
2016-11-10 21:46:55 +08:00
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
2017-12-12 12:45:09 +08:00
:::demo
2016-11-10 21:46:55 +08:00
```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
2017-10-25 11:58:58 +08:00
Used for node selection.
2016-11-10 21:46:55 +08:00
2017-12-12 12:45:09 +08:00
:::demo This example also shows how to load node data asynchronously.
2016-11-10 21:46:55 +08:00
```html
2016-11-13 22:00:55 +08:00
< el-tree
: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 {
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-08-05 17:25:51 +08:00
### Custom leaf node in lazy mode
2017-12-12 12:45:09 +08:00
:::demo A node's data is not fetched until it is clicked, so the Tree cannot predict whether a node is a leaf node. That's why a drop-down button is added to each node, and if it is a leaf node, the drop-down button will disappear when clicked. That being said, you can also tell the Tree in advance whether the node is a leaf node, avoiding the render of the drop-down button before a leaf node.
2017-08-05 17:25:51 +08:00
```html
< el-tree
:props="props1"
:load="loadNode1"
lazy
show-checkbox>
< / el-tree >
< script >
export default {
data() {
return {
props1: {
label: 'name',
children: 'zones',
isLeaf: 'leaf'
},
};
},
methods: {
loadNode1(node, resolve) {
if (node.level === 0) {
return resolve([{ name: 'region' }]);
}
if (node.level > 1) return resolve([]);
setTimeout(() => {
const data = [{
name: 'leaf',
leaf: true
}, {
name: 'zone'
}];
resolve(data);
}, 500);
}
}
};
< / script >
```
:::
2017-07-20 14:42:43 +08:00
### Disabled checkbox
2017-07-19 18:36:26 +08:00
2017-07-20 14:42:43 +08:00
The checkbox of a node can be set as disabled.
2017-07-19 18:36:26 +08:00
2017-12-12 12:45:09 +08:00
:::demo In the example, 'disabled' property is declared in defaultProps, and some nodes are set as 'disabled:true'. The corresponding checkboxes are disabled and can't be clicked.
2017-07-19 18:36:26 +08:00
```html
< el-tree
:data="data3"
:props="defaultProps"
show-checkbox
@check -change="handleCheckChange">
< / el-tree >
< script >
export default {
data() {
return {
data3: [{
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
}]
}]
}],
defaultProps: {
children: 'children',
label: 'label',
disabled: 'disabled',
},
};
}
};
< / script >
```
:::
2017-02-08 11:49:54 +08:00
### Default expanded and default checked
Tree nodes can be initially expanded or checked
2017-12-12 12:45:09 +08:00
:::demo Use `default-expanded-keys` and `default-checked-keys` to set initially expanded and initially checked nodes respectively. Note that for them to work, `node-key` is required. Its value is the name of a key in the data object, and the value of that key should be unique across the whole tree.
2017-02-08 11:49:54 +08:00
```html
< el-tree
:data="data2"
show-checkbox
node-key="id"
:default-expanded-keys="[2, 3]"
:default-checked-keys="[5]"
:props="defaultProps">
< / el-tree >
< script >
export default {
data() {
return {
data2: [{
id: 1,
label: 'Level one 1',
children: [{
id: 4,
label: 'Level two 1-1',
children: [{
id: 9,
label: 'Level three 1-1-1'
}, {
id: 10,
label: 'Level three 1-1-2'
}]
}]
}, {
id: 2,
label: 'Level one 2',
children: [{
id: 5,
label: 'Level two 2-1'
}, {
id: 6,
label: 'Level two 2-2'
}]
}, {
id: 3,
label: 'Level one 3',
children: [{
id: 7,
label: 'Level two 3-1'
}, {
id: 8,
label: 'Level two 3-2'
}]
}],
defaultProps: {
children: 'children',
label: 'label'
}
};
}
};
< / script >
```
:::
### Checking tree nodes
2017-12-12 12:45:09 +08:00
:::demo This example shows how to get and set checked nodes. They both can be done in two approaches: node and key. If you are taking the key approach, `node-key` is required.
2017-02-08 11:49:54 +08:00
```html
< el-tree
:data="data2"
show-checkbox
default-expand-all
node-key="id"
ref="tree"
highlight-current
:props="defaultProps">
< / el-tree >
< div class = "buttons" >
< el-button @click =" getCheckedNodes " > get by node</ el-button >
< el-button @click =" getCheckedKeys " > get by key</ el-button >
< el-button @click =" setCheckedNodes " > set by node</ el-button >
< el-button @click =" setCheckedKeys " > set by key</ el-button >
< el-button @click =" resetChecked " > reset</ el-button >
< / div >
< script >
export default {
methods: {
getCheckedNodes() {
console.log(this.$refs.tree.getCheckedNodes());
},
getCheckedKeys() {
console.log(this.$refs.tree.getCheckedKeys());
},
setCheckedNodes() {
this.$refs.tree.setCheckedNodes([{
id: 5,
label: 'Level two 2-1'
}, {
id: 9,
label: 'Level three 1-1-1'
}]);
},
setCheckedKeys() {
2017-02-28 16:30:55 +08:00
this.$refs.tree.setCheckedKeys([3]);
2017-02-08 11:49:54 +08:00
},
resetChecked() {
this.$refs.tree.setCheckedKeys([]);
}
},
2017-02-25 16:33:22 +08:00
2017-02-08 11:49:54 +08:00
data() {
return {
data2: [{
id: 1,
label: 'Level one 1',
children: [{
id: 4,
label: 'Level two 1-1',
children: [{
id: 9,
label: 'Level three 1-1-1'
}, {
id: 10,
label: 'Level three 1-1-2'
}]
}]
}, {
id: 2,
label: 'Level one 2',
children: [{
id: 5,
label: 'Level two 2-1'
}, {
id: 6,
label: 'Level two 2-2'
}]
}, {
id: 3,
label: 'Level one 3',
children: [{
id: 7,
label: 'Level two 3-1'
}, {
id: 8,
label: 'Level two 3-2'
}]
}],
defaultProps: {
children: 'children',
label: 'label'
}
};
}
};
< / script >
```
:::
### Custom node content
The content of tree nodes can be customized, so you can add icons or buttons as you will
2018-02-06 16:45:27 +08:00
:::demo There are two ways to customize template for tree nodes: `render-content` and scoped slot. Use `render-content` to assign a render function that returns the content of tree nodes. See Vue's documentation for a detailed introduction of render functions. If you prefer scoped slot, you'll have access to `node` and `data` in the scope, standing for the TreeNode object and node data of the current node respectively. Note that the `render-content` demo can't run in jsfiddle because it doesn't support JSX syntax. In a real project, `render-content` will work if relevant dependencies are correctly configured.
2017-02-08 11:49:54 +08:00
```html
2018-02-06 16:45:27 +08:00
< div class = "custom-tree-container" >
< div class = "block" >
< p > Using render-content< / p >
< el-tree
:data="data4"
show-checkbox
node-key="id"
default-expand-all
:expand-on-click-node="false"
:render-content="renderContent">
< / el-tree >
< / div >
< div class = "block" >
< p > Using scoped slot< / p >
< el-tree
:data="data5"
show-checkbox
node-key="id"
default-expand-all
:expand-on-click-node="false">
< span class = "custom-tree-node" slot-scope = "{ node, data }" >
< span > {{ node.label }}< / span >
< span >
< el-button
type="text"
size="mini"
@click ="() => append(data)">
Append
< / el-button >
< el-button
type="text"
size="mini"
@click ="() => remove(node, data)">
Delete
< / el-button >
< / span >
< / span >
< / el-tree >
< / div >
< / div >
2017-02-08 11:49:54 +08:00
< script >
let id = 1000;
2017-02-25 16:33:22 +08:00
2017-02-08 11:49:54 +08:00
export default {
data() {
2018-02-06 16:45:27 +08:00
const data = [{
id: 1,
label: 'Level one 1',
children: [{
id: 4,
label: 'Level two 1-1',
2017-02-08 11:49:54 +08:00
children: [{
2018-02-06 16:45:27 +08:00
id: 9,
label: 'Level three 1-1-1'
2017-02-08 11:49:54 +08:00
}, {
2018-02-06 16:45:27 +08:00
id: 10,
label: 'Level three 1-1-2'
2017-02-08 11:49:54 +08:00
}]
2018-02-06 16:45:27 +08:00
}]
}, {
id: 2,
label: 'Level one 2',
children: [{
id: 5,
label: 'Level two 2-1'
2017-02-08 11:49:54 +08:00
}, {
2018-02-06 16:45:27 +08:00
id: 6,
label: 'Level two 2-2'
}]
}, {
id: 3,
label: 'Level one 3',
children: [{
id: 7,
label: 'Level two 3-1'
}, {
id: 8,
label: 'Level two 3-2'
}]
}];
return {
data4: JSON.parse(JSON.stringify(data)),
data5: JSON.parse(JSON.stringify(data))
2017-02-08 11:49:54 +08:00
}
},
2017-02-25 16:33:22 +08:00
2017-02-08 11:49:54 +08:00
methods: {
2017-10-24 15:24:35 +08:00
append(data) {
2017-08-05 17:25:51 +08:00
const newChild = { id: id++, label: 'testtest', children: [] };
2017-10-24 15:24:35 +08:00
if (!data.children) {
this.$set(data, 'children', []);
}
2017-08-05 17:25:51 +08:00
data.children.push(newChild);
2017-02-08 11:49:54 +08:00
},
2017-02-25 16:33:22 +08:00
2017-10-24 15:24:35 +08:00
remove(node, data) {
2017-08-05 17:25:51 +08:00
const parent = node.parent;
2017-10-24 15:24:35 +08:00
const children = parent.data.children || parent.data;
const index = children.findIndex(d => d.id === data.id);
children.splice(index, 1);
2017-02-08 11:49:54 +08:00
},
2017-02-25 16:33:22 +08:00
2017-02-08 11:49:54 +08:00
renderContent(h, { node, data, store }) {
return (
2018-02-06 16:45:27 +08:00
< span class = "custom-tree-node" >
< span > {node.label}< / span >
2017-09-14 11:34:26 +08:00
< span >
2018-02-06 16:45:27 +08:00
< el-button size = "mini" type = "text" on-click = { ( ) = > this.append(data) }>Append< / el-button >
< el-button size = "mini" type = "text" on-click = { ( ) = > this.remove(node, data) }>Delete< / el-button >
2017-02-08 11:49:54 +08:00
< / span >
< / span > );
}
}
};
< / script >
2018-02-06 16:45:27 +08:00
< style >
.custom-tree-node {
flex: 1;
display: flex;
align-items: center;
justify-content: space-between;
font-size: 14px;
padding-right: 8px;
}
< / style >
2017-02-08 11:49:54 +08:00
```
:::
### Tree node filtering
Tree nodes can be filtered
2017-12-12 12:45:09 +08:00
:::demo Invoke the `filter` method of the Tree instance to filter tree nodes. Its parameter is the filtering keyword. Note that for it to work, `filter-node-method` is required, and its value is the filtering method.
2017-02-08 11:49:54 +08:00
```html
< el-input
placeholder="Filter keyword"
v-model="filterText">
< / el-input >
< el-tree
class="filter-tree"
:data="data2"
:props="defaultProps"
default-expand-all
:filter-node-method="filterNode"
ref="tree2">
< / el-tree >
< script >
export default {
watch: {
filterText(val) {
this.$refs.tree2.filter(val);
}
},
methods: {
filterNode(value, data) {
if (!value) return true;
return data.label.indexOf(value) !== -1;
}
},
data() {
return {
filterText: '',
data2: [{
id: 1,
label: 'Level one 1',
children: [{
id: 4,
label: 'Level two 1-1',
children: [{
id: 9,
label: 'Level three 1-1-1'
}, {
id: 10,
label: 'Level three 1-1-2'
}]
}]
}, {
id: 2,
label: 'Level one 2',
children: [{
id: 5,
label: 'Level two 2-1'
}, {
id: 6,
label: 'Level two 2-2'
}]
}, {
id: 3,
label: 'Level one 3',
children: [{
id: 7,
label: 'Level two 3-1'
}, {
id: 8,
label: 'Level two 3-2'
}]
}],
defaultProps: {
children: 'children',
label: 'label'
}
};
}
};
< / script >
```
:::
2017-01-14 16:02:52 +08:00
### Accordion
Only one node among the same level can be expanded at one time.
2017-12-12 12:45:09 +08:00
:::demo
2017-01-14 16:02:52 +08:00
```html
2017-02-08 11:49:54 +08:00
< el-tree
:data="data"
:props="defaultProps"
accordion
@node -click="handleNodeClick">
< / el-tree >
2017-01-14 16:02:52 +08:00
< 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
2017-01-18 17:49:32 +08:00
| Attribute | Description | Type | Accepted Values | Default |
| --------------------- | ---------------------------------------- | --------------------------- | --------------- | ------- |
| data | tree data | array | — | — |
| 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 | — | — |
| props | configuration options, see the following table | object | — | — |
2017-12-24 18:59:09 +08:00
| render-after-expand | whether to render child nodes only after a parent node is expanded for the first time | boolean | — | true |
2018-01-14 20:40:52 +08:00
| load | method for loading subtree data, only works when `lazy` is true | function(node, resolve) | — | — |
2017-10-24 15:24:35 +08:00
| render-content | render function for tree node | Function(h, { node, data, store } | — | — |
2017-01-18 17:49:32 +08:00
| highlight-current | whether current node is highlighted | boolean | — | false |
| 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 | |
| auto-expand-parent | whether to expand father node when a child node is expanded | boolean | — | true |
| default-expanded-keys | array of keys of initially expanded nodes | array | — | — |
| 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 | — | — |
| 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) | — | — |
| accordion | whether only one node among the same level can be expanded at one time | boolean | — | false |
2017-02-08 11:49:54 +08:00
| indent | horizontal indentation of nodes in adjacent levels in pixels | number | — | 16 |
2018-01-14 20:40:52 +08:00
| lazy | whether to lazy load leaf node, used with `load` attribute | boolean | — | false |
2016-11-10 21:46:55 +08:00
### props
2017-01-18 17:49:32 +08:00
| Attribute | Description | Type | Accepted Values | Default |
| --------- | ---------------------------------------- | ------ | --------------- | ------- |
2017-07-20 18:40:06 +08:00
| label | specify which key of node object is used as the node's label | string, function(data, node) | — | — |
| children | specify which node object is used as the node's subtree | string, function(data, node) | — | — |
2017-09-29 16:21:29 +08:00
| disabled | specify which key of node object represents if node's checkbox is disabled | boolean, function(data, node) | — | — |
2017-08-05 17:25:51 +08:00
| isLeaf | specify whether the node is a leaf node | boolean, function(data, node) | — | — |
2016-11-10 21:46:55 +08:00
### Method
`Tree` has the following method, which returns the currently selected array of nodes.
2017-01-18 17:49:32 +08:00
| Method | Description | Parameters |
| --------------- | ---------------------------------------- | ---------------------------------------- |
| filter | filter all tree nodes, filtered nodes will be hidden | Accept a parameter which will be used as first parameter for filter-node-method |
2017-07-21 16:25:28 +08:00
| updateKeyChildren | set new data to node, only works when `node-key` is assigned | (key, data) Accept two parameters: 1. key of node 2. new data |
2017-01-18 17:49:32 +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. |
| setCheckedNodes | set certain nodes to be checked, only works when `node-key` is assigned | an array of nodes to be checked |
2017-02-25 16:33:22 +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 `false` . 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 `false` . If the parameter is `true` , it only returns the currently selected array of sub-nodes. |
2017-01-18 17:49:32 +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. |
2017-07-23 17:28:26 +08:00
| getCurrentKey | return the highlight node's key (null if no node is highlighted) | — |
| getCurrentNode | return the highlight node (null if no node is highlighted) | — |
| setCurrentKey | set highlighted node by key, only works when `node-key` is assigned | (key) the node's key to be highlighted |
| setCurrentNode | set highlighted node, only works when `node-key` is assigned | (node) the node to be highlighted |
2016-11-10 21:46:55 +08:00
### Events
2017-01-18 17:49:32 +08:00
| Event Name | Description | Parameters |
| -------------- | ---------------------------------------- | ---------------------------------------- |
| node-click | triggers when a node is clicked | three parameters: node object corresponding to the node clicked, `node` property of TreeNode, TreeNode itself |
2018-02-06 11:42:01 +08:00
| node-contextmenu | triggers when a node is clicked by right button | four parameters: event, node object corresponding to the node clicked, `node` property of TreeNode, TreeNode itself |
2017-01-18 17:49:32 +08:00
| 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 |
| current-change | triggers when current node changes | two parameters: node object corresponding to the current node, `node` property of TreeNode |
2017-01-24 10:37:44 +08:00
| node-expand | triggers when current node open | three parameters: node object corresponding to the node opened, `node` property of TreeNode, TreeNode itself |
| node-collapse | triggers when current node close | three parameters: node object corresponding to the node closed, `node` property of TreeNode, TreeNode itself |
2018-02-06 16:45:27 +08:00
### Scoped slot
| name | Description |
|------|--------|
| — | Custom content for tree nodes. The scope parameter is { node, data } |