EasyUI的Tree部件的数据格式为:
[{
"id":1,
"text":"Folder1",
"iconCls":"icon-save",
"children":[{
"text":"File1",
"checked":true
},{
"text":"Books",
"state":"open",
"attributes":{
"url":"/demo/book/abc",
"price":100
},
"children":[{
"text":"PhotoShop",
"checked":true
},{
"id": 8,
"text":"Sub Bookds",
"state":"closed"
}]
}]
},{
"text":"Languages",
"state":"closed",
"children":[{
"text":"Java"
},{
"text":"C#"
}]
}]
为生成Tree准备的JavaBean为:
public class Organization implements Serializable {
private static final long serialVersionUID = 1L;
private String orgid;//机构ID
private String orgname;//机构名
private String parentorgid;//上级机构ID
public Organization() {
super();
}
public String getOrgid() {
return orgid;
}
public String getOrgname() {
return orgname;
}
public String getParentorgid() {
return parentorgid;
}
public void setOrgid(String orgid) {
this.orgid = orgid;
}
public void setOrgname(String orgname) {
this.orgname = orgname;
}
public void setParentorgid(String parentorgid) {
this.parentorgid = parentorgid;
}
}
public class OrgTree {
private String id;
private String text;
private List<OrgTree> children;
public String getId() {
return id;
}
public String getText() {
return text;
}
public List<OrgTree> getChildren() {
return children;
}
public void setId(String id) {
this.id = id;
}
public void setText(String text) {
this.text = text;
}
public void setChildren(List<OrgTree> children) {
this.children = children;
}
public OrgTree() {
super();
}
/**
* @param id
* @param text
* @param children
*/
public OrgTree(String id, String text,
List<OrgTree> children) {
super();
this.id = id;
this.text = text;
this.children = children;
}
}
生成Tree的代码为:
public OrgTree buildTree(List<Object> list){
Map<String, List<OrgTree>> map = createMap(list);
String rootid = CommonUtils.ROOTORGID;
OrgTree root = map.get(rootid).get(0);
OrgTree ot = new OrgTree();
ot.setId(root.getId());
ot.setText(root.getText());
ot.setChildren(getTrees(root, map));
return ot;
}
/**
* 根据HashMap递归生成树
* @param orgRoot
* @param map
* @return
*/
public List<OrgTree> getTrees(OrgTree orgRoot, Map<String,List<OrgTree>> map){
List<OrgTree> orgTreeList = new ArrayList<OrgTree>();
if(map.containsKey(orgRoot.getId())){
for(OrgTree org : map.get(orgRoot.getId())){
OrgTree ot = new OrgTree();
ot.setId(org.getId());
ot.setText(org.getText());
ot.setChildren(getTrees(org, map));
orgTreeList.add(ot);
}
}
return orgTreeList;
}
/**
* 生成HashMap,Key为parentorgid,Value为List<OrgTree>
* @param list
* @return
*/
private Map<String, List<OrgTree>> createMap(List<Object> list){
Map<String, List<OrgTree>> map = new HashMap<String, List<OrgTree>>();
for(Object o:list){
String parentorgid = ((Organization) o).getParentorgid();
if(map.containsKey(parentorgid)){
List<OrgTree> lot = map.get(parentorgid);
lot.add(create(o));
map.put(parentorgid,lot);
}else{
List<OrgTree> lot = new ArrayList<OrgTree>();
lot.add(create(o));
map.put(parentorgid, lot);
}
}
return map;
}
/**
* Organization转OrgTree
* @param o
* @return
*/
private OrgTree create(Object o){
String orgid = ((Organization) o).getOrgid();
String orgname = ((Organization) o).getOrgname();
OrgTree ot = new OrgTree(orgid, orgname, new ArrayList<OrgTree>());
return ot;
}
}