关于zTree插件删除节点的问题

bianweiall 发布于 2013/11/06 15:54
阅读 3K+
收藏 0

@zTree 你好,想跟你请教个问题:

我的beforeRemove和onRemove为什么不能弹出?我哪里有问题吗?


var zTree, rMenu;

function OnRightClick(event, treeId, treeNode) {
	if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) {
		zTree.cancelSelectedNode();
		showRMenu("root", event.clientX, event.clientY);
	} else if (treeNode && !treeNode.noR) {
		zTree.selectNode(treeNode);
		showRMenu("node", event.clientX, event.clientY);
	}
}

function showRMenu(type, x, y) {
	$("#rMenu ul").show();
	if (type == "root") {
		$("#m_del").hide();
	} else {
		$("#m_del").show();
	}
	rMenu.css({
		"top": y + "px",
		"left": x + "px",
		"visibility": "visible"
	});

	$("body").bind("mousedown", onBodyMouseDown);
}

function hideRMenu() {
	if (rMenu) rMenu.css({
		"visibility": "hidden"
	});
	$("body").unbind("mousedown", onBodyMouseDown);
}

function onBodyMouseDown(event) {
	if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length > 0)) {
		rMenu.css({
			"visibility": "hidden"
		});
	}
}

var addCount;

function getAndEditNode(zTree, fNode) {
	var nNode = zTree.getNodeByParam("temp", addCount, null);
	nNode.temp = 0;
	nNode.Ev = 0;
	if (fNode) {
		nNode.Fid = fNode.Id;
	} else {
		nNode.Fid = "0";
	}
	editTreeNode(nNode);
	addCount++;
}

function addTreeNode() {
	hideRMenu();
	var newNode = {
		Name: "新增" + addCount,
		temp: addCount
	};
	var fNode = zTree.getSelectedNodes()[0];
	if (fNode) {
		zTree.addNodes(fNode, newNode);
		getAndEditNode(zTree, fNode);
	} else {
		zTree.addNodes(null, newNode);
		getAndEditNode(zTree, fNode);
	}
}

function postData(jsonData, url) {
	var str;
	$.ajax({
		type: "post",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		url: url,
		data: jsonData,
		async: false,
		success: function(data) {
			str = data.Id;
		}
	});
	return str;
}

function editTreeNode(nNode) {
	hideRMenu();
	if (nNode) {
		zTree.editName(nNode);
	}
}

function beforeRename(treeId, treeNode, newName, isCancel) {
	if (newName.length == 0) {
		alert("节点名称不能为空.");
		return false;
	}
	return true;
}

function onRename(e, treeId, treeNode, isCancel) {
	if (treeNode.Ev == 0) {
		var id = postData(JSON.stringify(treeNode), "/green/category/create");
		treeNode.Id = id;
	} else {
		postData(JSON.stringify(treeNode), "/green/category/update");
		zTree.updateNode(treeNode);
	}

}

function beforeRemove(treeId, treeNode) {
	alert("treeId:" + treeNode.Id);
	return true;
}

function onRemove(event, treeId, treeNode) {
	alert("treeId:" + treeNode.Id);
}

$(document).ready(function() {

	$("#cangkuguanli").bind("click", function() {
		$("#treeBox").css("display", "block");
		$("article").css("width", "544px");

		var setting = {
			edit: {
				enable: true,
				showRemoveBtn: false,
				showRenameBtn: false
			},
			async: {
				enable: true,
				url: "green/category/list"
			},
			data: {
				key: {
					name: "Name"
				},
				simpleData: {
					enable: true,
					idKey: "Id",
					pIdKey: "Fid",
					rootPId: 0
				}
			},
			callback: {
				beforeRename: beforeRename,
				beforeRemove: beforeRemove,
				onRename: onRename,
				onRemove: onRemove,
				onRightClick: OnRightClick
			}
		};

		$.fn.zTree.init($("#tree"), setting);
		zTree = $.fn.zTree.getZTreeObj("tree");
		rMenu = $("#rMenu");
		addCount = 1;

	});

	//ztree添加一个新节点
	$("#m_add").click(function() {
		addTreeNode();
	});

	//ztree修改一个节点名称
	$("#m_rename").click(function() {
		var nNode = zTree.getSelectedNodes()[0];
		nNode.Ev = 1;
		editTreeNode(nNode);
	});

	//ztree删除一个节点
	$("#m_del").click(function() {
		var nNode = zTree.getSelectedNodes()[0];
		if (nNode) {
			hideRMenu();
			zTree.removeNode(nNode);
		}

	});

});

加载中
0
崔小涣
崔小涣

133行 开始 showRemoveBtn:true  就可以了 这是显示删除按钮

同理,下面showRenameBtn true 显示编辑名图标
zTree
zTree
回复 @崔小涣 : 感谢热心
bianweiall
bianweiall
谢谢你了!
崔小涣
崔小涣
回复 @bianweiall : 直接加在185行和186行之间就行了。一个form确定或者一个alert提示
bianweiall
bianweiall
我是搞定自定义右键菜单,所以关掉了这两个按钮,但是我现在添加和修改没问题,到删除的时候这个节点最后也删除了,我想在删除前加个提示,但alert都不出来
返回顶部
顶部