public class ZtreeUtil {
/**
* 数据
* @param list
* @return
*/
public static List<Map<String, Object>> econdingMap(List<ArticleType> list) {
List<Map<String, Object>> mapList = new ArrayList<Map<String,Object>>();
for (ArticleType shopCategory : list) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", shopCategory.getChildCode()+"");//子节点
map.put("pId", shopCategory.getParentCode()+"");//父节点
map.put("level", shopCategory.getTypeLevel()+"");//层级
map.put("name", shopCategory.getTypeName());//名称
mapList.add(map);
}
return getStandardJSON(mapList);
}
public static List<Map<String, Object>> getStandardJSON(List<Map<String, Object>> list) {
// 根据不同框架获取对应的List数据
List<Map<String, Object>> parentList = new ArrayList<Map<String,Object>>();
for (Map<String, Object> map : list) {
if (map.get("pId").equals("0")) {
parentList.add(map);
}
}
recursionChildren(parentList, list);
return parentList;
}
// 递归获取子节点数据
public static void recursionChildren(List<Map<String, Object>> parentList, List<Map<String, Object>> allList) {
for (Map<String, Object> parentMap : parentList) {
List<Map<String, Object>> childrenList = new ArrayList<Map<String,Object>>();
for (Map<String, Object> allMap : allList) {
if (allMap.get("pId").equals(parentMap.get("id"))) {
childrenList.add(allMap);
}
}
if (!StringUtils.isEmpty(childrenList)) {
parentMap.put("children", childrenList);
recursionChildren(childrenList, allList);
}
}
}
}