el-cascader 根据 已知数据 子节点的id 获取对应的所有父节点id

el-cascader 根据 已知数据 子节点的id 获取对应的所有父节点id,从左至右是一级id,二级id,三级id

方法一

  let opDatas = this.getData(this.options);  // 为处理后的树状数据


        //TODO: 要测试一下 树 ->[每一项的第一个id]和最后一个name
        let status = false;
        const func = (arr) => {
          arr.forEach((item, index) => {
            if (item.children && item.children.length > 0) {
              if (index == 0) {
                idsArr.push(item.id);
              }
              func(item.children);
            } else {
              if (index == 0 && !status) {
                idsArr.push(item.id);
                status = true;
              }
              if (name == "") {
                name = item.name;
              }
            }
          });
        };

        func(opDatas);

        this.parentIds = idsArr; // 输出为[祖先级id,父级id,id]
        this.tableTitle = name; // 对应的name

方法二


const response = {
  code: 200,
  msg: null,
  data: [
    {
      id: "0",
      parentId: "-1",
      name: "市区",
      fullPath: "-1,0,",
      scope: null,
      scopeAll: null,
      nightMin: null,
      treeLevel: 1,
      formula: null,
      trade: null,
      flowNum: 1,
      children: [
        {
          id: "10",
          parentId: "0",
          name: "周庄",
          fullPath: "0,10",
          scope: null,
          scopeAll: null,
          nightMin: null,
          treeLevel: 2,
          formula: null,
          trade: null,
          flowNum: null,
        },
        {
          id: "1",
          parentId: "0",
          name: "开发区",
          fullPath: "0,1,",
          scope: null,
          scopeAll: null,
          nightMin: null,
          treeLevel: 2,
          formula: null,
          trade: null,
          flowNum: null,
          children: [
            {
              id: "1701053903855128577",
              parentId: "1",
              name: "新增测试",
              fullPath: "0,1,1701053903855128577,",
              scope:
                "120.962906,31.41166 120.962391,31.411367 120.965138,31.372682 121.020466,31.40438 121.001068,31.417419 120.979267,31.421081 ",
              scopeAll:
                "[[120.962906,31.41166],[120.962391,31.411367],[120.965138,31.372682],[121.020466,31.40438],[121.001068,31.417419],[120.979267,31.421081]]",
              nightMin: "12.36",
              treeLevel: 3,
              formula: "",
              trade: "",
              flowNum: 3,
            },
          ],
        },
        {
          id: "2",
          parentId: "0",
          name: "花桥",
          fullPath: "0,2,",
          scope: null,
          scopeAll: null,
          nightMin: null,
          treeLevel: 2,
          formula: null,
          trade: null,
          flowNum: null,
        },
        {
          id: "3",
          parentId: "0",
          name: "巴城",
          fullPath: "0,3,",
          scope: null,
          scopeAll: null,
          nightMin: null,
          treeLevel: 2,
          formula: null,
          trade: null,
          flowNum: null,
        },
        {
          id: "4",
          parentId: "0",
          name: "周市",
          fullPath: "0,4,",
          scope: null,
          scopeAll: null,
          nightMin: null,
          treeLevel: 2,
          formula: null,
          trade: null,
          flowNum: null,
        },
        {
          id: "5",
          parentId: "0",
          name: "张浦",
          fullPath: "0,5,",
          scope: null,
          scopeAll: null,
          nightMin: null,
          treeLevel: 2,
          formula: null,
          trade: null,
          flowNum: null,
        },
        {
          id: "6",
          parentId: "0",
          name: "千灯",
          fullPath: "0,6,",
          scope: null,
          scopeAll: null,
          nightMin: null,
          treeLevel: 2,
          formula: null,
          trade: null,
          flowNum: null,
        },
        {
          id: "7",
          parentId: "0",
          name: "陆家",
          fullPath: "0,7,",
          scope: null,
          scopeAll: null,
          nightMin: null,
          treeLevel: 2,
          formula: null,
          trade: null,
          flowNum: null,
        },
        {
          id: "8",
          parentId: "0",
          name: "锦溪",
          fullPath: "0,8,",
          scope: null,
          scopeAll: null,
          nightMin: null,
          treeLevel: 2,
          formula: null,
          trade: null,
          flowNum: null,
        },
        {
          id: "9",
          parentId: "0",
          name: "淀山湖",
          fullPath: "0,9,",
          scope: null,
          scopeAll: null,
          nightMin: null,
          treeLevel: 2,
          formula: null,
          trade: null,
          flowNum: null,
        },
      ],
    },
  ],
};

export function helperCreateTreeFunc(handle) {
  return function (obj, iterate, options?, context?) {
    let opts = options || {};
    let optChildren = opts.children || "children";

    return handle(null, obj, iterate, context, [], [], optChildren, opts, null);
  };
}
function findTreeItem(
  parent,
  obj,
  iterate,
  context,
  path,
  node,
  parseChildren,
  opts
) {
  if (obj) {
    let item, index, len, paths, nodes, match;

    for (index = 0, len = obj.length; index < len; index++) {
      item = obj[index];
      paths = path.concat(["" + index]);
      nodes = node.concat([item]);
      if (iterate.call(context, item, index, obj, paths, parent, nodes)) {
        return {
          index: index,
          item: item,
          path: paths,
          items: obj,
          parent: parent,
          nodes: nodes,
        };
      }
      if (parseChildren && item) {
        match = findTreeItem(
          item,
          item[parseChildren],
          iterate,
          context,
          paths.concat([parseChildren]),
          nodes,
          parseChildren,
          opts
        );
        if (match) {
          return match;
        }
      }
    }
  }
}

export const findTree = helperCreateTreeFunc(findTreeItem);

const data = response.data;

const getNodePathIds = (id, idList = []) => {
  const resultItem = findTree(data, (item) => item.id === id);
  if (!resultItem) return idList;
  const target = resultItem.item;
  idList.unshift(target.id);
  if (target.parentId) {
    return getNodePathIds(target.parentId, idList);
  }
  return idList;
};

const ids = getNodePathIds("1701053903855128577");
console.log(ids, "--ids");

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值