1 其他相关文章
2 有父子id关联关系的树形结构数据,根据最后一级的id查完整结构
<template>
<div class="">
<el-cascader v-model="value" :options="options" :props="customProps" @change="handleChange"></el-cascader>
</div>
</template>
<script>
export default {
name: "cascader",
data () {
return {
value: "yizhi",
options: [
{
value: "zhinan",
label: "指南",
parentId: "0",
children: [
{
value: "shejiyuanze",
label: "设计原则",
parentId: "zhinan",
children: [
{
value: "yizhi",
label: "一致",
parentId: "shejiyuanze",
},
{
value: "fankui",
label: "反馈",
parentId: "shejiyuanze",
},
{
value: "xiaolv",
label: "效率",
parentId: "shejiyuanze",
},
{
value: "kekong",
label: "可控",
parentId: "shejiyuanze",
},
],
},
{
value: "daohang",
label: "导航",
parentId: "zhinan",
children: [
{
value: "cexiangdaohang",
label: "侧向导航",
parentId: "daohang",
},
{
value: "dingbudaohang",
label: "顶部导航",
parentId: "daohang",
},
],
},
],
},
],
customProps: {
label: "label",
value: "value",
children: "children",
},
};
},
mounted () {
let arr = []
const list = this.queryLevelAll(this.options, 'yizhi', arr)
console.log(list);
},
methods: {
queryLevelAll (list, valueId, arr) {
list.forEach(x => {
if (x.parentId && x.value == valueId) {
return arr.unshift(x.value)
}
if (x.children && x.children.length) {
const array = x.children.find(v => v.value == valueId)
if (array) {
this.queryLevelAll(this.options, x.value, arr)
}
this.queryLevelAll(x.children, valueId, arr)
}
})
return arr
}
},
};
</script>
<style scoped lang="less">
</style>
3 无父子id关联关系的树形结构数据,根据最后一级的id查完整结构
<template>
<div class="">
<el-cascader v-model="value" :options="options" :props="customProps" @change="handleChange"></el-cascader>
</div>
</template>
<script>
export default {
name: "cascader",
data () {
return {
value: "yizhi",
options: [
{
value: "zhinan",
label: "指南",
parentId: "0",
children: [
{
value: "shejiyuanze",
label: "设计原则",
children: [
{
value: "yizhi",
label: "一致",
},
{
value: "fankui",
label: "反馈",
},
{
value: "xiaolv",
label: "效率",
},
{
value: "kekong",
label: "可控",
},
],
},
{
value: "daohang",
label: "导航",
children: [
{
value: "cexiangdaohang",
label: "侧向导航",
},
{
value: "dingbudaohang",
label: "顶部导航",
},
],
},
],
},
],
customProps: {
label: "label",
value: "value",
children: "children",
},
};
},
mounted () {
let arr = []
const list = this.queryLevelAll(this.options, 'yizhi', arr)
console.log(list);
},
methods: {
queryLevelAll (list, valueId, arr) {
list.forEach(x => {
if (x.parentId && x.value == valueId) {
return arr.unshift(x.value)
}
if (x.children && x.children.length) {
const array = x.children.find(v => v.value == valueId)
if (array) {
arr.unshift(array.value)
this.queryLevelAll(this.options, x.value, arr)
}
this.queryLevelAll(x.children, valueId, arr)
}
})
return arr
}
},
};
</script>
<style scoped lang="less">
</style>
4 方法集合
- 有父子id关联关系的树形结构数据请使用
options
, 无父子id关联关系的树形结构数据请将optionsCopy
数据赋值到 options
中进行测试
<template>
<div class="">
<el-cascader v-model="value" :options="options" :props="customProps" @change="handleChange"></el-cascader>
</div>
</template>
<script>
export default {
name: "cascader",
data () {
return {
value: "yizhi",
options: [
{
value: "zhinan",
label: "指南",
parentId: "0",
children: [
{
value: "shejiyuanze",
label: "设计原则",
parentId: "zhinan",
children: [
{
value: "yizhi",
label: "一致",
parentId: "shejiyuanze",
},
{
value: "fankui",
label: "反馈",
parentId: "shejiyuanze",
},
{
value: "xiaolv",
label: "效率",
parentId: "shejiyuanze",
},
{
value: "kekong",
label: "可控",
parentId: "shejiyuanze",
},
],
},
{
value: "daohang",
label: "导航",
parentId: "zhinan",
children: [
{
value: "cexiangdaohang",
label: "侧向导航",
parentId: "daohang",
},
{
value: "dingbudaohang",
label: "顶部导航",
parentId: "daohang",
},
],
},
],
},
],
optionsCopy: [
{
value: "zhinan",
label: "指南",
parentId: "0",
children: [
{
value: "shejiyuanze",
label: "设计原则",
children: [
{
value: "yizhi",
label: "一致",
},
{
value: "fankui",
label: "反馈",
},
{
value: "xiaolv",
label: "效率",
},
{
value: "kekong",
label: "可控",
},
],
},
{
value: "daohang",
label: "导航",
children: [
{
value: "cexiangdaohang",
label: "侧向导航",
},
{
value: "dingbudaohang",
label: "顶部导航",
},
],
},
],
},
],
newOptions: [],
treeLevel: [],
customProps: {
label: "label",
value: "value",
children: "children",
},
};
},
mounted () {
this.treeConvert(this.options);
this.options = this.treeChildren(this.treeLevel, "0");
this.queryLevel(this.options, this.options, this.value, []);
let arr = []
const list = this.queryLevelAll(this.options, 'yizhi', arr)
console.log(list);
},
methods: {
treeConvert (list) {
list.forEach((x) => {
this.treeLevel.push({
value: x.value,
label: x.label,
parentId: x.parentId,
});
if (x.children && x.children.length) {
this.treeConvert(x.children);
}
});
},
treeChildren (list, parentId) {
let arr = [];
list.forEach((item) => {
if (item.parentId === parentId) {
const children = this.treeChildren(list, item.value);
if (children && children.length) {
item.children = children;
}
arr.push(item);
}
});
return arr
},
queryLevel (list, oldList, id, tree) {
list.forEach((x) => {
if (x.value === id || x.value === id) {
tree.unshift(x.value);
this.queryLevel(oldList, oldList, x.parentId, tree);
}
if (x.children && x.children.length) {
this.queryLevel(x.children, oldList, id, tree);
}
});
this.value = tree;
},
handleChange (value) {
console.log(value);
},
queryLevelAll (list, valueId, arr) {
list.forEach(x => {
if (x.parentId && x.value == valueId) {
return arr.unshift(x.value)
}
if (x.children && x.children.length) {
const array = x.children.find(v => v.value == valueId)
if (array) {
this.queryLevelAll(this.options, x.value, arr)
}
this.queryLevelAll(x.children, valueId, arr)
}
})
return arr
}
},
};
</script>
<style scoped lang="less">
</style>