1.树状数据点击加载
<el-tree class="filter-tree" :props="defaultProps" :filter-node-method="filterNode"
ref="tree" :load="loadNode" lazy @node-click="handleNodeClick">
</el-tree>
export default {
data() {
return {
defaultProps: {
children: '接收key(ID或)',
label: 'label'
}
}
}
methods:{
loadNode(node, resolve) {
setTimeout(() => {
if (node.level == 0) {
findRegionOrgByPermission().then(response => {
const cities = response.data.map((value, i) => ({
value: value.regionId,
isSubordinateUnits: value.isSubordinateUnits,
label: value.name,
leaf: node.level >= 2
}));
resolve(cities);
});
}
if (node.level == 1) {
let data = {
regionId: node.data.value,
isSubordinateUnits: node.data.isSubordinateUnits
}
findRegionOrgByPermission(data).then(response => {
const areas = response.data.map((value, i) => ({
value: value.regionId,
isSubordinateUnits: value.isSubordinateUnits,
label: value.name,
leaf: node.level >= 2
}));
resolve(areas);
});
} else if (node.level == 2) {
let data = {
regionId: node.data.value,
isSubordinateUnits: node.data.isSubordinateUnits
}
findRegionOrgByPermission(data).then(response => {
const areas = response.data.map((value, i) => ({
value: value.regionId,
isSubordinateUnits: value.isSubordinateUnits,
label: value.name,
leaf: node.level >= 2
}));
resolve(areas);
});
}
},
500);
},
}
2.拖拽问题(节点内的拖拽)
<template>
<div>
<el-tree :data="data" node-key="id" default-expand-all @node-drag-start="handleDragStart"
@node-drag-enter="handleDragEnter" @node-drag-leave="handleDragLeave" @node-drag-over="handleDragOver"
@node-drag-end="handleDragEnd" @node-drop="handleDrop" draggable :allow-drop="allowDrop" :allow-drag="allowDrag">
</el-tree>
</div>
</template>
<script>
export default {
data() {
return {
data: [{
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(draggingNode, dropNode, ev) {
console.log('tree drag enter: ', dropNode.label);
},
handleDragLeave(draggingNode, dropNode, ev) {
console.log('tree drag leave: ', dropNode.label);
},
handleDragOver(draggingNode, dropNode, ev) {
console.log('tree drag over: ', dropNode.label);
},
handleDragEnd(draggingNode, dropNode, dropType, ev) {
console.log('tree drag end: ', dropNode && dropNode.label, dropType);
},
//此时可以使用真实接口
handleDrop(draggingNode, dropNode, dropType, ev) {
console.log('tree drop: ', dropNode.label, dropType);
},
//拖拽时判定目标节点能否被放置
allowDrop(draggingNode, dropNode, type) {
console.log("起点", draggingNode)
console.log("起点--id", draggingNode.id)
console.log("起点--label", draggingNode.label)
console.log("起点--父id", draggingNode.parent.id)
console.log("终点", dropNode)
console.log("终点--id", dropNode.id)
console.log("终点---label", dropNode.label)
console.log("终点--父id", dropNode.parent.id)
console.log("3333", type)
//层级判断
if (draggingNode.level === dropNode.level) {
//父级统一性判断
if (draggingNode.parent.id === dropNode.parent.id) {
return type !== 'inner'
}
} else {
return false;
}
},
//判断节点能否被拖拽
allowDrag(draggingNode) {
return true
}
}
};
</script>