easyui中的树可以从标记中建立,也可以通过指定一个URL属性读取数据建立。如果想建立一棵异步树,需要为每个节点指定一个id属性值,这样在加载数据时会自动向后台传递id参数。
rush:js;">
rush:js;">
$('#tt').tree({
url:'/demo2/node/getNodes' // The url will be mapped to NodeController class and getNodes method
});
为测试用,建立一个节点的数据模型:
public boolean hasChildren() throws Exception{
long count = count(Node.class,"parentId=?",new Object[]{id});
return count > 0;
}
}
long count = count(Node.class,"parentId=?",new Object[]{id});
return count > 0;
}
}
rush:js;">
public class NodeController extends ApplicationController{
/**
* get nodes,if the 'id' parameter equals 0 then load the first level nodes,* otherwise load the children nodes
* @param id the parent node id value
* @return the tree required node json format
* @throws Exception
*/
public View getNodes(int id) throws Exception{
List nodes = null;
if (id == 0){ // return the f<a href="https://www.jb51.cc/tag/irs/" target="_blank" class="keywords">irs</a>t level nodes
nodes = Node.findAll(Node.class,"parentId=0 or parentId is null",null);
} else { // return the children nodes
nodes = Node.findAll(Node.class,new Object[]{id});
}
List<Map<String,Object>> items = new ArrayList<Map<String,Object>>();
for(Node node: nodes){
Map<String,Object> item = new HashMap<String,Object>();
item.put("id",node.id);
item.put("text",node.name);
// the node has children,// set the state to 'closed' so the node can asynchronous load children nodes
if (node.hasChildren()){
item.put("state","closed");
}
items.add(item);
}
return new JsonView(items);
}
}
官网例子地址:http://www.jeasyui.com/tutorial/tree/tree2.php
重要的事情说三遍!!!
rush:js;">
$('#tt').tree({
method:"POST",url:'/demo2/node/getNodes' // The url will be mapped to NodeController class and getNodes method
});
method一定要用POST,GET的话要在URL后面用一个变量来做时间戳处理。
method一定要用POST,GET的话要在URL后面用一个变量来做时间戳处理。
method一定要用POST,GET的话要在URL后面用一个变量来做时间戳处理。