java如何提供Element中Tree 树形控件数组格式

目标格式

id: "A",
    label: 'Level one 1',
    children: [
      {
        id: 4,
        label: 'Level two 1-1',
        children: [
          {
            id: 9,
            label: 'Level three 1-1-1',
          },
          {
            id: 10,
            label: 'Level three 1-1-2',
          },
        ],
      },
    ],
  },
  {
    id: 2,
    label: 'Level one 2',
    children: [
      {
        id: 5,
        label: 'Level two 2-1',
      },
      {
        id: 6,
        label: 'Level two 2-2',
      },
    ],
  },

第一步数据库查询

image.png

第二步后端查询

image.png

第三步定义TreeNode类型

package com.example.demo.entily;

import java.util.ArrayList;
import java.util.List;

public class TreeNode {
    private String id;
    private String label;
    private List<TreeNode> children;
    public TreeNode() {

    }
    public TreeNode(TreeNode rootNode) {
        this.id = rootNode.getId();
        this.label = rootNode.getLabel();
        if (rootNode.getChildren() != null) {
            this.children = new ArrayList<>();
            for (TreeNode child : rootNode.getChildren()) {
                this.children.add(new TreeNode(child));
            }
        }
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public List<TreeNode> getChildren() {
        return children;
    }

    public void setChildren(List<TreeNode> children) {
        this.children = children;
    }

    @Override
    public String toString() {
        return "{" +
                "id='" + id + ''' +
                ", label='" + label + ''' +
                ", children=" + children +
                '}';
    }
}

注意我的toString方法有一点小改变

第四步编写逻辑方法


@Service
public class EEBasic_Information implements EEBasic_InformationIMP {

    @Autowired
    EE_Basic ee_basic;


    @Override
    public List<TreeNode> API_URL_TYPE_IMP(Map<String, Object> on) {
        List<Map<Object, String>> o = ee_basic.API_URL_TYPE(on.get("Organization_No").toString());
        // 从ee_basic类中根据组织编号获取API的类型并存储在列表o中

        int index = 0; // 标记索引
        String bi = "标记"; // 标记字符串
        List<TreeNode> children = new ArrayList<>(); // 存储子节点列表
        List<TreeNode> collection = new ArrayList<>(); // 存储结果集列表
        TreeNode rootNode = new TreeNode(); // 创建根节点对象
        for (Map<Object, String> map : o) {
            if (!bi.equals(map.get("part_ao"))) {
                // 如果当前遍历的节点的"part_ao"值和之前的不相等,则创建新的节点对象并将其添加到collection列表中
                if (index != 0) {
                    TreeNode newNode = new TreeNode(rootNode); // 使用复制构造函数创建一个新的节点对象并复制属性
                    collection.add(newNode);
                }
                bi = map.get("part_ao"); // 更新bi的值为当前节点的"part_ao"值
                rootNode.setId(map.get("part_ao")); // 设置根节点的ID为当前节点的"part_ao"值
                rootNode.setLabel(map.get("part_nm")); // 设置根节点的标签为当前节点的"part_nm"值
                rootNode.setChildren(null); // 清空根节点的子节点列表
                children.clear(); // 清空子节点列表
                index++; // 索引加1
            }
            TreeNode child = new TreeNode(); // 创建子节点对象
            for (Map.Entry<Object, String> entry : map.entrySet()) {
                Object key = entry.getKey(); // 获取键
                String value = entry.getValue(); // 获取值
                if (key.equals("part_bo")) {
                    child.setId(value); // 设置子节点的ID为当前节点的"part_bo"值
                }
                if (key.equals("part_bm")) {
                    child.setLabel(value); // 设置子节点的标签为当前节点的"part_bm"值
                }
            }
            children.add(child); // 将子节点添加到子节点列表中
            rootNode.setChildren(children); // 设置根节点的子节点列表为children列表
        }
        collection.add(rootNode); // 将根节点添加到结果集列表中
        return collection; // 返回结果集列表
    }
}
原理对应代码
遍历数据库查询出来的每一行for (Map<Object, String> map : o)
因为数据库是排序所以判断是否与下组大类是否相当不同对参数进行保存初始化21-31行
这一行第一次判断肯定为空所以跳过这一次if (index != 0)
获取每次的中类后添加到数组里面child.setId(value); child.setLabel(value);children.add(child);
47行这里是为了添加最后一次的rootNode组47行

第5步调用方法返回参数

@CrossOrigin
@RequestMapping(value = HttpClientTool.API_URL_TYPE,method = RequestMethod.POST,headers = "Accept=application/json")
public Object API_SUPPLIER(@RequestBody Map<String,Object> on){
    return responseJSON(ApiResponseEnum.SUCCESS.getCode(), ApiResponseEnum.SUCCESS.getName(),eeBasic_information.API_URL_TYPE_IMP(on));
}

第6效果图

image.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值