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-11-16 15:35:46 +08:00
|
|
|
|
const data = [{
|
2016-09-04 10:27:09 +08:00
|
|
|
|
label: '一级 1',
|
|
|
|
|
children: [{
|
2017-01-14 16:02:52 +08:00
|
|
|
|
label: '二级 1-1',
|
|
|
|
|
children: [{
|
|
|
|
|
label: '三级 1-1-1'
|
|
|
|
|
}]
|
2016-09-04 10:27:09 +08:00
|
|
|
|
}]
|
|
|
|
|
}, {
|
|
|
|
|
label: '一级 2',
|
2016-07-27 14:15:02 +08:00
|
|
|
|
children: [{
|
2017-01-14 16:02:52 +08:00
|
|
|
|
label: '二级 2-1',
|
|
|
|
|
children: [{
|
|
|
|
|
label: '三级 2-1-1'
|
|
|
|
|
}]
|
2016-09-04 10:27:09 +08:00
|
|
|
|
}, {
|
2017-01-14 16:02:52 +08:00
|
|
|
|
label: '二级 2-2',
|
|
|
|
|
children: [{
|
|
|
|
|
label: '三级 2-2-1'
|
|
|
|
|
}]
|
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: [{
|
2017-01-14 16:02:52 +08:00
|
|
|
|
label: '二级 3-1',
|
|
|
|
|
children: [{
|
|
|
|
|
label: '三级 3-1-1'
|
|
|
|
|
}]
|
2016-07-27 14:15:02 +08:00
|
|
|
|
}, {
|
2017-01-14 16:02:52 +08:00
|
|
|
|
label: '二级 3-2',
|
|
|
|
|
children: [{
|
|
|
|
|
label: '三级 3-2-1'
|
|
|
|
|
}]
|
2016-07-27 14:15:02 +08:00
|
|
|
|
}]
|
|
|
|
|
}];
|
|
|
|
|
|
2016-11-16 15:35:46 +08:00
|
|
|
|
const regions = [{
|
2016-07-27 14:15:02 +08:00
|
|
|
|
'name': 'region1'
|
|
|
|
|
}, {
|
|
|
|
|
'name': 'region2'
|
|
|
|
|
}];
|
|
|
|
|
|
2016-11-16 15:35:46 +08:00
|
|
|
|
let count = 1;
|
2016-07-27 14:15:02 +08:00
|
|
|
|
|
2016-11-16 15:35:46 +08:00
|
|
|
|
const props = {
|
2016-07-27 14:15:02 +08:00
|
|
|
|
label: 'name',
|
2016-11-16 15:35:46 +08:00
|
|
|
|
children: 'zones'
|
2016-07-27 14:15:02 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var defaultProps = {
|
|
|
|
|
children: 'children',
|
|
|
|
|
label: 'label'
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
methods: {
|
2016-10-05 23:06:17 +08:00
|
|
|
|
handleCheckChange(data, checked, indeterminate) {
|
|
|
|
|
console.log(data, checked, indeterminate);
|
|
|
|
|
},
|
|
|
|
|
handleNodeClick(data) {
|
|
|
|
|
console.log(data);
|
|
|
|
|
},
|
2016-07-27 14:15:02 +08:00
|
|
|
|
loadNode(node, resolve) {
|
2016-11-16 15:35:46 +08:00
|
|
|
|
if (node.level === 0) {
|
2016-10-05 23:06:17 +08:00
|
|
|
|
return resolve([{ name: 'region1' }, { name: 'region2' }]);
|
2016-07-27 14:15:02 +08:00
|
|
|
|
}
|
2016-11-16 15:35:46 +08:00
|
|
|
|
if (node.level > 3) return resolve([]);
|
2016-09-21 15:10:27 +08:00
|
|
|
|
var hasChild;
|
2016-10-05 23:06:17 +08:00
|
|
|
|
if (node.data.name === 'region1') {
|
2016-09-21 15:10:27 +08:00
|
|
|
|
hasChild = true;
|
2016-10-05 23:06:17 +08:00
|
|
|
|
} else if (node.data.name === 'region2') {
|
2016-09-21 15:10:27 +08:00
|
|
|
|
hasChild = false;
|
|
|
|
|
} else {
|
|
|
|
|
hasChild = Math.random() > 0.5;
|
|
|
|
|
}
|
2016-07-27 14:15:02 +08:00
|
|
|
|
|
|
|
|
|
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-10-05 23:06:17 +08:00
|
|
|
|
<el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
|
2016-07-27 14:15:02 +08:00
|
|
|
|
|
2016-09-04 10:27:09 +08:00
|
|
|
|
<script>
|
|
|
|
|
export default {
|
|
|
|
|
data() {
|
|
|
|
|
return {
|
|
|
|
|
data: [{
|
|
|
|
|
label: '一级 1',
|
|
|
|
|
children: [{
|
2017-01-14 16:02:52 +08:00
|
|
|
|
label: '二级 1-1',
|
|
|
|
|
children: [{
|
|
|
|
|
label: '三级 1-1-1'
|
|
|
|
|
}]
|
2016-09-04 10:27:09 +08:00
|
|
|
|
}]
|
|
|
|
|
}, {
|
|
|
|
|
label: '一级 2',
|
|
|
|
|
children: [{
|
2017-01-14 16:02:52 +08:00
|
|
|
|
label: '二级 2-1',
|
|
|
|
|
children: [{
|
|
|
|
|
label: '三级 2-1-1'
|
|
|
|
|
}]
|
2016-09-04 10:27:09 +08:00
|
|
|
|
}, {
|
2017-01-14 16:02:52 +08:00
|
|
|
|
label: '二级 2-2',
|
|
|
|
|
children: [{
|
|
|
|
|
label: '三级 2-2-1'
|
|
|
|
|
}]
|
2016-09-04 10:27:09 +08:00
|
|
|
|
}]
|
|
|
|
|
}, {
|
|
|
|
|
label: '一级 3',
|
|
|
|
|
children: [{
|
2017-01-14 16:02:52 +08:00
|
|
|
|
label: '二级 3-1',
|
|
|
|
|
children: [{
|
|
|
|
|
label: '三级 3-1-1'
|
|
|
|
|
}]
|
2016-09-04 10:27:09 +08:00
|
|
|
|
}, {
|
2017-01-14 16:02:52 +08:00
|
|
|
|
label: '二级 3-2',
|
|
|
|
|
children: [{
|
|
|
|
|
label: '三级 3-2-1'
|
|
|
|
|
}]
|
2016-09-04 10:27:09 +08:00
|
|
|
|
}]
|
|
|
|
|
}],
|
|
|
|
|
defaultProps: {
|
|
|
|
|
children: 'children',
|
|
|
|
|
label: 'label'
|
|
|
|
|
}
|
|
|
|
|
};
|
2016-11-13 22:00:55 +08:00
|
|
|
|
},
|
|
|
|
|
methods: {
|
|
|
|
|
handleNodeClick(data) {
|
|
|
|
|
console.log(data);
|
|
|
|
|
}
|
2016-09-04 10:27:09 +08:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
</script>
|
|
|
|
|
```
|
|
|
|
|
:::
|
|
|
|
|
|
|
|
|
|
### 可选择
|
|
|
|
|
|
2016-09-21 15:37:41 +08:00
|
|
|
|
适用于需要选择层级时使用。在下例中,由于在点击时才进行该层数据的获取,导致层级不可预知,如果没有下层数据,则点击后下拉按钮会消失。
|
2016-07-27 14:15:02 +08:00
|
|
|
|
|
2016-09-04 10:27:09 +08:00
|
|
|
|
::: demo
|
|
|
|
|
```html
|
2016-10-05 23:06:17 +08:00
|
|
|
|
<el-tree
|
|
|
|
|
:data="regions"
|
|
|
|
|
:props="props"
|
|
|
|
|
:load="loadNode"
|
|
|
|
|
lazy
|
|
|
|
|
show-checkbox
|
|
|
|
|
@check-change="handleCheckChange">
|
|
|
|
|
</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: {
|
2016-10-05 23:06:17 +08:00
|
|
|
|
handleCheckChange(data, checked, indeterminate) {
|
|
|
|
|
console.log(data, checked, indeterminate);
|
|
|
|
|
},
|
|
|
|
|
handleNodeClick(data) {
|
|
|
|
|
console.log(data);
|
2016-09-04 10:27:09 +08:00
|
|
|
|
},
|
|
|
|
|
loadNode(node, resolve) {
|
2016-11-16 15:35:46 +08:00
|
|
|
|
if (node.level === 0) {
|
2016-10-05 23:06:17 +08:00
|
|
|
|
return resolve([{ name: 'region1' }, { name: 'region2' }]);
|
2016-09-04 10:27:09 +08:00
|
|
|
|
}
|
2016-11-16 15:35:46 +08:00
|
|
|
|
if (node.level > 3) return resolve([]);
|
2016-09-04 10:27:09 +08:00
|
|
|
|
|
2016-10-05 23:06:17 +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-08 15:59:02 +08:00
|
|
|
|
setTimeout(() => {
|
2016-09-04 10:27:09 +08:00
|
|
|
|
var data;
|
|
|
|
|
if (hasChild) {
|
|
|
|
|
data = [{
|
2016-11-08 15:59:02 +08:00
|
|
|
|
name: 'zone' + this.count++
|
2016-09-04 10:27:09 +08:00
|
|
|
|
}, {
|
2016-11-08 15:59:02 +08:00
|
|
|
|
name: 'zone' + this.count++
|
2016-09-04 10:27:09 +08:00
|
|
|
|
}];
|
|
|
|
|
} else {
|
|
|
|
|
data = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resolve(data);
|
|
|
|
|
}, 500);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
</script>
|
|
|
|
|
```
|
|
|
|
|
:::
|
|
|
|
|
|
2017-01-14 16:02:52 +08:00
|
|
|
|
### 手风琴模式
|
|
|
|
|
|
|
|
|
|
每次只打开一个同级树节点展开
|
|
|
|
|
|
|
|
|
|
::: demo
|
|
|
|
|
```html
|
|
|
|
|
<el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
export default {
|
|
|
|
|
data() {
|
|
|
|
|
return {
|
|
|
|
|
data: [{
|
|
|
|
|
label: '一级 1',
|
|
|
|
|
children: [{
|
|
|
|
|
label: '二级 1-1',
|
|
|
|
|
children: [{
|
|
|
|
|
label: '三级 1-1-1'
|
|
|
|
|
}]
|
|
|
|
|
}]
|
|
|
|
|
}, {
|
|
|
|
|
label: '一级 2',
|
|
|
|
|
children: [{
|
|
|
|
|
label: '二级 2-1',
|
|
|
|
|
children: [{
|
|
|
|
|
label: '三级 2-1-1'
|
|
|
|
|
}]
|
|
|
|
|
}, {
|
|
|
|
|
label: '二级 2-2',
|
|
|
|
|
children: [{
|
|
|
|
|
label: '三级 2-2-1'
|
|
|
|
|
}]
|
|
|
|
|
}]
|
|
|
|
|
}, {
|
|
|
|
|
label: '一级 3',
|
|
|
|
|
children: [{
|
|
|
|
|
label: '二级 3-1',
|
|
|
|
|
children: [{
|
|
|
|
|
label: '三级 3-1-1'
|
|
|
|
|
}]
|
|
|
|
|
}, {
|
|
|
|
|
label: '二级 3-2',
|
|
|
|
|
children: [{
|
|
|
|
|
label: '三级 3-2-1'
|
|
|
|
|
}]
|
|
|
|
|
}]
|
|
|
|
|
}],
|
|
|
|
|
defaultProps: {
|
|
|
|
|
children: 'children',
|
|
|
|
|
label: 'label'
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
methods: {
|
|
|
|
|
handleNodeClick(data) {
|
|
|
|
|
console.log(data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
</script>
|
|
|
|
|
```
|
|
|
|
|
:::
|
|
|
|
|
|
2016-09-04 10:27:09 +08:00
|
|
|
|
### Attributes
|
|
|
|
|
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|
|
|
|
|
|---------- |-------------- |---------- |-------------------------------- |-------- |
|
|
|
|
|
| data | 展示数据 | array | — | — |
|
2016-11-16 15:35:46 +08:00
|
|
|
|
| empty-text | 内容为空的时候展示的文本 | String | — | — |
|
|
|
|
|
| node-key | 每个树节点用来作为唯一标识的属性,整颗树应该是唯一的 | String | — | — |
|
2016-09-04 10:27:09 +08:00
|
|
|
|
| props | 配置选项,具体看下表 | object | — | — |
|
|
|
|
|
| load | 加载子树数据的方法 | function(node, resolve) | — | — |
|
2016-12-29 20:53:33 +08:00
|
|
|
|
| render-content | 树节点的内容区的渲染 Function | Function(h, { node } | — | — |
|
|
|
|
|
| highlight-current | 是否高亮当前选中节点,默认值是 false。| boolean | — | false |
|
|
|
|
|
| current-node-key | 当前选中节点的 key,只写属性 | string, number | — | — |
|
|
|
|
|
| default-expand-all | 是否默认展开所有节点 | boolean | — | false |
|
2017-01-03 12:26:41 +08:00
|
|
|
|
| expand-on-click-node | 是否在点击节点的时候展开或者收缩节点, 默认值为 true,如果为 false,则只有点箭头图标的时候才会展开或者收缩节点。 | boolean | — | true |
|
2016-11-16 15:35:46 +08:00
|
|
|
|
| auto-expand-parent | 展开子节点的时候是否自动展开父节点 | boolean | — | true |
|
2016-11-22 23:28:19 +08:00
|
|
|
|
| default-expanded-keys | 默认展开的节点的 key 的数组 | array | — | — |
|
2016-11-16 15:35:46 +08:00
|
|
|
|
| show-checkbox | 节点是否可被选择 | boolean | — | false |
|
|
|
|
|
| check-strictly | 在显示复选框的情况下,是否严格的遵循父子不互相关联的做法,默认为 false | boolean | — | false |
|
|
|
|
|
| default-checked-keys | 默认勾选的节点的 key 的数组 | array | — | — |
|
2016-12-29 20:53:33 +08:00
|
|
|
|
| filter-node-method | 对树节点进行筛选时执行的方法,返回 true 表示这个节点可以显示,返回 false 则表示这个节点会被隐藏 | Function(value, data, node) | — | — |
|
2017-01-14 16:02:52 +08:00
|
|
|
|
| accordion | 是否每次只打开一个同级树节点展开 | boolean | — | false |
|
2016-09-04 10:27:09 +08:00
|
|
|
|
|
|
|
|
|
### props
|
|
|
|
|
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|
|
|
|
|
|---------- |-------------- |---------- |-------------------------------- |-------- |
|
|
|
|
|
| label | 指定节点标签为节点对象的某个属性值 | string | — | — |
|
|
|
|
|
| children | 指定子树为节点对象的某个属性值 | string | — | — |
|
2016-10-05 23:06:17 +08:00
|
|
|
|
|
|
|
|
|
### 方法
|
|
|
|
|
`Tree` 拥有如下方法,返回目前被选中的节点数组:
|
|
|
|
|
| 方法名 | 说明 | 参数 |
|
|
|
|
|
|------|--------|------|
|
2016-11-22 00:08:22 +08:00
|
|
|
|
| filter | 对树节点进行筛选操作 | 接收一个任意类型的参数,该参数会在 filter-node-method 中作为第一个参数 |
|
2016-12-13 22:52:29 +08:00
|
|
|
|
| getCheckedNodes | 若节点可被选择(即 `show-checkbox` 为 `true`),则返回目前被选中的节点所组成的数组 | (leafOnly) 接收一个 boolean 类型的参数,若为 `true` 则仅返回被选中的叶子节点,默认值为 `false` |
|
2016-11-21 21:53:55 +08:00
|
|
|
|
| setCheckedNodes | 设置目前勾选的节点,使用此方法必须设置 node-key 属性 | (nodes) 接收勾选节点数据的数组 |
|
2016-12-13 22:52:29 +08:00
|
|
|
|
| getCheckedKeys | 若节点可被选择(即 `show-checkbox` 为 `true`),则返回目前被选中的节点所组成的数组 | (leafOnly) 接收一个 boolean 类型的参数,若为 `true` 则仅返回被选中的叶子节点的 keys,默认值为 `true` |
|
|
|
|
|
| setCheckedKeys | 通过 keys 设置目前勾选的节点,使用此方法必须设置 node-key 属性 | (keys, leafOnly) 接收两个参数,1. 勾选节点的 key 的数组 2. boolean 类型的参数,若为 `true` 则仅设置叶子节点的选中状态,默认值为 `true` |
|
|
|
|
|
| setChecked | 通过 key / data 设置某个节点的勾选状态,使用此方法必须设置 node-key 属性 | (key/data, checked, deep) 接收三个参数,1. 勾选节点的 key 或者 data 2. boolean 类型,节点是否选中 3. boolean 类型,是否设置子节点 ,默认为 false |
|
2016-10-05 23:06:17 +08:00
|
|
|
|
|
|
|
|
|
### Events
|
|
|
|
|
| 事件名称 | 说明 | 回调参数 |
|
|
|
|
|
|---------- |-------- |---------- |
|
2016-10-18 00:31:37 +08:00
|
|
|
|
| node-click | 节点被点击时的回调 | 共三个参数,依次为:传递给 `data` 属性的数组中该节点所对应的对象、节点对应的 Node、节点组件本身。 |
|
2016-12-13 22:52:29 +08:00
|
|
|
|
| check-change | 节点选中状态发生变化时的回调 | 共三个参数,依次为:传递给 `data` 属性的数组中该节点所对应的对象、节点本身是否被选中、节点的子树中是否有被选中的节点 |
|
2016-12-17 14:09:05 +08:00
|
|
|
|
| current-change | 当前选中节点变化时触发的事件 | 共两个参数,依次为:当前节点的数据,当前节点的 Node 对象 |
|