default-expand-all 是否默認展開所有節(jié)點
影響 上下 移動
<template>
<div>
<div class="question_info_lists">
<el-tree
ref="tree"
:key="tree_key"
:data="treeData"
node-key="id"
:render-content="renderContent"
:default-expanded-keys="defaultExpand"
></el-tree>
</div>
</div>
</template>
<script>
export default {
data() {
return {
treeData: [
{
id: 1,
label: "一級 1",
children: [
{
id: 4,
label: "二級 1-1",
children: [
{ id: 9, label: "三級 1-1-1", children: [] },
{
id: 10,
label: "三級 1-1-2",
children: []
},
{
id: 11,
label: "三級 1-1-3",
children: []
}
]
},
{
id: 12,
label: "二級 1-2",
children: []
},
{
id: 13,
label: "二級 1-3",
children: []
}
]
},
{
id: 2,
label: "一級 2",
children: [
{ id: 5, label: "二級 2-1", children: [] },
{
id: 6,
label: "二級 2-2",
children: []
}
]
},
{
id: 3,
label: "一級 3",
children: [
{ id: 7, label: "二級 3-1", children: [] },
{
id: 8,
label: "二級 3-2",
children: []
}
]
}
],
tree_key: 0,
defaultExpand: []
};
},
methods: {
// 節(jié)點上移
nodeUp(node, data) {
const parent = node.parent;
const children = parent.data.children || parent.data;
const cIndex = children.findIndex(d => d.id === data.id);
if (parent.level === 0 && cIndex === 0) {
return;
} else if (parent.level !== 0 && cIndex === 0) {
//不同父節(jié)點中移動
alert("不同父節(jié)點中移動");
// const parent2 = parent.parent
// const children2 = parent2.data.children || parent2.data
// const pIndex2 = parseInt(children2.findIndex(p => p.id === parent.data.id), 10) - 1
// if (pIndex2 < 0) return
// children2[pIndex2].children.push(data)
// children.splice(cIndex, 1)
// this.defaultExpand[0] = children2[pIndex2].id
} else if (
(parent.level === 0 && cIndex !== 0) ||
(parent.level !== 0 && cIndex !== 0)
) {
const tempChildrenNodex1 = children[cIndex - 1];
const tempChildrenNodex2 = children[cIndex];
this.$set(children, cIndex - 1, tempChildrenNodex2);
this.$set(children, cIndex, tempChildrenNodex1);
this.defaultExpand[0] = data.id;
}
this.tree_key++;
},
// 節(jié)點下移
nodeDown(store, node, data) {
const parent = node.parent;
const children = parent.data.children || parent.data;
const cIndex = children.findIndex(d => d.id === data.id);
const cLength = children.length - 1; // 最邊上的節(jié)點
const allLevel = store.data.length - 1; // 樹的深度
if (parent.level === allLevel && cIndex === cLength) {
// 最最末的節(jié)點
return;
} else if (parent.level !== allLevel && cIndex === cLength) {
//父節(jié)點不同
alert("不能移動");
// const parent2 = parent.parent
// const children2 = parent2.data.children || parent2.data
// const pIndex2 = parseInt((children2.findIndex(p => p.id === parent.data.id)), 10)
// if (pIndex2 === allLevel) return
// children2[pIndex2 + 1].children.push(data)
// children.splice(cIndex, 1)
// this.defaultExpand[0] = children2[pIndex2 + 1].id
} else if (
(parent.level === allLevel && cIndex !== cLength) ||
(parent.level !== allLevel && cIndex !== cLength)
) {
// 父節(jié)點相同
const tempChildrenNodex1 = children[cIndex + 1];
const tempChildrenNodex2 = children[cIndex];
this.$set(children, cIndex + 1, tempChildrenNodex2);
this.$set(children, cIndex, tempChildrenNodex1);
this.defaultExpand[0] = data.id;
}
this.tree_key++;
},
// 結(jié)構(gòu)樹操作group node,
renderContent(h, { node, data, store }) {
return (
<span>
<span className="node_labe">{data.label}</span>
<div class="tree_node_op" style=" float: right">
<i
class="el-icon-upload2"
on-click={() => this.nodeUp(node, data)}
></i>
<i
class="el-icon-download"
on-click={() => this.nodeDown(store, node, data)}
></i>
</div>
</span>
);
}
}
};
</script>
<style scoped></style>