前段时间工作中需要使用树,这一点,在前台用ext很好实现,只是这个tree的store的这个json在后台拼写的时候有些费了我的脑筋,下面总结一下,我发现很好用的一个方法:
(哦,亲们,这些代码里,数据是从不同数据源获得的,所以在代码里体现了切换数据源的代码,如果你不需要切换数据源,没有必要这个做哦,只需要看sql的组成和返回list就行了。)
sql:
public List FindComboTreeDataBySubjectId(String subjectId, List<String> paramList,List constantValue) {
//权限表的数据及主题
String pmsSql = reportPermissionService.getPmsSql(constantValue);
Subject pmsSubject = reportPermissionService.getPmsSubject();
Subject subject = this.subjectDomainService.getSubject(subjectId);
if(subject==null||"".equals(subject)){
return new ArrayList();
}
String subTable = subject.getSubTable();
String ds = subject.getDataSource().toString();
// 切换到给定的数据源
HotSwappableTargetSource swapper = (HotSwappableTargetSource)app.getBean("swappableDataSource");
DataSource dataSource = (DataSource)app.getBean(ds);
swapper.swap(dataSource);
// 查询元数据
List result = new ArrayList();
String sql = "SELECT ID AS COMPANYID, ID,PID,NAME,NODETYPE,YEARMONTH FROM "+subTable+" WHERE 1=1 ";
if(pmsSubject !=null && pmsSubject.getDataSource().toString().equals(ds) && pmsSql !=""){
sql = "SELECT T.ID AS COMPANYID,T.ID,T.PID,T.NAME,T.NODETYPE,T.YEARMONTH, DECODE(PMS.ID,NULL,0,1) AS PERMIS FROM "+subTable + " T LEFT JOIN" + pmsSql + " ON T.ID = PMS.ID WHERE 1=1 ";
}
if(paramList != null){
Iterator paramItem = paramList.iterator();
while(paramItem.hasNext()){
String param = paramItem.next().toString();
if(!"".equals(param)||param!="")
sql +=" and "+param+" ";
}
}
sql = "SELECT T.*,LEVEL FROM ("+ sql + ")T start with t.pid is null connect by prior t.id = t.PID order by level,T.NAME desc";
SQLQuery query = getSession().createSQLQuery(sql);
List list = query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
return list;
}
json
public String FindTreeDataBySubjectId(String subjectId,List<String> paramList,List constantValue){
List list=componentDataDomainService.FindComboTreeDataBySubjectId(subjectId, paramList,constantValue);
String treeString=getChildren(list,"");
if("".equals(treeString)||treeString==""){
treeString="";
}else{
treeString=treeString.replace("children:[,", "children:[");
treeString=treeString.substring(1, treeString.length());//去掉第一个逗号
}
treeString="{companyId:'0',id:'0',text:'无',expanded:'true',children:["+treeString+"]}";
return treeString;
}
/**
* 根据树列表,递归返返回树结构
* @param list 数据列表
* @param pId父级节点
* @return :D :D :D :D
* @DATE:2012-8-31 @TIME: 下午14:20:58
*/
@SuppressWarnings("rawtypes")
private String getChildren(List list,String pId){
StringBuffer treeStore = new StringBuffer();
for(int i=0;i<list.size();i++){
Map company = (Map)list.get(i);
String _pId = "";
if(company.get("PID")!=null){
_pId = company.get("PID").toString();
}
if(_pId.equals(pId)){
//1.输出自己
treeStore.append(",{companyId:'").append(company.get("COMPANYID").toString()).append("'")
.append(",id:'").append(company.get("ID").toString()).append("'")
.append(",text:'").append(company.get("NAME").toString()).append("'")
.append(",nodeType:'").append(company.get("NODETYPE").toString()).append("'")
.append(",disabled:").append((company.get("PERMIS").toString().equals("1"))?false:true)
.append(",iconCls:'").append("icon-company").append("'");
if(company.get("LEVEL").toString().equals("1")){
treeStore.append(",expanded:'true'");
}else{
treeStore.append(",expanded:'false'");
}
treeStore.append(",children:[");
//2.调用递归
treeStore.append(getChildren(list,company.get("ID").toString())+"]}");
}
}
return treeStore.append("").toString();
}