Ztree实现拖拽功能

泪湿孤枕 提交于 2019-12-31 22:57:11

树节点设置:

 this.tree = $.fn.zTree.init($("#topologyTree"), {
				async:{
					enable:true 
				},
				edit : {
					enable : true,
					drag : {
						isMove : true, // 打开移动功能,鼠标左键点击后拖拽
						prev : true,
						autoOpenTime : 0
					},
					editNameSelectAll : true,
		 			showRemoveBtn : true,
		 			removeTitle : "删除节点",
		 			renameTitle : "编辑节点名称",
		 			showRenameBtn : true,
				},
				view : {
					selectedMulti : false,
					showIcon : true,// 显示图标
					showLine : true,
					addHoverDom : addHoverDom ,
					removeHoverDom : removeHoverDom 
				},
				data : {
					simpleData : {
						enable : true,
						idKey : 'id',
						pIdKey : 'pId',
						rootPId : 0
					},
					key : {
						name : "text"
					}
				},
				check : {
					enable : false
				},
				callback : {
 	 			   	beforeDrag : beforeDrag,
	 				beforeDrop : beforeDrop,
	 				onDrop : zTreeOnDrop
				}
			}, treeData);

拖拽方法实现:

 
//拖拽之前
 function beforeDrag(treeId, treeNodes) {
  	console.log(treeNodes);
 	if (treeNodes[0].id == "sys_A1") {
 		return false;
 	}
 	return true;
 }
 
 // 拽操作结束之前
 function beforeDrop(treeId, treeNodes, targetNode, moveType) {
 	if (targetNode == null) {
  		return false;
 	}
 	if (targetNode.id == treeNodes[0].id) {
  		return false;
 	}
 	var a_ = targetNode.nodeType != 0 || targetNode.nodeType != 4;
 
 	return a_;
  }
 
 
 // 拖拽操作结束
 function zTreeOnDrop(event, treeId, treeNodes, targetNode, moveType) {
 	if (targetNode == null || targetNode.id == treeNodes[0].id) {
 		return;
 	}
 
 	console.log(treeNodes[0].id + " 已移动至:" + targetNode.name + "-"+ targetNode.id + "下");
 	var index_ = layer.load(2);
 	$.ajax({
 		type : "post",
 		url : basePath + '/sncTopology/dragTreeNode',
 		async : true, // 使用同步方式
 		data : JSON.stringify({
 			nodeId : treeNodes[0].id,
 			targetId : targetNode.id,
 			nodeType : treeNodes[0].nodeType
 		}),
 		contentType : "application/json; charset=utf-8",
 		dataType : "json",
 		success : function(data) {
 			console.log(data);
 			if (data != null && data.code == 200) {
 				layer.close(index_);
 			} else {
 				layer.msg(data.msg);
 			}
 		}
 	});
 };

后台逻辑:

@Override
	public ReturnT<String> dragTreeNode(Map<String, Object> requestbody) {
		  Object nodeType = requestbody.get("nodeType");
	        if (nodeType == null) {
	            return new ReturnT<String>(500, "没有节点类型");
	        }
	        Object targetId = requestbody.get("targetId");
	        if (targetId == null) {
	            return new ReturnT<String>(500, "没有目标ID");
	        }
	        Object nodeId = requestbody.get("nodeId");
	        if (nodeId == null) {
	            return new ReturnT<String>(500, "没有节点ID");
	        }

	        int result = 0;
	        SncVisualpanelTree sncVisualpanelTree  = sncVisualpanelTreeMapper.selectById(nodeId.toString());
	        
	        sncVisualpanelTree.setParentid(targetId.toString());
	        result = sncVisualpanelTreeMapper.updateById(sncVisualpanelTree);
	        
	        return result > 0 ? ReturnT.SUCCESS : ReturnT.FAIL;
	}
	

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!