• 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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    方法二

    
    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");
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
  • 相关阅读:
    UnityDOTS备忘
    Godot4.1 GDExtension 配置VisualStudio方法梳理以及快捷配置工具
    使用keil 5.37版本编译FreeRTOS出错原因及解决办法
    Nginx惊群问题分析及解决
    [附源码]计算机毕业设计JAVAjsp医院挂号系统
    Persistent data structure 不可变数据结构
    QML:拖动曲线坐标点修改曲线
    2023 收入最高的十大编程语言
    算法——回溯法(1)
    让开发回归简单模式-组件封装
  • 原文地址:https://blog.csdn.net/anny_mei/article/details/132803384