上面呢,是jsp頁面的代碼,因為是純插件測試,可以直接複製過去使用的, dao層就不用寫了吧,只是一個查詢,下麵我把ztree的表欄位附上 希望可以幫到大家,這就是我再學習ztree插件時的測試代碼,ssh框架註解版,在其他框架里還沒有試過,但是瞭解了使用方法,應該都沒問題,我相信你們都比我強的多。 ...
<!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的表欄位附上
希望可以幫到大家,這就是我再學習ztree插件時的測試代碼,ssh框架註解版,在其他框架里還沒有試過,但是瞭解了使用方法,應該都沒問題,我相信你們都比我強的多。不是大神,只是分享一下。大神勿噴~~~~~~~~~~