有 || 无父子id关联关系的树形结构数据,根据最后一级的id查完整结构

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)          
        }
        // 如果找到与 valueId  相同的,循环不停止
        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>

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)          
        }
        // 如果找到与 valueId  相同的,循环不停止
        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);
        }
      });
    },
    // 平转树
    // list 每次循环值 parentId第一层的父级ID(一般第一层的父级ID都是统一的)
    treeChildren (list, parentId) {
      let arr = [];
      list.forEach((item) => {
        // 是否找到了节点
        // parentId 默认为null 优先查父节点
        if (item.parentId === parentId) {
          // 找到了节点  递归循环匹配是否存在子级节点
          const children = this.treeChildren(list, item.value);
          if (children && children.length) {
            item.children = children;
          }
          arr.push(item);
        }
      });
      return arr
    },
    // 根据最后一级标识查出完整层级
    // list 每次循环值  oldList 完整的树级结构 id 最后一层的标识ID tree 容器
    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);
    },

    // --------  上方是有父子ID关联的数据 ------------
    
    // 无父子ID关联循环找到完整层级
    queryLevelAll (list, valueId, arr) {
      list.forEach(x => {
        // 如果找到了第一层级,停止循环
        if (x.parentId && x.value == valueId) {
          return arr.unshift(x.value)          
        }
        // 如果找到与 valueId  相同的,循环不停止
        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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值