解析jquery easyui tree异步加载子节点问题
easyui中的树可以从标记中建立,也可以通过指定一个URL属性读取数据建立。如果想建立一棵异步树,需要为每个节点指定一个id属性值,这样在加载数据时会自动向后台传递id参数。
编写前台代码:
$('#tt').tree({
url:'/demo2/node/getNodes'//TheurlwillbemappedtoNodeControllerclassandgetNodesmethod
});
为测试用,建立一个节点的数据模型:
@Table(name="nodes")
publicclassNodeextendsActiveRecordBase{
@IdpublicIntegerid;
@ColumnpublicIntegerparentId;
@ColumnpublicStringname;
publicbooleanhasChildren()throwsException{
longcount=count(Node.class,"parentId=?",newObject[]{id});
returncount>0;
}
}
编写后台的控制器代码:
publicclassNodeControllerextendsApplicationController{
/**
*getnodes,ifthe'id'parameterequals0thenloadthefirstlevelnodes,
*otherwiseloadthechildrennodes
*@paramidtheparentnodeidvalue
*@returnthetreerequirednodejsonformat
*@throwsException
*/
publicViewgetNodes(intid)throwsException{
Listnodes=null;
if(id==0){//returnthefirstlevelnodes
nodes=Node.findAll(Node.class,"parentId=0orparentIdisnull",null);
}else{//returnthechildrennodes
nodes=Node.findAll(Node.class,"parentId=?",newObject[]{id});
}
List
官网例子地址:http://www.jeasyui.com/tutorial/tree/tree2.php
demo下载:easyui-tree2_jb51.rar
重要的事情说三遍!!!
$('#tt').tree({
method:"POST",
url:'/demo2/node/getNodes'//TheurlwillbemappedtoNodeControllerclassandgetNodesmethod
});
method一定要用POST,GET的话要在URL后面用一个变量来做时间戳处理。
method一定要用POST,GET的话要在URL后面用一个变量来做时间戳处理。
method一定要用POST,GET的话要在URL后面用一个变量来做时间戳处理。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。