1. zTree拖拽功能存储到数据库
callback : { beforeDrag : beforeDrag, beforeDrop : beforeDrop, }
//拖拽前,检查是否允许拖拽,是否为根节点 function beforeDrag(treeId,treeNode){ for(var i=0;i<treeNode.length;i++){ if(treeNode[i].drag == false || !treeNode[i].getParentNode()) return false; } return true; } function beforeDrop(treeId, treeNode, targetNode) { //处理拖拽事件 for(var i = 0;i<treeNode.length;i++){ var curNode = treeNode[i]; var pNode = curNode.getParentNode(); //只允许同级拖拽 if(pNode && pNode !== targetNode.getParentNode()){ return false; } } handleDrag(treeNode,targetNode); }
List<***> dataList = ...;//获取ztree节点list String firstCurNode = *** ; //第一个节点 String lastCurNode = ***; //最后一个节点(支持多个节点同时拖拽) Integer firstCurNodeIndex = null; Integer lastCurNodeIndex = null; Integer tarIndex = null; for(int i = 0;i<dataList.size();i++){ if(DataList.get(i).getNodeName().equals(firstCurNode)) firstCurNodeIndex = i; if(DataList.get(i).getNodeName().equals(lastCurNode)) lastCurNodeIndex = i; if(DataList.get(i).getNodeName().equals(targetNode)) tarIndex = i; } //向下移动 if(lastCurNodeIndex<tarIndex){ Integer offset = tarIndex-lastCurNodeIndex;//移动距离 for(int i=lastCurNodeIndex+1;i<=tarIndex;i++){ Integer nodeOrder = dataList.get(i).getNodeOrder(); dataList.get(i).setNodeOrder(nodeOrder-currentNodes.length); } for(int j = firstCurNodeIndex;j<=lastCurNodeIndex;j++){ Integer curNodeOrder = dataList.get(j).getNodeOrder(); dataList.get(j).setNodeOrder(curNodeOrder+offset); } //向上移动 }else{ Integer offset = firstCurNodeIndex - tarIndex;//移动距离 for(int i=firstCurNodeIndex-1;i>=tarIndex;i--){ Integer nodeOrder = dataList.get(i).getNodeOrder(); dataList.get(i).setNodeOrder(nodeOrder+currentNodes.length); } for(int j = firstCurNodeIndex;j<=lastCurNodeIndex;j++){ Integer curNodeOrder = dataList.get(j).getNodeOrder(); dataList.get(j).setNodeOrder(curNodeOrder-offset); } } //保存到数据库 saveToDb();2. zTree shift键区域多选/勾选
callback : { onCheck : onCheck, //回调 } //处理shift键多节点勾选 function onCheck(event, treeId, treeNode) { var preClickedNode = window.preClickedNode; window.preClickedNode=treeNode; event = window.event||event;//兼容IE if((!event.shiftKey && !event.srcEvent.shiftKey)||!preClickedNode){//event.srcEvent.shiftKey解决firefox兼容性问题 console.log("event shiftKey error"); return;// shift键 } if(preClickedNode.getParentNode()!=treeNode.getParentNode()){ //是否同级 preClickedNode=null; return; } var obj = jQuery.fn.zTree.getZTreeObj(treeId); // obj.selectNode(preClickedNode,true); //选择 var firstNode =obj.getNodeIndex(preClickedNode); var lastNode =obj.getNodeIndex(treeNode); var count = lastNode - firstNode; var nodeNew = preClickedNode; if (count > 0) { for (var i = 1; i < count; i++){ nodeNew = nodeNew.getNextNode(); if(!nodeNew)break;//用于排除隐患 // obj.selectNode(nodeNew, true); //选择 obj.checkNode(nodeNew, true, true);//勾选 } }else { for (var j = 1; j < (-count); j++) { nodeNew = nodeNew.getPreNode(); if(!nodeNew)break;//用于排除隐患 // obj.selectNode(nodeNew, true); //选择 obj.checkNode(nodeNew, true, true);//勾选 } } // window.preClickedNode=null; } }