前臺jsp:
<div class="mini-fit">
<ul id="resourceTree" class="mini-tree" style="width:100%;height:95%;" onbeforeload="onBeforeTreeLoad"
showTreeIcon="true" textField="text" idField="id" resultAsTree="false" expandOnLoad="true" onnodeclick="onclicknode" url="/dabot/dsm/dataSourceManager/sourceList">
</ul>
</div>
其中,動態加載主要依靠的是onBeforeTreeLoad方法
js中方法爲
function onBeforeTreeLoad(e){//動態加載樹
var tree = e.sender; //樹控件
var node = e.node; //當前節點
var params = e.params; //參數對象
var levelnode =mini.get("#resourceTree").getAncestors(e.node);//獲取該節點所有父節點
var nodepath="";
if(levelnode.length==0){nodepath=e.node.text}
else{//對路徑進行拼接 ,用來爲後臺判斷節點位置
for(i=0;i<levelnode.length;i++){
nodepath=nodepath+levelnode[i].text+"/";
}
nodepath=nodepath+e.node.text;
}
console.log(nodepath);
//可以傳遞自定義的屬性
var id=null;
if(levelnode.length>0){id=levelnode[0].id;}
else{id="0";}
params.id = id; //後臺獲取數據源id
params.nodepath=nodepath;//後臺獲取nodepath
}
後臺java代碼
public String sourceList() {
HttpServletRequest request = ActionContext.getActionContext().getHttpServletRequest();
String nodepath=request.getParameter("nodepath");//
String id=request.getParameter("id");//數據源id
System.out.println(nodepath);
if(nodepath==null){
ProcessResult<JSON> sourceList=this.getSourceListTree();
return sourceList.getData().toString();
}else if (nodepath.contains("query")) {
ProcessResult<JSON> sourceList=this.getQueryList(id);
return sourceList.getData().toString();
}
else {
ProcessResult<JSON> sourceList=treeNodeLoader.tree(null, nodepath);
return sourceList.getData().toString();
}
}
其中 pathnode爲null時,調用的方法如下
public ProcessResult<JSON> getSourceListTree() {
String sql="select * from dsm02";
List<Map<String, Object>> list=this.jdbcTemplate.queryForList(sql);
JSONArray jsons = new JSONArray();
for(int i=0;i<list.size();i++) {
JSONObject json = new JSONObject();
json.element("type", "sql");//數據庫類型
json.element("id", list.get(i).get("id"));
json.element("text", list.get(i).get("linkname"));
//json.element("iconCls", "/dabot/dsm/images/database.png");
json.element("isLeaf", false);
json.element("expanded", false);//LK添加
jsons.add(json);
}
ProcessResult<JSON> pr = new ProcessResult<JSON>(true);
pr.setData(jsons);
return pr;
}
注意,寫返回json值的時候,這裏isLeaf和expanded一定要寫false,否則是子節點,無法展開。
最終實現效果圖