当前位置: 首页 > 工具软件 > formSelects > 使用案例 >

组织formSelects树状结构数据

弓方伟
2023-12-01
  1. 下拉框选项为层级为2的树
List<JSONObject> resObject = new LinkedList<>();
for(Dict dict : dictList){
    JSONObject parentObject = new JSONObject();
    parentObject.put("name", dict.getName());
    parentObject.put("value", dict.getValue());
    List<JSONObject> childListObject = new LinkedList<>();
    for(Catalog catalog : catalogList){
        JSONObject childObject = new JSONObject();
        if(catalog.getCatalogType().equals(dict.getValue())){
            childObject.put("name", catalog.getName());
            childObject.put("value", catalog.getCatalogValue());
            childObject.put("children", null);
            childListObject.add(childObject);
        }
    }
    parentObject.put("children", childListObject);
    resObject.add(parentObject);
}
  1. 将下拉框选项树的根节点改为分组
List<JSONObject> resObject = new LinkedList<>();
for(Dict dict : dictList){
    JSONObject parentObject = new JSONObject();
    parentObject.put("name", dict.getName());
    parentObject.put("value", dict.getValue());
    parentObject.put("type", "optgroup");
    resObject.add(parentObject);
    for(Catalog catalog : catalogList){
        JSONObject childObject = new JSONObject();
        if(catalog.getCatalogType().equals(dict.getValue())){
            childObject.put("name", catalog.getName());
            childObject.put("value", catalog.getCatalogValue());
            childObject.put("children", null);
            resObject.add(childObject);
        }
    }
}
 类似资料: