首先建一个实体类,如:
package ***.bean;
import java.io.Serializable;
import java.util.List;
public class Competence implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private int roleID;
private int parentRoleID;//如果是自己生成id,就设成字符串形式,有利于拼凑
private String parentRoleName;
private String roleName;
private String createTime;
private String description;
private String useFlag;//角色是否在使用
private boolean leaf;
private boolean expanded;
private List<Competence> children;
public int getRoleID() {
return roleID;
}
public void setRoleID(int roleID) {
this.roleID = roleID;
}
public int getParentRoleID() {
return parentRoleID;
}
public void setParentRoleID(int parentRoleID) {
this.parentRoleID = parentRoleID;
}
public String getParentRoleName() {
return parentRoleName;
}
public void setParentRoleName(String parentRoleName) {
this.parentRoleName = parentRoleName;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public boolean isLeaf() {
return leaf;
}
public void setLeaf(boolean leaf) {
this.leaf = leaf;
}
public boolean isExpanded() {
return expanded;
}
public void setExpanded(boolean expanded) {
this.expanded = expanded;
}
public List<Competence> getChildren() {
return children;
}
public void setChildren(List<Competence> children) {
this.children = children;
}
}
在service层把dao层查出来的数据拼接
package ****.service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.eaglory.fq.bean.Competence;
import com.eaglory.fq.dao.CompetenceDao;
public class CompetenceService {
private CompetenceDao competenceDao = new CompetenceDao();
public List<Competence> getAllRole() {
List<Competence> roles = competenceDao.getAllRole();
// List<Competence> result1 = new ArrayList<Competence>();
Set<Integer> set = new HashSet<Integer>();
int m = roles.size();
for(int i=0;i<m; i++){
Competence c1 = roles.get(i);
c1.setChildren(new ArrayList<Competence>());
List<Competence> childrens = c1.getChildren();
// if(childrens == null){
// childrens = new ArrayList<Competence>();
// }
for(int j=0;j<m; j++){
Competence c2 = roles.get(j);
if(c1.getRoleID()==c2.getParentRoleID()){
childrens.add(c2);
c1.setLeaf(false);
c1.setExpanded(true);
set.add(j);
}
}
}
int l =set.size();
int[] arr = new int[l];
List<Integer> list = new ArrayList<Integer>();
list.addAll(set);
for(int i=0;i<list.size();i++){
arr[i]=list.get(i);
}
Arrays.sort(arr);
for(int i = l-1;i>=0;i--){
roles.remove(roles.get(arr[i]));
}
return roles;
}
}
在action层传给前端就可以了
List<Competence> competences;
try {
competences = competenceService.getAllRole();
JSONObject jsonObject = new JSONObject();
logger.info("competences:"+competences);
jsonObject.put("children", competences);
retValue = jsonObject.toString();
} catch (Exception e) {
e.printStackTrace();
retValue = "{\"children\":[]}";
}
!注意: 这里的名称要写children因为前端是默认不显示根节点的
前端roleTreeStore 数据源:
var roleTreeStore = Ext.create('Ext.data.TreeStore', {
model: 'Task',
proxy: {
type: 'ajax',
url: getRoleURL,
reader: {
type: 'json',
root : "children"
}
},
fields: ["roleName", "roleID"],
folderSort: true
});
就可以得到想要的树状图统计表: