easyui中的树可以从标记中建立,也可以通过指定一个URL属性读取数据建立。如果想建立一棵异步树,需要为每个节点指定一个id属性值,这样在加载数据时会自动向后台传递id参数。
编写前台代码:
$('#tt').tree({
url:'/demo2/node/getNodes' // The url will be mapped to NodeController class and getNodes method
});
为测试用,建立一个节点的数据模型:
@Table(name="nodes")
public class Node extends ActiveRecordBase{
@Id public Integer id;
@Column public Integer parentId;
@Column public String name;
public boolean hasChildren() throws Exception{
long count = count(Node.class, "parentId=?", new Object[]{id});
return count > 0;
}
}
编写后台的控制器代码:
public class NodeController extends ApplicationController{
/**
* get nodes, if the 'id' parameter equals 0 then load