Java构造前端vue使用的树(或者使用Java构造常用的部门树等)
程序员文章站
2022-07-03 18:25:46
...
Web项目开发中,由于前端框架的限制,很多时候需要将返回的数据加工后再返回给前端。最典型的就是返回一棵部门层级关系的树。下面即是一棵带有层级的文档标题树,和我们书籍的目录结构一样。
在树的结构中,每一个节点都需要有一颗父级节点,即使是*节点,也需要给其父节点赋值。比如,在下面的带有父子关系的线性结构中,有些id的parentId是null的,此时就需要将parentId为null赋值为projectId。同时使用了fastjson作为辅助工具,具有一定的通用性。其数值如下:
"data": [
{
"id": "661661966154199040",
"text": null,
"parentId": null,//父级id为空,需要将其赋值为projectId
"orderCount": 0,
"projectId": "741917334711331456",
"children": null//下级节点
},
{
"id": "661662130646413312",
"text": null,
"parentId": null,
"orderCount": 1,
"projectId": "741917334711331456",
"children": null
},
{
"id": "661662428978868224",
"text": null,
"parentId": null,
"orderCount": 2,
"projectId": "741917334711331456",
"children": null
},
{
"id": "772919473459032064",
"text": null,
"parentId": "653294833572617088",
"orderCount": 2,
"projectId": "741917334711331456",
"children": null
},
{
"id": "772917879430574080",
"text": null,
"parentId": "653294833572617088",
"orderCount": 3,
"projectId": "741917334711331456",
"children": null
},
{
"id": "661661793088827392",
"text": null,
"parentId": null,
"orderCount": 4,
"projectId": "741917334711331456",
"children": null
},
{
"id": "661661918448185344",
"text": null,
"parentId": null,
"orderCount": 5,
"projectId": "741917334711331456",
"children": null
},
{
"id": "772919255229394944",
"text": null,
"parentId": null,
"orderCount": 6,
"projectId": "741917334711331456",
"children": null
},
{
"id": "661661956607963136",
"text": null,
"parentId": null,
"orderCount": 7,
"projectId": "741917334711331456",
"children": null
},
{
"id": "661661957128056832",
"text": null,
"parentId": null,
"orderCount": 8,
"projectId": "741917334711331456",
"children": null
},
{
"id": "653294833572617088",
"text": null,
"parentId": null,
"orderCount": 11,
"projectId": "741917334711331456",
"children": null
},
{
"id": "653370321808759680",
"text": null,
"parentId": null,
"orderCount": 11,
"projectId": "741917334711331456",
"children": null
},
{
"id": "653385876393532288",
"text": null,
"parentId": null,
"orderCount": 11,
"projectId": "741917334711331456",
"children": null
}
]
树的DTO结构如***意children:
public class TreeDTO {
/** 儿子id **/
private Long id;
/** 附带文本 **/
private String text;
/** 父级id **/
private Long parentId;
/** 排序值 **/
private Integer orderCount;
/** 项目id **/
private Long projectId;
/** 孩子节点 **/
private List<TreeDTO> children;
/**
* @return the text
*/
public String getText() {
return text;
}
/**
* @param text
*/
public void setText(String text) {
this.text = text;
}
/**
* @return the id
*/
public Long getId() {
return id;
}
/**
* @param id
*/
public void setId(Long id) {
this.id = id;
}
/**
* @return the parentId
*/
public Long getParentId() {
return parentId;
}
/**
* @param parentId
*/
public void setParentId(Long parentId) {
this.parentId = parentId;
}
/**
* @return the orderCount
*/
public Integer getOrderCount() {
return orderCount;
}
/**
* @param orderCount
*/
public void setOrderCount(Integer orderCount) {
this.orderCount = orderCount;
}
/**
* @return the projectId
*/
public Long getProjectId() {
return projectId;
}
/**
* @param projectId
*/
public void setProjectId(Long projectId) {
this.projectId = projectId;
}
/**
* @return the children
*/
public List<TreeDTO> getChildren() {
return children;
}
/**
* @param children
*/
public void setChildren(List<TreeDTO> children) {
this.children = children;
}
}
- 先将parentId为空的节点设置为projectId。
/**
* 将根目录节点的设置为projectId
*/
private void setRootTreeNode(List<TreeDTO> treeList, Long projectId) {
// 没有parentId,即将projectId作为parentId
for (TreeDTO dto : treeList) {
Long parentId = dto.getParentId();
if (parentId == null) {
dto.setParentId(projectId);
}
}
}
- 返回树型结构
// 构造树结构,treeList为上面的JSON节点的List形式,返回之后即为树型
JSONArray resultTree = listInfoToTree(JSONArray.parseArray(JSON.toJSONString(treeList)), "id", "parentId", "children");
- 将文中开始列出的list转为tree
/**
* 将JSONArray数组转为树状结构
*
* @param arr
* 需要转化的数组
* @param id
* 数据唯一的标识键值名称
* @param pid
* 父id唯一标识键值名称
* @param child
* 子节点名称
*
* @return array
*/
private JSONArray listInfoToTree(JSONArray arr, String id, String pid, String child) {
JSONArray ret = new JSONArray();
// 将数据直接构建成为hash
JSONObject hash = new JSONObject();
// 节点大小
int size = arr.size();
// 将数组转为Object的形式,key为数组中的id,并组装为map
for (int i = 0; i < size; i++) {
JSONObject json = (JSONObject) arr.get(i);
hash.put(json.getString(id), json);
}
// 遍历结果集
for (int j = 0; j < size; j++) {
// 单条记录
JSONObject nodeVal = (JSONObject) arr.get(j);
// 在hash中取出key为单条记录中pid的值
JSONObject hashParent = (JSONObject) hash.get(nodeVal.get(pid).toString());
// 如果记录的pid存在,则说明它有父节点,将她添加到孩子节点的集合中(这里与刚才所说的所有parentId为空的节点填充一个projectid并不冲突,因为当填入projectId的时候,hashParent是get不到值的)
if (hashParent != null) {
// 构造child属性
if (hashParent.get(child) != null) {
JSONArray chArr = (JSONArray) hashParent.get(child);
chArr.add(nodeVal);
hashParent.put(child, chArr);
} else {
JSONArray chArr = new JSONArray();
chArr.add(nodeVal);
hashParent.put(child, chArr);
}
} else {
ret.add(nodeVal);
}
}
return ret;
}
- PostMan返回结果:
"data": [
{
"orderCount": 0,
"id": "661661966154199040",
"text": "迄病蚤莎紊脸液板锌猿灯金",
"projectId": "741917334711331456",
"parentId": "741917334711331456"
},
{
"orderCount": 1,
"id": "661662130646413312",
"text": "虽曙拱抿蛊绩簧蛛伞杀",
"projectId": "741917334711331456",
"parentId": "741917334711331456"
},
{
"orderCount": 2,
"id": "661662428978868224",
"text": "粳野赣下喉迎醇丛攘颓",
"projectId": "741917334711331456",
"parentId": "741917334711331456"
},
{
"orderCount": 4,
"id": "661661793088827392",
"text": "仰儡辖龙蒸锰篡带伞蚂",
"projectId": "741917334711331456",
"parentId": "741917334711331456"
},
{
"orderCount": 5,
"id": "661661918448185344",
"text": "殃书您衡牛阎农鸣",
"projectId": "741917334711331456",
"parentId": "741917334711331456"
},
{
"orderCount": 6,
"id": "772919255229394944",
"text": "154641",
"projectId": "741917334711331456",
"parentId": "741917334711331456"
},
{
"orderCount": 7,
"id": "661661956607963136",
"text": "赊于毋钱永患几伤凛婆辉雪",
"projectId": "741917334711331456",
"parentId": "741917334711331456"
},
{
"orderCount": 8,
"id": "661661957128056832",
"text": "刀脑散郎蒙铀饼每舍誓刺卷",
"projectId": "741917334711331456",
"parentId": "741917334711331456"
},
{
"children": [
{
"orderCount": 2,
"id": "772919473459032064",
"text": "154642",
"projectId": "741917334711331456",
"parentId": "653294833572617088"
},
{
"orderCount": 3,
"id": "772917879430574080",
"text": "15464",
"projectId": "741917334711331456",
"parentId": "653294833572617088"
}
],
"orderCount": 11,
"id": "653294833572617088",
"text": "测试链22",
"projectId": "741917334711331456",
"parentId": "741917334711331456"
},
{
"orderCount": 12,
"id": "653370321808759680",
"text": "sfdas",
"projectId": "741917334711331456",
"parentId": "741917334711331456"
},
{
"orderCount": 13,
"id": "653385876393532288",
"text": "哈哈哈哈哈哈哈哈",
"projectId": "741917334711331456",
"parentId": "741917334711331456"
}
]