JQuery zTree v2.6 基本用法实例 博客分类: JQuery jquery
程序员文章站
2024-03-07 11:59:51
...
JQuery zTree v2.6
基本用法实例
初始化zTree:
获取动态Url的方法:
Action:
下层动态加载目录:
根目录:
获取目录树中有改变的节点:
可参照附件中zTreedemo和zTreeAPI v2.6.chm
基本用法实例
初始化zTree:
var zTree1; var setting; var zNodes =[]; function refreshTree(asyncUrl) { $("#getUrl").html(""); $("#onAsyncSuccessNode").html("<br/>"); $("#onAsyncErrorNode").html("<br/>"); setting = { checkable: true, async: true, //asyncUrl: "/Gw_sptrainTree_search.do?id=50", //获取节点数据的URL地址 asyncUrl: getAsyncUrl, //获取节点数据的URL地址 asyncParam: ["name", "id", "group", "trainunit"], //获取节点数据时,必须的数据名称,例如:id、name asyncParamOther: ["sptrainID","50"], //其它参数 ( key, value 键值对格式) callback:{ beforeAsync: zTreeBeforeAsync, asyncSuccess: zTreeOnAsyncSuccess, asyncError: zTreeOnAsyncError } };
获取动态Url的方法:
function getAsyncUrl(treeNode){ var specialtyNum = document.getElementById("id").value; var positionNum = document.getElementById("positionNum").value; var url = ""; if(treeNode==null){ url = "/Gw_sptrainTree_search.do?id=50&specialtyNum="+specialtyNum+"&positionNum="+positionNum+""; }else{ url = "/Gw_sptrainTree_search.do?id="+treeNode.id+"&specialtyNum="+specialtyNum+"&positionNum="+positionNum+""; } return url; } zTree1 = $("#treeDemo").zTree(setting, zNodes); }
Action:
package com.company.erp.train.gw_sptrain.action; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.TreeMap; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import com.company.erp.common.gw_sptrain.databean.Gw_sptrainDO; import com.company.erp.common.gw_trainunit.databean.Gw_trainunitDO; import com.company.erp.common.gw_tugroup.databean.Gw_tugroupDO; import com.company.util.Common; import com.gsf.arch.BaseAction; import com.gsf.tool.exception.BaseException; public class Gw_sptrainTree_searchAction extends BaseAction{ @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String id = request.getParameter("id"); if (id != null && !id.equals("")) { response.setCharacterEncoding("gb2312"); response.setContentType("text/xml charset=gb2312"); PrintWriter out = response.getWriter(); searchTopTugroup(request, id, out); out.flush(); return null; } else { return (mapping.findForward("success")); } } public void searchTopTugroup(HttpServletRequest request, String id, PrintWriter out) throws BaseException { Long parent_id = Long.parseLong(id); // 构造数据begin TreeMap<Long, Long> all_map = null; TreeMap<Long, Long> select_map = null; // 专业、岗位 //获取页面hidden的专业工种 String specialtyCode = request.getParameter("specialtyNum"); String positionCode = request.getParameter("positionNum"); if(specialtyCode==null || positionCode==null || specialtyCode.equals("") ||positionCode.equals("")){ }else{ // 根据培训单元 String cacheName = "Gw_sptrain"; String className = Gw_sptrainDO.class.getName(); ArrayList gw_sptrain_list = Common.parseDBCache(cacheName, className); // 进行排序 以trainunit_id排列 培训单元显示的先后顺序 treemap排序 TreeMap<Long, Gw_sptrainDO> gw_sptrain_Select_map = new TreeMap<Long, Gw_sptrainDO>(); for (int i = 0; i < gw_sptrain_list.size(); i++) { Gw_sptrainDO gw_sptrainDO = (Gw_sptrainDO) gw_sptrain_list.get(i); if (gw_sptrainDO.getSpecialtyNum().equals(Long.valueOf(specialtyCode)) && gw_sptrainDO.getPositionNum().equals(Long.valueOf(positionCode))) { gw_sptrain_Select_map .put(gw_sptrainDO.getTrainunit_id(), gw_sptrainDO); } } // 将map的数据add到list ArrayList<Gw_sptrainDO> gw_sptrain_Select_list_filter = new ArrayList<Gw_sptrainDO>(); gw_sptrain_Select_list_filter.addAll(gw_sptrain_Select_map.values()); all_map = searchTugroup(gw_sptrain_list); select_map = searchTugroup(gw_sptrain_Select_list_filter); // 构造数据end // 生成树 String cacheName2 = "Tugroup"; String className2 = Gw_tugroupDO.class.getName(); int sign = 1; StringBuffer bf = new StringBuffer(); // 最下层的培训单元组: Set set = all_map.entrySet(); Collection<Long> coll = all_map.values(); Iterator itr = set.iterator(); while (itr.hasNext()) { Map.Entry entry = (Map.Entry) itr.next(); Long key = (Long) entry.getKey(); Object value = entry.getValue(); // 获得根以下的培训单元组 if (parent_id.equals(((Long) value))) { Gw_tugroupDO gw_tugroupDO = (Gw_tugroupDO) Common .parseDBCache_Row(cacheName2, key.toString(), className2); // 如果培训单元组被禁用 则不显示 组信息 if (gw_tugroupDO != null && "Y".equals(gw_tugroupDO.getIsValid())) { sign=sign+1; boolean check = false; if(select_map.containsKey(key)){ check = true; } //action if(bf.toString().equals("")){ bf.append("["); } bf.append("{ name:\"" + gw_tugroupDO.getTrainUnitGroupName() + "\", asyncUrl: getAsyncUrl,open:false, checked:"+check+", isParent:true,id:" + key + ", group:"+key+",trainunit:\"\"},"); } } } if(sign != 1 && !bf.toString().equals("")){ String str = bf.toString(); str = str.substring(0,str.length()-1); str = str+"]"; out.write(str); } StringBuffer bfTrainunit = new StringBuffer(); if (sign == 1) { // 最下层的单元组的ID // 根据条件 生成培训单元 String cacheName1 = "Trainunit"; String className1 = Gw_trainunitDO.class.getName(); ArrayList gw_trainunit_list = Common.parseDBCache(cacheName1, className1); for (int i = 0; i < gw_trainunit_list.size(); i++) { Gw_trainunitDO gw_trainunitDO = (Gw_trainunitDO) gw_trainunit_list.get(i); Long trainunit_id = gw_trainunitDO.getTrainunit_id(); // 如果培训单元被禁用 则不显示 单元信息 if (gw_trainunitDO != null && "Y".equals(gw_trainunitDO.getIsValid())) { Long tugroup_id = gw_trainunitDO.getTugroup_id(); if (tugroup_id.equals(parent_id)) { boolean check = false; for(int k=0;k<gw_sptrain_Select_list_filter.size();k++){ Gw_sptrainDO gw_sptrainDO = (Gw_sptrainDO) gw_sptrain_Select_list_filter.get(k); if(trainunit_id.equals(gw_sptrainDO.getTrainunit_id())){ check=true; break; } } if(bfTrainunit.toString().equals("")){ bfTrainunit.append("["); } bfTrainunit.append("{ name:\"" + gw_trainunitDO.getTrainName() + "\",open:false, checked:"+check+",id:" + trainunit_id + ",group:\"\" , trainunit:"+trainunit_id+"},"); } } } } if(sign == 1 && !bfTrainunit.toString().equals("")){ String str = bfTrainunit.toString(); str = str.substring(0,str.length()-1); str = str+"]"; out.write(str); } } } }
下层动态加载目录:
bf.append("{ name:\"" + gw_tugroupDO.getTrainUnitGroupName() + "\", asyncUrl: getAsyncUrl,open:false, checked:"+check+", isParent:true,id:" + key + ", group:"+key+",trainunit:\"\"},");
根目录:
bfTrainunit.append("{ name:\"" + gw_trainunitDO.getTrainName() + "\",open:false, checked:"+check+",id:" + trainunit_id + ",group:\"\" , trainunit:"+trainunit_id+"},");
获取目录树中有改变的节点:
function savezTree(){ //保存目录树 //有改变的节点 var srcNode = zTree1.getChangeCheckedNodes(); //alert(srcNode.length); var availableData=""; var groupID=""; var trainunitID=""; for( var i=0; i<srcNode.length; i++){ //alert("name:"+srcNode[i].name); if(srcNode[i].check_False_Full == true && srcNode[i].check_True_Full == true){ availableData=availableData+","+srcNode[i].name; var group = srcNode[i].group; var trainunit = srcNode[i].trainunit; if(typeof(group)!=undefined && group != null && group != ""){ groupID = groupID + group +","; //alert("group:"+group); } if(typeof(trainunit)!=undefined && trainunit != null && trainunit != ""){ trainunitID = trainunitID +","+ trainunit; //alert("trainunit:"+trainunit); } } //alert("id:"+srcNode[i].id); //alert("group:"+srcNode[i].group); //alert("trainunit:"+srcNode[i].trainunit); } //alert(availableData); //alert(trainunitID); var specialtyNum = document.getElementById("id").value; var positionNum = document.getElementById("positionNum").value; //alert(groupID); //alert(trainunitID); //updateSptrainWJS(groupID,trainunitID,specialtyNum,positionNum); if((groupID!=null && groupID!="") || (trainunitID!=null && trainunitID!="")){ UpdateSptrainDWR.updateSptrain(groupID,trainunitID,specialtyNum,positionNum,function(data){ if(data==1){ removeTree(); alert("更新成功!"); }else{ alert("更新失败!"); } }); }else{ alert("更新成功!"); } }
可参照附件中zTreedemo和zTreeAPI v2.6.chm
上一篇: Java游戏服务器之数据库表存取封装
下一篇: 浅谈JAVASE单例设计模式
推荐阅读