2016-07-27 14:15:02 +08:00
|
|
|
|
<style>
|
2017-02-08 11:49:54 +08:00
|
|
|
|
.demo-tree {
|
|
|
|
|
.leaf {
|
|
|
|
|
width: 20px;
|
|
|
|
|
background: #ddd;
|
|
|
|
|
}
|
2017-02-25 16:33:22 +08:00
|
|
|
|
|
2017-02-08 11:49:54 +08:00
|
|
|
|
.folder {
|
|
|
|
|
width: 20px;
|
|
|
|
|
background: #888;
|
|
|
|
|
}
|
2017-02-25 16:33:22 +08:00
|
|
|
|
|
2017-02-08 11:49:54 +08:00
|
|
|
|
.buttons {
|
|
|
|
|
margin-top: 20px;
|
|
|
|
|
}
|
2017-02-25 16:33:22 +08:00
|
|
|
|
|
2017-02-08 11:49:54 +08:00
|
|
|
|
.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;
|
|
|
|
|
}
|
2016-07-27 14:15:02 +08:00
|
|
|
|
}
|
|
|
|
|
</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
|
|
|
|
}]
|
|
|
|
|
}];
|
|
|
|
|
|
2017-02-08 11:49:54 +08:00
|
|
|
|
const data2 = [{
|
|
|
|
|
id: 1,
|
|
|
|
|
label: '一级 1',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 4,
|
|
|
|
|
label: '二级 1-1',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 9,
|
|
|
|
|
label: '三级 1-1-1'
|
|
|
|
|
}, {
|
|
|
|
|
id: 10,
|
|
|
|
|
label: '三级 1-1-2'
|
|
|
|
|
}]
|
|
|
|
|
}]
|
|
|
|
|
}, {
|
|
|
|
|
id: 2,
|
|
|
|
|
label: '一级 2',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 5,
|
|
|
|
|
label: '二级 2-1'
|
|
|
|
|
}, {
|
|
|
|
|
id: 6,
|
|
|
|
|
label: '二级 2-2'
|
|
|
|
|
}]
|
|
|
|
|
}, {
|
|
|
|
|
id: 3,
|
|
|
|
|
label: '一级 3',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 7,
|
|
|
|
|
label: '二级 3-1'
|
|
|
|
|
}, {
|
|
|
|
|
id: 8,
|
|
|
|
|
label: '二级 3-2'
|
|
|
|
|
}]
|
|
|
|
|
}];
|
|
|
|
|
|
2017-07-19 18:36:26 +08:00
|
|
|
|
const data3 = [{
|
|
|
|
|
id: 1,
|
|
|
|
|
label: '一级 2',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 3,
|
|
|
|
|
label: '二级 2-1',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 4,
|
|
|
|
|
label: '三级 3-1-1'
|
|
|
|
|
}, {
|
|
|
|
|
id: 5,
|
|
|
|
|
label: '三级 3-1-2',
|
|
|
|
|
disabled: true
|
|
|
|
|
}]
|
|
|
|
|
}, {
|
|
|
|
|
id: 2,
|
|
|
|
|
label: '二级 2-2',
|
|
|
|
|
disabled: true,
|
|
|
|
|
children: [{
|
|
|
|
|
id: 6,
|
|
|
|
|
label: '三级 3-2-1'
|
|
|
|
|
}, {
|
|
|
|
|
id: 7,
|
|
|
|
|
label: '三级 3-2-2',
|
|
|
|
|
disabled: true
|
|
|
|
|
}]
|
|
|
|
|
}]
|
|
|
|
|
}];
|
|
|
|
|
|
2018-03-20 13:42:04 +08:00
|
|
|
|
const data6 = [{
|
|
|
|
|
id: 1,
|
|
|
|
|
label: '一级 1',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 4,
|
|
|
|
|
label: '二级 1-1',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 9,
|
|
|
|
|
label: '三级 1-1-1'
|
|
|
|
|
}, {
|
|
|
|
|
id: 10,
|
|
|
|
|
label: '三级 1-1-2'
|
|
|
|
|
}]
|
|
|
|
|
}]
|
|
|
|
|
}, {
|
|
|
|
|
id: 2,
|
|
|
|
|
label: '一级 2',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 5,
|
|
|
|
|
label: '二级 2-1'
|
|
|
|
|
}, {
|
|
|
|
|
id: 6,
|
|
|
|
|
label: '二级 2-2'
|
|
|
|
|
}]
|
|
|
|
|
}, {
|
|
|
|
|
id: 3,
|
|
|
|
|
label: '一级 3',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 7,
|
|
|
|
|
label: '二级 3-1'
|
|
|
|
|
}, {
|
|
|
|
|
id: 8,
|
|
|
|
|
label: '二级 3-2',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 11,
|
|
|
|
|
label: '三级 3-2-1'
|
|
|
|
|
}, {
|
|
|
|
|
id: 12,
|
|
|
|
|
label: '三级 3-2-2'
|
|
|
|
|
}, {
|
|
|
|
|
id: 13,
|
|
|
|
|
label: '三级 3-2-3'
|
|
|
|
|
}]
|
|
|
|
|
}]
|
|
|
|
|
}];
|
|
|
|
|
|
2017-02-08 11:49:54 +08:00
|
|
|
|
let id = 1000;
|
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
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-07-27 14:15:02 +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-07-27 14:15:02 +08:00
|
|
|
|
methods: {
|
2016-10-05 23:06:17 +08:00
|
|
|
|
handleCheckChange(data, checked, indeterminate) {
|
|
|
|
|
console.log(data, checked, indeterminate);
|
|
|
|
|
},
|
|
|
|
|
handleNodeClick(data) {
|
|
|
|
|
console.log(data);
|
|
|
|
|
},
|
2018-03-20 13:42:04 +08:00
|
|
|
|
handleDragStart(node, ev) {
|
|
|
|
|
console.log('drag start', node);
|
|
|
|
|
},
|
|
|
|
|
handleDragEnter(node, ev) {
|
|
|
|
|
console.log('tree drag enter: ', node.label);
|
|
|
|
|
},
|
|
|
|
|
handleDragLeave(node, ev) {
|
|
|
|
|
console.log('tree drag leave: ', node.label);
|
|
|
|
|
},
|
|
|
|
|
handleDragEnd(from, target, position, ev) {
|
|
|
|
|
console.log('tree drag end: ', target.label);
|
|
|
|
|
if (position !== null) {
|
|
|
|
|
console.log(`target position: parent node: ${position.parent.label}, index: ${position.index}`);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
allowDrop(from, target) {
|
|
|
|
|
return target.data.label !== '二级 3-1';
|
|
|
|
|
},
|
|
|
|
|
allowDrag(node) {
|
|
|
|
|
return node.data.label.indexOf('三级 3-1-1') === -1;
|
|
|
|
|
},
|
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;
|
|
|
|
|
}
|
2017-02-25 16:33:22 +08:00
|
|
|
|
|
2016-07-27 14:15:02 +08:00
|
|
|
|
setTimeout(function() {
|
|
|
|
|
var data;
|
|
|
|
|
if (hasChild) {
|
|
|
|
|
data = [{
|
|
|
|
|
name: 'zone' + count++
|
|
|
|
|
}, {
|
|
|
|
|
name: 'zone' + count++
|
|
|
|
|
}];
|
|
|
|
|
} else {
|
|
|
|
|
data = [];
|
|
|
|
|
}
|
2017-02-25 16:33:22 +08:00
|
|
|
|
|
2016-07-27 14:15:02 +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,
|
|
|
|
|
label: '二级 2-1'
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: 9,
|
|
|
|
|
label: '三级 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-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-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-10-24 15:24:35 +08:00
|
|
|
|
renderContent(h, { node, data }) {
|
2017-02-08 11:49:54 +08:00
|
|
|
|
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-07-27 14:15:02 +08:00
|
|
|
|
}
|
|
|
|
|
},
|
2017-02-25 16:33:22 +08:00
|
|
|
|
|
2016-07-27 14:15:02 +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)),
|
2018-03-20 13:42:04 +08:00
|
|
|
|
data6,
|
2016-07-27 14:15:02 +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-07-27 14:15:02 +08:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
</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
|
|
|
|
基础的树形结构展示。
|
|
|
|
|
|
2017-12-12 12:45:09 +08:00
|
|
|
|
:::demo
|
2016-09-04 10:27:09 +08:00
|
|
|
|
```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>
|
|
|
|
|
```
|
|
|
|
|
:::
|
|
|
|
|
|
|
|
|
|
### 可选择
|
|
|
|
|
|
2017-10-25 11:58:58 +08:00
|
|
|
|
适用于需要选择层级时使用。
|
2016-07-27 14:15:02 +08:00
|
|
|
|
|
2017-12-12 12:45:09 +08:00
|
|
|
|
:::demo 本例还展示了动态加载节点数据的方法。
|
2016-09-04 10:27:09 +08:00
|
|
|
|
```html
|
2016-10-05 23:06:17 +08:00
|
|
|
|
<el-tree
|
|
|
|
|
: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 {
|
|
|
|
|
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-08-05 17:25:51 +08:00
|
|
|
|
### 懒加载自定义叶子节点
|
|
|
|
|
|
2017-12-12 12:45:09 +08:00
|
|
|
|
:::demo 由于在点击节点时才进行该层数据的获取,默认情况下 Tree 无法预知某个节点是否为叶子节点,所以会为每个节点添加一个下拉按钮,如果节点没有下层数据,则点击后下拉按钮会消失。同时,你也可以提前告知 Tree 某个节点是否为叶子节点,从而避免在叶子节点前渲染下拉按钮。
|
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-02-08 11:49:54 +08:00
|
|
|
|
### 默认展开和默认选中
|
|
|
|
|
可将 Tree 的某些节点设置为默认展开或默认选中
|
|
|
|
|
|
2017-12-12 12:45:09 +08:00
|
|
|
|
:::demo 分别通过`default-expanded-keys`和`default-checked-keys`设置默认展开和默认选中的节点。需要注意的是,此时必须设置`node-key`,其值为节点数据中的一个字段名,该字段在整棵树中是唯一的。
|
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: '一级 1',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 4,
|
|
|
|
|
label: '二级 1-1',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 9,
|
|
|
|
|
label: '三级 1-1-1'
|
|
|
|
|
}, {
|
|
|
|
|
id: 10,
|
|
|
|
|
label: '三级 1-1-2'
|
|
|
|
|
}]
|
|
|
|
|
}]
|
|
|
|
|
}, {
|
|
|
|
|
id: 2,
|
|
|
|
|
label: '一级 2',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 5,
|
|
|
|
|
label: '二级 2-1'
|
|
|
|
|
}, {
|
|
|
|
|
id: 6,
|
|
|
|
|
label: '二级 2-2'
|
|
|
|
|
}]
|
|
|
|
|
}, {
|
|
|
|
|
id: 3,
|
|
|
|
|
label: '一级 3',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 7,
|
|
|
|
|
label: '二级 3-1'
|
|
|
|
|
}, {
|
|
|
|
|
id: 8,
|
|
|
|
|
label: '二级 3-2'
|
|
|
|
|
}]
|
|
|
|
|
}],
|
|
|
|
|
defaultProps: {
|
|
|
|
|
children: 'children',
|
|
|
|
|
label: 'label'
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
</script>
|
|
|
|
|
```
|
|
|
|
|
:::
|
|
|
|
|
|
2017-07-19 18:36:26 +08:00
|
|
|
|
### 禁用状态
|
|
|
|
|
可将 Tree 的某些节点设置为禁用状态
|
|
|
|
|
|
2017-12-12 12:45:09 +08:00
|
|
|
|
:::demo 通过`disabled`设置禁用状态。
|
2017-07-19 18:36:26 +08:00
|
|
|
|
```html
|
|
|
|
|
<el-tree
|
|
|
|
|
:data="data3"
|
|
|
|
|
show-checkbox
|
|
|
|
|
node-key="id"
|
|
|
|
|
:default-expanded-keys="[2, 3]"
|
|
|
|
|
:default-checked-keys="[5]">
|
|
|
|
|
</el-tree>
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
export default {
|
|
|
|
|
data() {
|
|
|
|
|
return {
|
|
|
|
|
data3: [{
|
|
|
|
|
id: 1,
|
|
|
|
|
label: '一级 2',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 3,
|
|
|
|
|
label: '二级 2-1',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 4,
|
|
|
|
|
label: '三级 3-1-1'
|
|
|
|
|
}, {
|
|
|
|
|
id: 5,
|
|
|
|
|
label: '三级 3-1-2',
|
|
|
|
|
disabled: true
|
|
|
|
|
}]
|
|
|
|
|
}, {
|
|
|
|
|
id: 2,
|
|
|
|
|
label: '二级 2-2',
|
|
|
|
|
disabled: true,
|
|
|
|
|
children: [{
|
|
|
|
|
id: 6,
|
|
|
|
|
label: '三级 3-2-1'
|
|
|
|
|
}, {
|
|
|
|
|
id: 7,
|
|
|
|
|
label: '三级 3-2-2',
|
|
|
|
|
disabled: true
|
|
|
|
|
}]
|
|
|
|
|
}]
|
|
|
|
|
}],
|
|
|
|
|
defaultProps: {
|
|
|
|
|
children: 'children',
|
|
|
|
|
label: 'label'
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
</script>
|
|
|
|
|
```
|
|
|
|
|
:::
|
|
|
|
|
|
2017-02-08 11:49:54 +08:00
|
|
|
|
### 树节点的选择
|
|
|
|
|
|
2017-12-12 12:45:09 +08:00
|
|
|
|
:::demo 本例展示如何获取和设置选中节点。获取和设置各有两种方式:通过 node 或通过 key。如果需要通过 key 来获取或设置,则必须设置`node-key`。
|
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">通过 node 获取</el-button>
|
|
|
|
|
<el-button @click="getCheckedKeys">通过 key 获取</el-button>
|
|
|
|
|
<el-button @click="setCheckedNodes">通过 node 设置</el-button>
|
|
|
|
|
<el-button @click="setCheckedKeys">通过 key 设置</el-button>
|
|
|
|
|
<el-button @click="resetChecked">清空</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: '二级 2-1'
|
|
|
|
|
}, {
|
|
|
|
|
id: 9,
|
|
|
|
|
label: '三级 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: '一级 1',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 4,
|
|
|
|
|
label: '二级 1-1',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 9,
|
|
|
|
|
label: '三级 1-1-1'
|
|
|
|
|
}, {
|
|
|
|
|
id: 10,
|
|
|
|
|
label: '三级 1-1-2'
|
|
|
|
|
}]
|
|
|
|
|
}]
|
|
|
|
|
}, {
|
|
|
|
|
id: 2,
|
|
|
|
|
label: '一级 2',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 5,
|
|
|
|
|
label: '二级 2-1'
|
|
|
|
|
}, {
|
|
|
|
|
id: 6,
|
|
|
|
|
label: '二级 2-2'
|
|
|
|
|
}]
|
|
|
|
|
}, {
|
|
|
|
|
id: 3,
|
|
|
|
|
label: '一级 3',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 7,
|
|
|
|
|
label: '二级 3-1'
|
|
|
|
|
}, {
|
|
|
|
|
id: 8,
|
|
|
|
|
label: '二级 3-2'
|
|
|
|
|
}]
|
|
|
|
|
}],
|
|
|
|
|
defaultProps: {
|
|
|
|
|
children: 'children',
|
|
|
|
|
label: 'label'
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
</script>
|
|
|
|
|
```
|
|
|
|
|
:::
|
|
|
|
|
|
|
|
|
|
### 自定义节点内容
|
|
|
|
|
节点的内容支持自定义,可以在节点区添加按钮或图标等内容
|
|
|
|
|
|
2018-02-06 16:45:27 +08:00
|
|
|
|
:::demo 可以通过两种方法进行树节点内容的自定义:`render-content`和 scoped slot。使用`render-content`指定渲染函数,该函数返回需要的节点区内容即可。渲染函数的用法请参考 Vue 文档。使用 scoped slot 会传入两个参数`node`和`data`,分别表示当前节点的 Node 对象和当前节点的数据。注意:由于 jsfiddle 不支持 JSX 语法,所以`render-content`示例在 jsfiddle 中无法运行。但是在实际的项目中,只要正确地配置了相关依赖,就可以正常运行。
|
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>使用 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>使用 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;
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
data() {
|
2018-02-06 16:45:27 +08:00
|
|
|
|
const data = [{
|
|
|
|
|
id: 1,
|
|
|
|
|
label: '一级 1',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 4,
|
|
|
|
|
label: '二级 1-1',
|
2017-02-08 11:49:54 +08:00
|
|
|
|
children: [{
|
2018-02-06 16:45:27 +08:00
|
|
|
|
id: 9,
|
|
|
|
|
label: '三级 1-1-1'
|
2017-02-08 11:49:54 +08:00
|
|
|
|
}, {
|
2018-02-06 16:45:27 +08:00
|
|
|
|
id: 10,
|
|
|
|
|
label: '三级 1-1-2'
|
2017-02-08 11:49:54 +08:00
|
|
|
|
}]
|
2018-02-06 16:45:27 +08:00
|
|
|
|
}]
|
|
|
|
|
}, {
|
|
|
|
|
id: 2,
|
|
|
|
|
label: '一级 2',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 5,
|
|
|
|
|
label: '二级 2-1'
|
2017-02-08 11:49:54 +08:00
|
|
|
|
}, {
|
2018-02-06 16:45:27 +08:00
|
|
|
|
id: 6,
|
|
|
|
|
label: '二级 2-2'
|
|
|
|
|
}]
|
|
|
|
|
}, {
|
|
|
|
|
id: 3,
|
|
|
|
|
label: '一级 3',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 7,
|
|
|
|
|
label: '二级 3-1'
|
|
|
|
|
}, {
|
|
|
|
|
id: 8,
|
|
|
|
|
label: '二级 3-2'
|
|
|
|
|
}]
|
|
|
|
|
}];
|
|
|
|
|
return {
|
|
|
|
|
data4: JSON.parse(JSON.stringify(data)),
|
|
|
|
|
data5: JSON.parse(JSON.stringify(data))
|
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-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
|
|
|
|
```
|
|
|
|
|
:::
|
|
|
|
|
|
|
|
|
|
### 节点过滤
|
|
|
|
|
通过关键字过滤树节点
|
|
|
|
|
|
2017-12-12 12:45:09 +08:00
|
|
|
|
:::demo 在需要对节点进行过滤时,调用 Tree 实例的`filter`方法,参数为关键字。需要注意的是,此时需要设置`filter-node-method`,值为过滤函数。
|
2017-02-08 11:49:54 +08:00
|
|
|
|
```html
|
|
|
|
|
<el-input
|
|
|
|
|
placeholder="输入关键字进行过滤"
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
},
|
2017-02-25 16:33:22 +08:00
|
|
|
|
|
2017-02-08 11:49:54 +08:00
|
|
|
|
data() {
|
|
|
|
|
return {
|
|
|
|
|
filterText: '',
|
|
|
|
|
data2: [{
|
|
|
|
|
id: 1,
|
|
|
|
|
label: '一级 1',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 4,
|
|
|
|
|
label: '二级 1-1',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 9,
|
|
|
|
|
label: '三级 1-1-1'
|
|
|
|
|
}, {
|
|
|
|
|
id: 10,
|
|
|
|
|
label: '三级 1-1-2'
|
|
|
|
|
}]
|
|
|
|
|
}]
|
|
|
|
|
}, {
|
|
|
|
|
id: 2,
|
|
|
|
|
label: '一级 2',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 5,
|
|
|
|
|
label: '二级 2-1'
|
|
|
|
|
}, {
|
|
|
|
|
id: 6,
|
|
|
|
|
label: '二级 2-2'
|
|
|
|
|
}]
|
|
|
|
|
}, {
|
|
|
|
|
id: 3,
|
|
|
|
|
label: '一级 3',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 7,
|
|
|
|
|
label: '二级 3-1'
|
|
|
|
|
}, {
|
|
|
|
|
id: 8,
|
|
|
|
|
label: '二级 3-2'
|
|
|
|
|
}]
|
|
|
|
|
}],
|
|
|
|
|
defaultProps: {
|
|
|
|
|
children: 'children',
|
|
|
|
|
label: 'label'
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
</script>
|
|
|
|
|
```
|
|
|
|
|
:::
|
|
|
|
|
|
2017-01-14 16:02:52 +08:00
|
|
|
|
### 手风琴模式
|
|
|
|
|
|
2017-04-24 13:44:27 +08:00
|
|
|
|
对于同一级的节点,每次只能展开一个
|
2017-01-14 16:02:52 +08:00
|
|
|
|
|
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: '一级 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>
|
|
|
|
|
```
|
|
|
|
|
:::
|
|
|
|
|
|
2018-03-20 13:42:04 +08:00
|
|
|
|
### 可拖拽节点
|
|
|
|
|
|
|
|
|
|
通过draggable属性可让节点变为可拖拽,节点只能放到相同level节点旁边。
|
|
|
|
|
|
|
|
|
|
:::demo
|
|
|
|
|
```html
|
|
|
|
|
<el-tree
|
|
|
|
|
:data="data6"
|
|
|
|
|
node-key="id"
|
|
|
|
|
default-expand-all
|
|
|
|
|
@node-drag-start="handleDragStart"
|
|
|
|
|
@node-drag-enter="handleDragEnter"
|
|
|
|
|
@node-drag-leave="handleDragLeave"
|
|
|
|
|
@node-drag-end="handleDragEnd"
|
|
|
|
|
draggable
|
|
|
|
|
:allow-drop="allowDrop"
|
|
|
|
|
:allow-drag="allowDrag">
|
|
|
|
|
</el-tree>
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
export default {
|
|
|
|
|
data() {
|
|
|
|
|
return {
|
|
|
|
|
data6: [{
|
|
|
|
|
id: 1,
|
|
|
|
|
label: '一级 1',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 4,
|
|
|
|
|
label: '二级 1-1',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 9,
|
|
|
|
|
label: '三级 1-1-1'
|
|
|
|
|
}, {
|
|
|
|
|
id: 10,
|
|
|
|
|
label: '三级 1-1-2'
|
|
|
|
|
}]
|
|
|
|
|
}]
|
|
|
|
|
}, {
|
|
|
|
|
id: 2,
|
|
|
|
|
label: '一级 2',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 5,
|
|
|
|
|
label: '二级 2-1'
|
|
|
|
|
}, {
|
|
|
|
|
id: 6,
|
|
|
|
|
label: '二级 2-2'
|
|
|
|
|
}]
|
|
|
|
|
}, {
|
|
|
|
|
id: 3,
|
|
|
|
|
label: '一级 3',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 7,
|
|
|
|
|
label: '二级 3-1'
|
|
|
|
|
}, {
|
|
|
|
|
id: 8,
|
|
|
|
|
label: '二级 3-2',
|
|
|
|
|
children: [{
|
|
|
|
|
id: 11,
|
|
|
|
|
label: '三级 3-2-1'
|
|
|
|
|
}, {
|
|
|
|
|
id: 12,
|
|
|
|
|
label: '三级 3-2-2'
|
|
|
|
|
}, {
|
|
|
|
|
id: 13,
|
|
|
|
|
label: '三级 3-2-3'
|
|
|
|
|
}]
|
|
|
|
|
}]
|
|
|
|
|
}],
|
|
|
|
|
defaultProps: {
|
|
|
|
|
children: 'children',
|
|
|
|
|
label: 'label'
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
methods: {
|
|
|
|
|
handleDragStart(node, ev) {
|
|
|
|
|
console.log('drag start', node);
|
|
|
|
|
},
|
|
|
|
|
handleDragEnter(node, ev) {
|
|
|
|
|
console.log('tree drag enter: ', node.label);
|
|
|
|
|
},
|
|
|
|
|
handleDragLeave(node, ev) {
|
|
|
|
|
console.log('tree drag leave: ', node.label);
|
|
|
|
|
},
|
|
|
|
|
handleDragEnd(from, target, position, ev) {
|
|
|
|
|
console.log('tree drag end: ', target.label);
|
|
|
|
|
if (position !== null) {
|
|
|
|
|
console.log(`target position: parent node: ${position.parent.label}, index: ${position.index}`);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
allowDrop(from, target) {
|
|
|
|
|
return target.data.label !== '二级 3-1';
|
|
|
|
|
},
|
|
|
|
|
allowDrag(node) {
|
|
|
|
|
return node.data.label.indexOf('三级 3-1-1') === -1;
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
</script>
|
|
|
|
|
```
|
|
|
|
|
:::
|
|
|
|
|
|
2016-09-04 10:27:09 +08:00
|
|
|
|
### Attributes
|
2018-01-14 20:40:52 +08:00
|
|
|
|
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|
2017-01-18 17:49:32 +08:00
|
|
|
|
| --------------------- | ---------------------------------------- | --------------------------- | ---- | ----- |
|
2018-01-14 20:40:52 +08:00
|
|
|
|
| data | 展示数据 | array | — | — |
|
|
|
|
|
| empty-text | 内容为空的时候展示的文本 | String | — | — |
|
2017-09-12 11:20:02 +08:00
|
|
|
|
| node-key | 每个树节点用来作为唯一标识的属性,整棵树应该是唯一的 | String | — | — |
|
2017-01-18 17:49:32 +08:00
|
|
|
|
| props | 配置选项,具体看下表 | object | — | — |
|
2017-12-24 18:59:09 +08:00
|
|
|
|
| render-after-expand | 是否在第一次展开某个树节点后才渲染其子节点 | boolean | — | true |
|
2018-01-14 20:40:52 +08:00
|
|
|
|
| load | 加载子树数据的方法,仅当 lazy 属性为true 时生效 | function(node, resolve) | — | — |
|
2017-10-24 15:24:35 +08:00
|
|
|
|
| render-content | 树节点的内容区的渲染 Function | Function(h, { node, data, store } | — | — |
|
2018-01-14 20:40:52 +08:00
|
|
|
|
| highlight-current | 是否高亮当前选中节点,默认值是 false。 | boolean | — | false |
|
2017-01-18 17:49:32 +08:00
|
|
|
|
| default-expand-all | 是否默认展开所有节点 | boolean | — | false |
|
|
|
|
|
| expand-on-click-node | 是否在点击节点的时候展开或者收缩节点, 默认值为 true,如果为 false,则只有点箭头图标的时候才会展开或者收缩节点。 | boolean | — | true |
|
2018-01-14 20:40:52 +08:00
|
|
|
|
| auto-expand-parent | 展开子节点的时候是否自动展开父节点 | boolean | — | true |
|
|
|
|
|
| default-expanded-keys | 默认展开的节点的 key 的数组 | array | — | — |
|
|
|
|
|
| show-checkbox | 节点是否可被选择 | boolean | — | false |
|
2017-01-18 17:49:32 +08:00
|
|
|
|
| check-strictly | 在显示复选框的情况下,是否严格的遵循父子不互相关联的做法,默认为 false | boolean | — | false |
|
2018-01-14 20:40:52 +08:00
|
|
|
|
| default-checked-keys | 默认勾选的节点的 key 的数组 | array | — | — |
|
2017-01-18 17:49:32 +08:00
|
|
|
|
| filter-node-method | 对树节点进行筛选时执行的方法,返回 true 表示这个节点可以显示,返回 false 则表示这个节点会被隐藏 | Function(value, data, node) | — | — |
|
2018-01-14 20:40:52 +08:00
|
|
|
|
| accordion | 是否每次只打开一个同级树节点展开 | boolean | — | false |
|
|
|
|
|
| indent | 相邻级节点间的水平缩进,单位为像素 | number | — | 16 |
|
|
|
|
|
| lazy | 是否懒加载子节点,需与 load 方法结合使用 | boolean | — | false |
|
2018-03-20 13:42:04 +08:00
|
|
|
|
| draggable | 是否开启拖拽节点功能 | boolean | — | false |
|
|
|
|
|
| allow-drag | 判断节点能否被拖拽 | Function(Node) | — | — |
|
|
|
|
|
| allow-drop | 拖拽时判定位置能否被放置 | Function(fromNode, toNode) | — | — |
|
2016-09-04 10:27:09 +08:00
|
|
|
|
|
|
|
|
|
### props
|
2017-01-18 17:49:32 +08:00
|
|
|
|
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|
|
|
|
|
| -------- | ----------------- | ------ | ---- | ---- |
|
2017-07-20 18:40:06 +08:00
|
|
|
|
| label | 指定节点标签为节点对象的某个属性值 | string, function(data, node) | — | — |
|
2018-02-09 10:29:32 +08:00
|
|
|
|
| children | 指定子树为节点对象的某个属性值 | string | — | — |
|
2017-09-29 16:21:29 +08:00
|
|
|
|
| disabled | 指定节点选择框是否禁用为节点对象的某个属性值 | boolean, function(data, node) | — | — |
|
2018-02-09 10:29:32 +08:00
|
|
|
|
| isLeaf | 指定节点是否为叶子节点,仅在指定了 lazy 属性的情况下生效 | boolean, function(data, node) | — | — |
|
2016-10-05 23:06:17 +08:00
|
|
|
|
|
|
|
|
|
### 方法
|
2018-02-09 10:28:43 +08:00
|
|
|
|
`Tree` 内部使用了 Node 类型的对象来包装用户传入的数据,用来保存目前节点的状态。
|
|
|
|
|
`Tree` 拥有如下方法:
|
|
|
|
|
|
2017-01-18 17:49:32 +08:00
|
|
|
|
| 方法名 | 说明 | 参数 |
|
|
|
|
|
| --------------- | ---------------------------------------- | ---------------------------------------- |
|
|
|
|
|
| filter | 对树节点进行筛选操作 | 接收一个任意类型的参数,该参数会在 filter-node-method 中作为第一个参数 |
|
2017-07-21 16:25:28 +08:00
|
|
|
|
| updateKeyChildren | 通过 keys 设置节点子元素,使用此方法必须设置 node-key 属性 | (key, data) 接收两个参数,1. 节点 key 2. 节点数据的数组 |
|
2016-12-13 22:52:29 +08:00
|
|
|
|
| getCheckedNodes | 若节点可被选择(即 `show-checkbox` 为 `true`),则返回目前被选中的节点所组成的数组 | (leafOnly) 接收一个 boolean 类型的参数,若为 `true` 则仅返回被选中的叶子节点,默认值为 `false` |
|
2017-01-18 17:49:32 +08:00
|
|
|
|
| setCheckedNodes | 设置目前勾选的节点,使用此方法必须设置 node-key 属性 | (nodes) 接收勾选节点数据的数组 |
|
2018-02-09 10:31:11 +08:00
|
|
|
|
| getCheckedKeys | 若节点可被选择(即 `show-checkbox` 为 `true`),则返回目前被选中的节点的 key 所组成的数组 | (leafOnly) 接收一个 boolean 类型的参数,若为 `true` 则仅返回被选中的叶子节点的 keys,默认值为 `false` |
|
2017-02-25 16:33:22 +08:00
|
|
|
|
| setCheckedKeys | 通过 keys 设置目前勾选的节点,使用此方法必须设置 node-key 属性 | (keys, leafOnly) 接收两个参数,1. 勾选节点的 key 的数组 2. boolean 类型的参数,若为 `true` 则仅设置叶子节点的选中状态,默认值为 `false` |
|
2017-01-18 17:49:32 +08:00
|
|
|
|
| setChecked | 通过 key / data 设置某个节点的勾选状态,使用此方法必须设置 node-key 属性 | (key/data, checked, deep) 接收三个参数,1. 勾选节点的 key 或者 data 2. boolean 类型,节点是否选中 3. boolean 类型,是否设置子节点 ,默认为 false |
|
2018-02-09 10:31:11 +08:00
|
|
|
|
| getHalfCheckedNodes | 若节点可被选择(即 `show-checkbox` 为 `true`),则返回目前半选中的节点所组成的数组 | - |
|
|
|
|
|
| getHalfCheckedKeys | 若节点可被选择(即 `show-checkbox` 为 `true`),则返回目前半选中的节点的 key 所组成的数组 | - |
|
2017-07-23 17:28:26 +08:00
|
|
|
|
| getCurrentKey | 获取当前被选中节点的 key,使用此方法必须设置 node-key 属性,若没有节点被选中则返回 null | — |
|
|
|
|
|
| getCurrentNode | 获取当前被选中节点的 node,若没有节点被选中则返回 null | — |
|
|
|
|
|
| setCurrentKey | 通过 key 设置某个节点的当前选中状态,使用此方法必须设置 node-key 属性 | (key) 待被选节点的 key |
|
|
|
|
|
| setCurrentNode | 通过 node 设置某个节点的当前选中状态,使用此方法必须设置 node-key 属性 | (node) 待被选节点的 node |
|
2018-02-09 10:28:43 +08:00
|
|
|
|
| getNode | 根据 data 或者 key 拿到 Tree 组件中的 node | (data) 要获得 node 的 key 或者 data |
|
|
|
|
|
| remove | 删除 Tree 中的一个节点 | (data) 要删除的节点的 data、key 或者 node |
|
|
|
|
|
| append | 为 Tree 中的一个节点追加一个子节点 | (data, parentNode) 接收两个参数,1. 要追加的子节点的 data 2. 子节点的 parent 的 data、key 或者 node |
|
|
|
|
|
| insertBefore | 为 Tree 的一个节点的前面增加一个节点 | (data, refNode) 接收两个参数,1. 要增加的节点的 data 2. 要增加的节点的后一个节点的 data、key 或者 node |
|
|
|
|
|
| insertAfter | 为 Tree 的一个节点的后面增加一个节点 | (data, refNode) 接收两个参数,1. 要增加的节点的 data 2. 要增加的节点的前一个节点的 data、key 或者 node |
|
2016-10-05 23:06:17 +08:00
|
|
|
|
|
|
|
|
|
### Events
|
2017-01-18 17:49:32 +08:00
|
|
|
|
| 事件名称 | 说明 | 回调参数 |
|
|
|
|
|
| -------------- | -------------- | ---------------------------------------- |
|
|
|
|
|
| node-click | 节点被点击时的回调 | 共三个参数,依次为:传递给 `data` 属性的数组中该节点所对应的对象、节点对应的 Node、节点组件本身。 |
|
2018-02-06 11:42:01 +08:00
|
|
|
|
| node-contextmenu | 当某一节点被鼠标右键点击时会触发该事件 | 共四个参数,依次为:event、传递给 `data` 属性的数组中该节点所对应的对象、节点对应的 Node、节点组件本身。 |
|
2017-01-18 17:49:32 +08:00
|
|
|
|
| check-change | 节点选中状态发生变化时的回调 | 共三个参数,依次为:传递给 `data` 属性的数组中该节点所对应的对象、节点本身是否被选中、节点的子树中是否有被选中的节点 |
|
2018-02-09 10:31:11 +08:00
|
|
|
|
| check | 当复选框被点击的时候触发 | 共两个参数,依次为:传递给 `data` 属性的数组中该节点所对应的对象、树目前的选中状态对象,包含 checkedNodes、checkedKeys、halfCheckedNodes、halfCheckedKeys 四个属性 |
|
2017-01-18 17:49:32 +08:00
|
|
|
|
| current-change | 当前选中节点变化时触发的事件 | 共两个参数,依次为:当前节点的数据,当前节点的 Node 对象 |
|
2017-01-24 10:37:44 +08:00
|
|
|
|
| node-expand | 节点被展开时触发的事件 | 共三个参数,依次为:传递给 `data` 属性的数组中该节点所对应的对象、节点对应的 Node、节点组件本身。 |
|
|
|
|
|
| node-collapse | 节点被关闭时触发的事件 | 共三个参数,依次为:传递给 `data` 属性的数组中该节点所对应的对象、节点对应的 Node、节点组件本身。 |
|
2018-03-20 13:42:04 +08:00
|
|
|
|
| node-drag-start| 节点开始拖拽时触发的事件 | 共两个参数,依次为:被拖拽节点对应的 Node、Vue传来的drag event。 |
|
|
|
|
|
| node-drag-enter| 拖拽进入其他节点时触发的事件 | 共两个参数,依次为:所进入节点对应的 Node、Vue传来的drag event。 |
|
|
|
|
|
| node-drag-leave| 拖拽离开某个节点时触发的事件 | 共两个参数,依次为:所离开节点对应的 Node、Vue传来的drag event。(注意:上个节点的leave事件有可能在下个节点enter之后执行) |
|
|
|
|
|
| node-drag-end | 拖拽结束时触发的事件 | 共四个参数,依次为:被拖拽节点对应的 Node、结束拖拽时最后指向的节点、被拖拽节点的放置位置{ parent: 位置的父节点, index: 在父节点中的序号 }、Vue传来的drag event。|
|
2018-02-06 16:45:27 +08:00
|
|
|
|
|
|
|
|
|
### Scoped slot
|
|
|
|
|
| name | 说明 |
|
|
|
|
|
|------|--------|
|
|
|
|
|
| — | 自定义树节点的内容,参数为 { node, data } |
|