JQuery zTree v2.6 基本用法实例
JQuery zTree v2.6
基本用法实例
初始化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、nameasyncParamOther: ["sptrainID","50"], //其它参数 ( key, value 键值对格式)callback:{beforeAsync: zTreeBeforeAsync,asyncSuccess: zTreeOnAsyncSuccess,asyncError: zTreeOnAsyncError}};
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);}
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{@Overridepublic 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);// 构造数据beginTreeMap<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到listArrayList<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;}//actionif(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("更新成功!");}}