ztree实现根节点右击事件,显示添加删除
需求,右击树节点,出现编辑和删除的提示框
1:在setting 配置里面,给callback设置,右击事件onRightClick:
2:写一个函数onRightClick
function onRightClick(event, treeId, treeNode) {
}
3:禁用默认鼠标右击事件
document.oncontextmenu = function(){
return false;
}
4:父节点不需要点击事件,父节点为1,如果节点为1 的时候,不执行下一步
if (treeNode.id == "1") {
return;
}
以上步骤,组成右击事件以下代码:
//右击事件
function onRightClick(event, treeId, treeNode) {
document.oncontextmenu = function(){
return false;
}
//alert(1)
if (treeNode.id == "1") {
return;
}
if (treeNode) {
zTreeObj.selectNode(treeNode);
showContextMenu(treeNode.organId,treeNode.leaf);
/*showContextMenu(treeNode.organId,treeNode.leaf, event.clientX -10, event.clientY -10);*/
}
}
众所周知,在PC端,我们通常用event.clienX或者event.clientY来获取手指的坐标,注释部分的代码控制提示框的位置。
5:触发事件之后,出现提示框
jsp代码:
<div class="dropdown open" id="treeContextMenu" style="display: none;position: absolute">
<ul class="dropdown-menu">
<li><a href="javascript:;" id="toUpdateBtn">编辑</a></li>
<li><a href="#" id="deleteBtn" data-target="#confirmDialog" data-toggle="modal">删除</a></li>
</ul>
</div>
js代码
function showContextMenu(type,leaf, x, y) {
if (type == -1) {
$(".dropdown-menu").find("li:not(:first)").hide();
} else if(leaf){
$(".dropdown-menu").find("li:first").hide();
}else{
$(".dropdown-menu").find("li").show();
}
$("#treeContextMenu").css({"top": y + "px", "left": x + "px"}).show();
$("body").on("mousedown", onBodyMouseDown);
}
6:提示框的一些处理
function hideContextMenu() {
$("#treeContextMenu").hide();
$("body").off("mousedown", onBodyMouseDown);
}
function onBodyMouseDown(event) {
if (!(event.target.id == "treeContextMenu" || $(event.target).parents("#treeContextMenu").length > 0)) {
hideContextMenu();
}
}
项目js代码展示(仅供参考):
var detain = function() {
AssetSavetype = null;
AssetSelecttype = null;
getFloorList();
initLoadMap('detainmap');
var beforeNodeID;
var basePath;
var url;
var setting = {
check : {
enable : true,
chkStyle : "radio",
radioType : "all"
},
view : {
selectedMulti : true,
showLine : false
},
data : {
key : {
name : "name"
},
simpleData : {
enable : true,
idKey : "id",
pIdKey : "pId",
}
},
edit : {
enable : true,
removeTitle : "删除节点",
showRemoveBtn : setRemoveBtn,
showRenameBtn : setRenameBtn
},
async : {
enable : true,
url : "/bison/design/detain/getTree",
autoParam : [ "id" ],
type : "get",
dataFilter : ajaxDataFilter,
dataType : "json"
},
callback : {
onRightClick: onRightClick,
onCheck : zTreeOnCheck,
beforeRemove : zTreeBeforeRemove,
onRemove : zTreeOnRemove,
onRename : zTreeOnRename
}
};
var zTreeObj;
// 初始化根节点
function initTree() {
$.get(basePath + "/design/detain/initNode?type=1", function(data) {
// 设置父节点不显示checkbox
data.returnData.node.nocheck = true;
zTreeObj = $.fn.zTree.init($("#zTree"), setting,
data.returnData.node);
});
}
//右击事件
function onRightClick(event, treeId, treeNode) {
document.oncontextmenu = function(){
return false;
}
//alert(1)
if (treeNode.id == "1") {
return;
}
if (treeNode) {
zTreeObj.selectNode(treeNode);
showContextMenu(treeNode.organId,treeNode.leaf);
/*showContextMenu(treeNode.organId,treeNode.leaf, event.clientX -10, event.clientY -10);*/
}
}
function showContextMenu(type,leaf, x, y) {
if (type == -1) {
$(".dropdown-menu").find("li:not(:first)").hide();
} else if(leaf){
$(".dropdown-menu").find("li:first").hide();
}else{
$(".dropdown-menu").find("li").show();
}
$("#treeContextMenu").css({"top": y + "px", "left": x + "px"}).show();
$("body").on("mousedown", onBodyMouseDown);
}
function hideContextMenu() {
$("#treeContextMenu").hide();
$("body").off("mousedown", onBodyMouseDown);
}
function onBodyMouseDown(event) {
if (!(event.target.id == "treeContextMenu" || $(event.target).parents("#treeContextMenu").length > 0)) {
hideContextMenu();
}
}
//编辑信息
$("#toUpdateBtnd").on("click", function() {
layer.open({
type : 2,
title : '编辑信息',
area : [ '1000px', '650px' ],
fix : false, // �
content : basePath + 'personInfo/toAdduser',
end : function() {
}
});
});
function setRemoveBtn(treeId, treeNode) {
if(treeNode.id == 1){
return false;
}
return true;
}
function setRenameBtn(treeId, treeNode) {
if(treeNode.id == 1){
return false;
}
return true;
}
function zTreeBeforeRemove(treeId, treeNode) {
if (confirm("是否确认删除"))
return true;
return false;
}
function zTreeOnRemove(event, treeId, treeNode) {
$.ajax({
url : basePath + "/design/detain/deleteNode",
data : {
id : treeNode.id,
},
type : "get",
success : function(data) {
}
});
deleteDetain(treeNode.id);
}
function zTreeOnRename(event, treeId, treeNode) {
$.ajax({
url : basePath + "/design/detain/updateName",
data : {
id : treeNode.id,
name : treeNode.name
},
type : "POST",
success : function(data) {
}
});
}
// 异步加载数据过滤器
function ajaxDataFilter(treeId, parentNode, responseData) {
var data = responseData.returnData.treeList;
return data;
}
;
// 节点勾选事件
function zTreeOnCheck(event, treeId, treeNode) {
// 显示围栏
if (beforeNodeID != treeNode.id) {
electronicLayerOff = true;
beforeNodeID = treeNode.id;
}
showDetain([ treeNode.id ]);
}
;
// 获取项目路径
function getContextPath() {
var currentPath = window.document.location.href;
var pathName = window.document.location.pathname;
var pos = currentPath.indexOf(pathName);
var localhostPath = currentPath.substring(0, pos);
var projectName = pathName.substring(0,
pathName.substr(1).indexOf('/') + 1);
return (localhostPath + projectName);
}
// 显示配置记录
function showDetain(DetainNum) {
electronicLayer.getSource().clear();
if (electronicLayerOff) {
for (var num = 0; num < DetainNum.length; num++) {
var electronicParam = {
service : 'WFS',
version : '1.1.0',
request : 'GetFeature',
typeName : DBs + ':detain',
outputFormat : 'application/json',
cql_filter : "bid='" + DetainNum[num] + "'"
};
$.ajax({
url : wfsUrl,
data : $.param(electronicParam),
type : 'GET',
dataType : 'json',
success : function(response) {
var features = new ol.format.GeoJSON()
.readFeatures(response);
electronicLayer.getSource().addFeatures(features);
}
});
}
electronicLayerOff = false;
} else {
electronicLayerOff = true;
}
}
// 资产FID获取
var FIDObject = function(Filter, Typename) {
var Fid = [];
$.ajax({
url : wfsUrl,
data : {
service : 'WFS',
version : '1.1.0',
request : 'GetFeature',
typename : Typename,
outputFormat : 'application/json',
cql_filter : Filter
},
type : 'GET',
dataType : 'json',
async : false,
success : function(response) {
if (response.features.length == 1) {
Fid[0] = response.features[0].id;
} else if (response.features.length > 1) {
for (var i = 0; i < response.features.length; i++) {
Fid[i] = response.features[i].id;
}
} else {
}
}
});
return Fid;
};
// 删除配置记录
function deleteDetain(id) {
var Filter = "bid=" + "'" + id + "'";
var Typename = DBs + ':detain';
var newFeature = new ol.Feature();
newFeature.setId(FIDObject(Filter, Typename)[0]);
var tableType = 'detain';
updateNewFeature([ newFeature ], tableType, 'remove');
if (beforeNodeID == id) {
electronicLayer.getSource().clear();
}
}
// 添加配置
$("#adddetain").on("click", function() {
layer.open({
type : 2,
title : '添加配置',
area : [ '550px', '550px' ],
// fix : false, �
content : [ './adddetain.jsp', ],
end : function() {
initTree();
electronicLayer.getSource().clear();
}
});
});
return {
init : function() {
basePath = getContextPath();
initTree();
}
};
}();
ztree实现根节点右击事件,显示添加删除的更多相关文章
- ztree实现根节点单击事件,显示节点信息
这段时间在维护公司的项目,去年做的项目里面有ztree树的例子,想起之前还没有开始写博客,一些知识点也无从找起,要新加一个右击节点事件,折腾了半天,其中也包含了一些知识点,稍稍做了一些demo. zT ...
- ztree 获取根节点
function getRoot() { var treeObj = $.fn.zTree.getZTreeObj("tree-div"); //返回一个根节点 var node ...
- ztree : checkbox 选中/不选中时动态添加/删除DOM元素
先上代码. var IDMark_Switch = "_switch", IDMark_Icon = "_ico", IDMark_Span = "_ ...
- zTree 无子节点 单击事件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Gridview的RowDataBound事件(添加删除提示,改变背景颜色)
protected void gvTest_RowDataBound(object sender, GridViewRowEventArgs e) { //如果是绑定数据行 if (e.Row.Row ...
- 节点操作-创建并添加&删除节点&替换&克隆节点
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" ...
- Ztree加载完成默认选中根节点右侧生成表格
需求:页面加载完成之后,默认选中ztree的根节点,并执行其点击方法,右侧生成表格: 效果:如下图所示: 思路:在节点点击事件clickNode方法中根据节点的部门code查询这个部门下的所有员工,并 ...
- 【转】 oracle 层次查询判断叶子和根节点
Oracle 9i判断是叶子或根节点,是比较麻烦的一件事情,SQL演示脚本如下: DROP TABLE idb_hierarchical; create TABLE idb_hierarchical ...
- ASPxTreeList控件去根节点的新增修改操作(写在onCommandColumnButtonInitialize()事件中)
treelist去掉根节点按钮效果图: //去掉父节点及子节点旁的新增.修改.删除操作(写在onCommandColumnButtonInitialize事件中) protected void Tre ...
随机推荐
- ECharts树图节点过多时取消缩放,调整容器高度自适应内容变化
问题现象 使用ECharts树图,在数据维度大,节点过多时,所看到的内容会重叠交错,无法查看. 原因 在给定ECharts树图容器尺寸后,无论数据多么庞大或者稀少,数据始终会尝试在给定容器内撑满.全部 ...
- BZOJ 1878 HH的项链 (树状数组+离线)
题目大意:给你一个序列,求某区间出现不同的数的个数. 貌似离线树状数组是最好的解法 先把所有询问挂在它们询问的右端点上 然后从头到尾遍历这个序列,记录这个位置的值上一次出现的位置 那么,当遍历到第i位 ...
- PHP实现的毫秒定时器,同时解决进程不重复堆积
定时器任务,在WEB应用比较常见,如何使用PHP实现定时器任务,大致有两种方案:1)使用Crontab命令,写一个shell脚本,在脚本中调用PHP文件,然后定期执行该脚本:2)配合使用ignore_ ...
- 2019-03-19 用SSIS把SQLServer中的数据导出来保存到Excel中
Control FLow 点击空白处,右键打开Variable,配置存储过程 Excel路径 在SQL Server 中新建一个存储过程,用于从数据表提取特定的数据 create proc Prici ...
- tp框架表单提交注意!不要提交到当前方法
tp框架 表单提交到当前方法,会重复执行显示部分和保存部分的代码.导致不知名的错误.
- 【 【henuacm2016级暑期训练】动态规划专题 G】 Palindrome pairs
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 先用枚举回文串中点的方法. 得到这个字符串中出现的所有的回文. 得到他们的左端点以及右端点. 整理成一个pair<int,in ...
- ZOJ 2705
这题,找找规律,可以发现一个斐波那契数列.按照斐波那契数列求和,知道, SUM=Fn+2-F1,于是,该长度为Fn+2的倍数.因为斐波那契数列不一定是从1开始的,而从2开始的每个数都是从1开始的倍数. ...
- C#上传文件
QQ:1187362408 欢迎技术交流和学习 关于C#上传文件(产品开发): TODO: 1.文件大小不足500M(web.config配置直接处理) 2,文件大小超过500M(ASP.NET分段读 ...
- 安卓WebView的使用,在应用程序中嵌入一个浏览器,轻松地展示各种各样的网页
WebView 在应用程序中嵌入一个浏览器,轻松地展示各种各样的网页. 1.定义一个WebView位置 <?xml version="1.0" encoding=" ...
- m_Orchestrate learning system---二、如何实现验证码自动点击刷新
m_Orchestrate learning system---二.如何实现验证码自动点击刷新 一.总结 一句话总结:传过去的url带随机数来避免读取缓存 onclick="this.src ...