java如何根据list以及根节点id获取树形展示数据?

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/suyu_yuan/article/details/52872898

话不多说,键代码:


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.cdkj.sys.domain.vo.DeptTreeVo;


public class DeptTreeConUtil {


/**

* @Title: queryItemListByTmpl
* @Description: 根据list以及根节点id获取树形展示数据
* @param itemList
* @param pkId
* @return List<Map<String,Object>> 返回类型
* @throws
*/
public static List<Map<String, Object>> queryItemListByTmpl(
List<DeptTreeVo> itemList, Integer pkId) {
try {
Map<String, Object> map = new HashMap<String, Object>();
List<Map<String, Object>> rusult = new ArrayList<Map<String, Object>>();
for (DeptTreeVo item : itemList) {
if (item.getPkId().equals(pkId)) {
// 设置根节点
map = new HashMap<String, Object>();
// 设置根节点属性
map.put("id", item.getPkId());
map.put("superPk", item.getSuperPk());
map.put("text", item.getText());
map.put("expanded", "true");
// map.put("item", item);
// 设置子节点
getSonTree(map, itemList);
rusult.add(map);
break;
}
}
return rusult;
} catch (Exception e) {
e.printStackTrace();
// log.error(this.getClass().getName(), e);
}
return null;
}


/**

* @Title: getSonTree
* @Description: 递归获取所有的根节点
* @param map
* @param itemList
* @return Map<String,Object> 返回类型
* @throws
*/
private static Map<String, Object> getSonTree(Map<String, Object> map,
List<DeptTreeVo> itemList) {
List<Map<String, Object>> sonList = new ArrayList<Map<String, Object>>();
Map<String, Object> sonMap;
for (DeptTreeVo item : itemList) {
if (map.get("id").toString().equals(item.getSuperPk().toString())) {
sonMap = new HashMap<String, Object>();
// sonMap.put("id", item.getPkId());
// sonMap.put("text", item.getText());
// sonMap.put("state", "closed");

sonMap.put("id", item.getPkId());
sonMap.put("superPk", item.getSuperPk());
sonMap.put("text", item.getText());
sonMap.put("expanded", "true");


// sonMap.put("item", item);
sonList.add(sonMap);
getSonTree(sonMap, itemList);
}
}
map.put("children", sonList);
return map;
}
}


实例类:

import java.io.Serializable;
import java.util.List;


public class DeptTreeVo implements Serializable {
/**
* @Fields serialVersionUID : TODO
*/
private static final long serialVersionUID = 5205187054922851719L;
private Integer pkId;
private String jgbm;
private String text;
private String layer;
private String layerStr;
private Integer superPk;
private List<DeptTreeVo> children;


public Integer getPkId() {
return pkId;
}


public void setPkId(Integer pkId) {
this.pkId = pkId;
}


public String getJgbm() {
return jgbm;
}


public void setJgbm(String jgbm) {
this.jgbm = jgbm;
}

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


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

}

展开阅读全文

没有更多推荐了,返回首页