zTree树形插件异步加载方法详解
程序员文章站
2022-07-05 20:43:09
本文实例为大家分享了ztree树形插件异步加载,struts2框架,供大家参考,具体内容如下
本文实例为大家分享了ztree树形插件异步加载,struts2框架,供大家参考,具体内容如下
<!doctype html> <html> <head> <meta charset="utf-8"> <title>异步加载</title> <link rel="stylesheet" href="${pagecontext.request.contextpath}/ztree_v3-master/css/demo.css"> <link rel="stylesheet" href="${pagecontext.request.contextpath}/ztree_v3-master/css/ztreestyle/ztreestyle.css"> <script type="text/javascript" src="${pagecontext.request.contextpath}/jquery-2.1.1.min.js"></script> <script type="text/javascript" src="${pagecontext.request.contextpath}/ztree_v3-master/js/jquery.ztree.core.min.js"></script> <script type="text/javascript"> var setting = { async : { enable : true, url : "${pagecontext.request.contextpath}/ztreeaction!getmenusbyid.jhtml", autoparam : [ "id" ], //datafilter : filter }, data : { key : { url : "xurl" }, simpledata : { enable : true, pidkey : "pid" } }, }; $(document).ready(function() { $.fn.ztree.init($("#treedemo"), setting); }); </script> </head> <body> <div class="ztree" style="width: 20%; height: 500px; padding-top: 10px; float: left; border: 1px solid #ff0000;"> <ul id="treedemo"></ul> </div> </body> </html>
上面呢,是jsp页面的代码,因为是纯插件测试,可以直接复制过去使用的,
action层代码,可以直接复制使用
public void getmenusbyid(){ list<ztree> list=null; try { list=ztreeservice.getmenusbyid(ztree); } catch (exception e) { e.printstacktrace(); } super.writejson(list); }
public list<ztree> getmenusbyid(ztree ztree) throws exception; //接口
//service代码,几本都可以复制使用 package com.jk.service.impl; import java.util.arraylist; import java.util.hashmap; import java.util.list; import java.util.map; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.repository; import com.jk.dao.ztreedao; import com.jk.model.ztree; import com.jk.service.ztreeservice; @repository("ztreeservice") public class ztreeserviceimpl implements ztreeservice { @autowired private ztreedao ztreedao; @override public list querylist(ztree ztree) throws exception { string hql=" from ztree where 1=1 "; map<string, object> params = new hashmap<string, object>(); //传的对象是否为空,拼接sql+id if(ztree.getid()!=null){ hql+=" and id = :id "; params.put("id",ztree.getid()); } return ztreedao.querylist(hql, params); } //递归查询 @override public list<ztree> getmenusbyid(ztree ztree) throws exception { string hql=" from ztree where 1=1 "; map<string,object> params=new hashmap<string,object>(); if(ztree.getid()==null||ztree.getid().equals("")){ hql+=" and pid is null "; }else{ hql+= " and pid = :id "; params.put("id",ztree.getid()); } list<ztree> queryztree = ztreedao.querylist(hql, params); list<ztree> list=new arraylist<ztree>(); for(ztree ztree2:queryztree){ if(queryboolean(ztree2)){ ztree2.setisparent("true"); } list.add(ztree2); } return list; } //熟悉递归的朋友应该可以看懂,我就不解释了,我也聊不清楚这个递归,不误导了,代码在这,可以复制进去直接运行, public boolean queryboolean(ztree ztree) throws exception{ boolean flag=false; string hql= " from ztree where pid = :id "; map<string,object> params=new hashmap<string,object>(); params.put("id", ztree.getid()); list<ztree> queryztree = ztreedao.querylist(hql, params); if(queryztree!=null&&queryztree.size()>0){ flag=true; } return flag; } }
dao层就不用写了吧,只是一个查询,下面我把ztree的表字段附上
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。