您的位置:首页 > Web前端 > BootStrap

bootstrap-treeview 后台拼装JSON

2015-11-16 16:53 375 查看
<span style="font-family: Arial, Helvetica, sans-serif;">/**</span>
* 初始化数据
* @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 {
private String id;
private String text;
private String parentId;

/**
* 孩子节点列表
*/
private Children children = new Children();

// 先序遍历,拼接JSON字符串
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);
}

// 拼接孩子节点的JSON字符串
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() {
// 对本层节点进行排序
// 可根据不同的排序属性,传入不同的比较器,这里传入ID比较器
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));
}
}
}
 
<pre name="code" class="java">public String  init(HttpServletRequest request,HttpServletResponse response) 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);
}
}
// 输出无序的树形菜单的JSON字符串
System.out.println(root.toString());
return "["+root.toString()+"]";
}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: