package com.treeTable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.jgoodies.binding.beans.Model;
import com.utils.DataUtil;
public class TreeTableUtil {
/**
*
* @param fileds 表字段集合
* @param code 根节点的pcode,一般为ROOT
* @param nodes 所有数据的集合
* @return
*/
public static List<Object> getTreeTable(String[] fileds, String code, List<Model> nodes) {
List<Model> childList = getChildNodes(code, nodes);
List<Object> childTree = new ArrayList<Object>();
for (Object object : childList) {
Model map = (Model) object;
HashMap<String, Object> o = new HashMap<String, Object>();
for (int i = 0; i < fileds.length; i++) {
o.put(fileds[i], DataUtil.objToStr(map.getStrValue("" + fileds[i] + "")));
}
List<Object> childs = getTreeTable(fileds, map.getStrValue("code"), nodes); // 递归调用该方法
if (!childs.isEmpty()) {
o.put("children", childs);
}
childTree.add(o);
}
return childTree;
}
private static List<Model> getChildNodes(String code, List<Model> nodes) {
List<Model> childList = new ArrayList<Model>();
for (Object obj : nodes) {
Model map = (Model) obj;
if (map.getStrValue("pcode").equals(code)) {
childList.add(map);
}
}
return childList;
}
}
调用方法:
String[] fileds = new String[] {"aa","bb","cc","dd"};
List list = TreeTableUtil.getTreeTable(fileds,"ROOT",allDataList);