2016-07-27 14:15:02 +08:00
|
|
|
<style>
|
|
|
|
.leaf {
|
|
|
|
width: 20px;
|
|
|
|
background: #ddd;
|
|
|
|
}
|
|
|
|
|
|
|
|
.folder {
|
|
|
|
width: 20px;
|
|
|
|
background: #888;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
|
2016-08-12 14:45:06 +08:00
|
|
|
<script>
|
2016-07-27 14:15:02 +08:00
|
|
|
var data = [{
|
2016-09-04 10:27:09 +08:00
|
|
|
label: '一级 1',
|
|
|
|
children: [{
|
|
|
|
label: '二级 1-1'
|
|
|
|
}]
|
|
|
|
}, {
|
|
|
|
label: '一级 2',
|
2016-07-27 14:15:02 +08:00
|
|
|
children: [{
|
2016-09-04 10:27:09 +08:00
|
|
|
label: '二级 2-1'
|
|
|
|
}, {
|
|
|
|
label: '二级 2-2'
|
2016-07-27 14:15:02 +08:00
|
|
|
}]
|
|
|
|
}, {
|
2016-09-04 10:27:09 +08:00
|
|
|
label: '一级 3',
|
2016-07-27 14:15:02 +08:00
|
|
|
children: [{
|
2016-09-04 10:27:09 +08:00
|
|
|
label: '二级 3-1'
|
2016-07-27 14:15:02 +08:00
|
|
|
}, {
|
2016-09-04 10:27:09 +08:00
|
|
|
label: '二级 3-2'
|
2016-07-27 14:15:02 +08:00
|
|
|
}]
|
|
|
|
}];
|
|
|
|
|
|
|
|
var regions = [{
|
|
|
|
'name': 'region1'
|
|
|
|
}, {
|
|
|
|
'name': 'region2'
|
|
|
|
}];
|
|
|
|
|
|
|
|
var count = 1;
|
|
|
|
|
|
|
|
var props = {
|
|
|
|
label: 'name',
|
|
|
|
children: 'zones',
|
|
|
|
icon(data, node) {
|
|
|
|
if (node.isLeaf) {
|
|
|
|
return 'el-icon-close';
|
|
|
|
}
|
|
|
|
return 'el-icon-search';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var defaultProps = {
|
|
|
|
children: 'children',
|
|
|
|
label: 'label'
|
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
methods: {
|
|
|
|
loadNode(node, resolve) {
|
2016-08-23 15:24:26 +08:00
|
|
|
console.log(node);
|
2016-07-27 14:15:02 +08:00
|
|
|
if (node.level === -1) {
|
|
|
|
return resolve([{ name: 'Root1' }, { name: 'Root2' }]);
|
|
|
|
}
|
|
|
|
var hasChild = Math.random() > 0.5;
|
|
|
|
if (node.level > 4) return resolve([]);
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
var data;
|
|
|
|
if (hasChild) {
|
|
|
|
data = [{
|
|
|
|
name: 'zone' + count++
|
|
|
|
}, {
|
|
|
|
name: 'zone' + count++
|
|
|
|
}];
|
|
|
|
} else {
|
|
|
|
data = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(data);
|
|
|
|
}, 500);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
data,
|
|
|
|
regions,
|
|
|
|
defaultProps,
|
|
|
|
props
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
|
2016-09-04 10:27:09 +08:00
|
|
|
## Tree 树形控件
|
|
|
|
|
2016-09-08 09:15:35 +08:00
|
|
|
用清晰的层级结构展示信息,可展开或折叠。
|
2016-07-27 14:15:02 +08:00
|
|
|
|
2016-09-04 10:27:09 +08:00
|
|
|
### 基础用法
|
|
|
|
|
2016-09-08 09:15:35 +08:00
|
|
|
基础的树形结构展示。
|
|
|
|
|
2016-09-04 10:27:09 +08:00
|
|
|
::: demo
|
|
|
|
```html
|
2016-07-27 14:15:02 +08:00
|
|
|
<el-tree :data="data" :props="defaultProps"></el-tree>
|
|
|
|
|
2016-09-04 10:27:09 +08:00
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
data: [{
|
|
|
|
label: '一级 1',
|
|
|
|
children: [{
|
|
|
|
label: '二级 1-1'
|
|
|
|
}]
|
|
|
|
}, {
|
|
|
|
label: '一级 2',
|
|
|
|
children: [{
|
|
|
|
label: '二级 2-1'
|
|
|
|
}, {
|
|
|
|
label: '二级 2-2'
|
|
|
|
}]
|
|
|
|
}, {
|
|
|
|
label: '一级 3',
|
|
|
|
children: [{
|
|
|
|
label: '二级 3-1'
|
|
|
|
}, {
|
|
|
|
label: '二级 3-2'
|
|
|
|
}]
|
|
|
|
}],
|
|
|
|
defaultProps: {
|
|
|
|
children: 'children',
|
|
|
|
label: 'label'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
```
|
|
|
|
:::
|
|
|
|
|
|
|
|
### 可选择
|
|
|
|
|
2016-09-08 09:15:35 +08:00
|
|
|
适用于需要选择层级时使用。
|
2016-07-27 14:15:02 +08:00
|
|
|
|
2016-09-04 10:27:09 +08:00
|
|
|
::: demo
|
|
|
|
```html
|
2016-07-27 14:15:02 +08:00
|
|
|
<el-tree :data="regions" :props="props" :load="loadNode" lazy show-checkbox></el-tree>
|
2016-09-04 10:27:09 +08:00
|
|
|
|
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
regions: [{
|
|
|
|
'name': 'region1'
|
|
|
|
}, {
|
|
|
|
'name': 'region2'
|
|
|
|
}],
|
|
|
|
props: {
|
|
|
|
label: 'name',
|
|
|
|
children: 'zones'
|
|
|
|
},
|
|
|
|
count: 1
|
|
|
|
};
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
getCheckedNodes() {
|
|
|
|
console.log(this.$refs.tree.getCheckedNodes(true));
|
|
|
|
},
|
|
|
|
|
|
|
|
loadNode(node, resolve) {
|
|
|
|
console.log(node);
|
|
|
|
if (node.level === -1) {
|
|
|
|
return resolve([{ name: 'Root1' }, { name: 'Root2' }]);
|
|
|
|
}
|
|
|
|
var hasChild = Math.random() > 0.5;
|
|
|
|
if (node.level > 4) return resolve([]);
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
var data;
|
|
|
|
if (hasChild) {
|
|
|
|
data = [{
|
|
|
|
name: 'zone' + count++
|
|
|
|
}, {
|
|
|
|
name: 'zone' + count++
|
|
|
|
}];
|
|
|
|
} else {
|
|
|
|
data = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(data);
|
|
|
|
}, 500);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
```
|
|
|
|
:::
|
|
|
|
|
|
|
|
### Attributes
|
|
|
|
|
|
|
|
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|
|
|
|
|---------- |-------------- |---------- |-------------------------------- |-------- |
|
|
|
|
| data | 展示数据 | array | — | — |
|
|
|
|
| props | 配置选项,具体看下表 | object | — | — |
|
|
|
|
| load | 加载子树数据的方法 | function(node, resolve) | — | — |
|
|
|
|
|
|
|
|
### props
|
|
|
|
|
|
|
|
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|
|
|
|
|---------- |-------------- |---------- |-------------------------------- |-------- |
|
|
|
|
| label | 指定节点标签为节点对象的某个属性值 | string | — | — |
|
|
|
|
| children | 指定子树为节点对象的某个属性值 | string | — | — |
|