bootstrap-treeview 后台拼装JSON
来源:互联网 发布:ant执行linux命令 编辑:程序博客网 时间:2024/05/16 06:26
- /* 初始化数据
- * @param specTableList
- * @return
- */
- @SuppressWarnings({ "unchecked", "rawtypes" })
- public List initList(List specTableList){
- List dataList = new ArrayList();
- HashMap dataRecord = new HashMap();
- for(int i = 0; i < specTableList.size(); i++) {
- VO VO = (VO)specTableList.get(i);
- dataRecord = new HashMap();
- dataRecord.put("id",VO.getSpecId());
- dataRecord.put("text", VO.getSpecName());
- String parentID = VO.getParentId();
- if (StringUtils.isEmpty(parentID)) {
- parentID = "0";
- }
- dataRecord.put("parentId",parentID);
- dataList.add(dataRecord);
- }
- return dataList;
- }
-
-
- class Node {
- public String id;
- public String text;
- public String parentId;
-
-
-
-
- private Children children = new Children();
-
-
- public String toString() {
- String result =
- "{"
- + "\"text\":\"" + text + "\","
- + "\"href\":\"" + id +"\"";
- if (children != null && children.getSize() != 0) {
- if (result.contains("nodes")) {
- result += ",";
- }else{
- result += ",\"nodes\":" + children.toString();
- }
- }
- return result + "}";
- }
-
-
- public void sortChildren() {
- if (children != null && children.getSize() != 0) {
- children.sortChildren();
- }
- }
-
-
- public void addChild(Node node) {
- this.children.addChild(node);
- }
- }
-
- class Children {
- private List list = new ArrayList();
-
- public int getSize(){
- return list.size();
- }
- public void addChild(Node node){
- list.add(node);
- }
-
-
- public String toString() {
- String result = "[";
- for (Iterator it = list.iterator(); it.hasNext();) {
- result += ((Node) it.next()).toString();
- result += ",";
- }
- result = result.substring(0, result.length() - 1);
- result += "]";
- return result;
- }
-
-
- public void sortChildren() {
-
-
- Collections.sort(list, new NodeIDComparator());
-
- for (Iterator it = list.iterator(); it.hasNext();) {
- ((Node) it.next()).sortChildren();
- }
- }
-
-
-
-
- class NodeIDComparator implements Comparator {
-
- public int compare(Object o1, Object o2) {
- int j1 = Integer.parseInt(((Node)o1).id);
- int j2 = Integer.parseInt(((Node)o2).id);
- return (j1 < j2 ? -1 : (j1 == j2 ? 0 : 1));
- }
- }
- }
-
- public String init() throws Exception{
- List specTableList = null;
- List list = cacheMap.get("cacheList");
- if (list!=null && list.size()>0) {
- specTableList = cacheMap.get("cacheList");
- }else{
- specTableList = specTableBo.queryTableList();
- cacheMap.put("cacheList", specTableList);
- }
- List dataList = initList(specTableList);
-
- HashMap nodeList = new HashMap();
-
- Node root = new Node();
- root.id = "0";
- root.text= "根节点";
- root.parentId = "";
- nodeList.put(root.id, root);
-
- for (Iterator it = dataList.iterator(); it.hasNext();) {
- Map dataRecord = (Map) it.next();
- Node node = new Node();
- node.id = (String) dataRecord.get("id");
- node.text = (String) dataRecord.get("text");
- node.parentId = (String) dataRecord.get("parentId");
- nodeList.put(node.id, node);
- }
-
- Set entrySet = nodeList.entrySet();
- for (Iterator it = entrySet.iterator(); it.hasNext();) {
- Node node = (Node) ((Map.Entry) it.next()).getValue();
- if (node.parentId == null || node.parentId.equals("")) {
- root = node;
- } else {
- ((Node) nodeList.get(node.parentId)).addChild(node);
- }
- }
-
- System.out.println(root.toString());
- return "["+root.toString()+"]";
- }
0 0