最新给公司后台写了一个配置页面,在网上搜到一个js插件ztree,记录一下使用心得。
首先说一下ztree官网,好多方法我都是从官网api上学习的,官网地址http://www.treejs.cn/v3/main.php#_zTreeInfo
切入正题,我开始没有使用异步加载模式,但写时间发现编辑和添加后更新要使用的方法都是在异步加载模式的前提下,建议大家使用异步加载,方便后续操作。
首先是页面引用
<link href="zTree/css/zTreeStyle/zTreeStyle.css" rel="stylesheet" />
<link href="bootstrap-3.3.7/css/bootstrap.css" rel="stylesheet" />
<script src="Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="zTree/js/jquery.ztree.all.js" type="text/javascript"></script>
官方文档中用的jquery是1.4版本,我感觉有点低,用了稍微高点的版本。
js代码如下:
var setting = {
data: {
key: {
//将treeNode的ItemName属性当做节点名称
name: "appName"
},
simpleData: {
//是否使用简单数据模式
enable: true,
//当前节点id属性
idKey: "appId",
//当前节点的父节点id属性
pIdKey: "appFatherId",
//用于修正根节点父节点数据,即pIdKey指定的属性值
rootPId: ""
}
},
async: {
enable: true,//是否启动异步加载模式
url: "handler/DeployZTree.ashx",//一般处理程序得地址
autoParam: ["appId"],//post请求时参数名
otherParam: { "Type": "All" },//附加参数
// dataType: "json",//默认text
type: "post",//默认post
dataFilter: addProperty //异步返回后经过Filter,如果返回的数据不完整,并且修改后台sql语句比较麻烦,可以用这个方法在前台修改
},
view: {
//是否支持同时选中多个节点
selectedMulti: false
},
callback: {
//onAsyncSuccess: zTreeOnAsyncSuccess,
onMouseDown: onMouseDown //鼠标点击节点事件
}
};
ztree基础配置
function addProperty(treeId, parentNode, responseData) {
if (responseData.length > ) {
for (var i = ; i < responseData.length; i++) {
if (parseInt(responseData[i].num) > ) {
responseData[i]["isParent"] = true;
}
}
}
return responseData;
}
addProperty方法
$.post("handler/DeployZTree.ashx", { "Type": "All", "async": "false" }, function (data) {
var nodes = JSON.parse(data);
for (var j = ; j < nodes.length; j++) {
if (parseInt(nodes[j].num) > ) {
nodes[j]["isParent"] = true;
}
}
var treeObj = $.fn.zTree.init($("#tree"), setting, nodes);
//默认展开所有节点
treeObj.expandAll(false);
treeObj.expandNode(treeObj.getNodes()[], true, null, null);
});//加载Ztree
页面加载后第一次返回的数据
$('#Edit').click(function () { var treeObj = $.fn.zTree.getZTreeObj("tree");
var nodes = treeObj.getSelectedNodes();
if (nodes.length < ) {
alert("请选择节点");
return;
} else {
$.ajax({
type: 'POST',
url: "handler/DeployZTree.ashx",
data: { "Type": "FatherNodes" },
success: function (data) {
for (var i = ; i < data.length; i++) {
if ($('#superiorNodeSelect').find("option[value='" + data[i].appId + "']").size() < ) {
$('<option value="' + data[i].appId + '">' + data[i].appName + '</option>').appendTo($('#superiorNodeSelect'));
}
}
var node = nodes[];
$('#NodeName').val(node.appName);
//$('#SuperiorNode').val(node.appFatherName);
$('#superiorNodeSelect').val(node.appFatherId);
$('#PageAddress').val(node.pagePath);
$('#NodeID').val(node.appId);
$('#FatherNodeID').val(node.appFatherId);
var systemid = node.systemType;
$('#systemSelect').val(systemid);
if (!$('#dvTxt').hasClass("hid")) {
$('#dvTxt').addClass("hid");
}
if ($('#dvSelect').hasClass("hid")) {
$('#dvSelect').removeClass("hid");
}
if ($('#ok').hasClass("hid")) {
$('#ok').removeClass("hid");
}
if ($('#nID').hasClass("hid")) {
$('#nID').removeClass("hid");
}
if ($('#fID').hasClass("hid")) {
$('#fID').removeClass("hid");
}
},
dataType: "json"
});
}
});//编辑
编辑按钮点击事件
$('#ok').click(function () {
if ($.trim($('#NodeName').val()) == "" || $('#NodeName').val() == "节点名称") {
alert("节点名称不能为空");
return;
}
if ($('#superiorNodeSelect option:selected').val() == "-1") {
alert("请选择上级节点");
return;
}
if ($('#systemSelect option:selected').val() == "") {
alert("请选择所属系统");
return;
}
if ($.trim($('#PageAddress').val()) == "" || $('#PageAddress').val() == "页面地址") {
alert("页面地址不能为空");
return;
}
if (!confirm("确定提交!")) {
return;
}
var name = $('#NodeName').val();//名称
var fatherNodeId = $('#superiorNodeSelect option:selected').val();//父节点ID
var systemId = $('#systemSelect option:selected').val();//所属系统
var pageAddress = $('#PageAddress').val();//页面地址
if ($('#NodeID').val() == "") {
$.ajax({
type: 'POST',
url: 'handler/DeployZTree.ashx',
data: {
"Type": "Add", "appName": name, "appFatherId": fatherNodeId, "systemType": systemId, "pagePath": pageAddress
},
success: function (data) {
if (data == "success") {
var treeObj = $.fn.zTree.getZTreeObj("tree");
if (!(treeObj.getNodeByParam("appId", fatherNodeId, null) == null)) {
if (treeObj.getNodeByParam("appId", fatherNodeId, null).isParent) {
treeObj.reAsyncChildNodes(treeObj.getNodeByParam("appId", fatherNodeId, null), "refresh");
} else {
if (!(treeObj.getNodeByParam("appId", $('#FatherNodeID').val(), null) == null)) {
treeObj.reAsyncChildNodes(treeObj.getNodeByParam("appId", $('#FatherNodeID').val(), null), "refresh");
}
}
}
alert("添加成功!");
} else {
alert("添加失败!");
}
pageInit();
}
});
}//添加
else {
$.ajax({
type: 'POST',
url: "handler/DeployZTree.ashx",
//appId, appName, appFatherId, systemType, pagePath
data: { "Type": "Edit", "appId": $('#NodeID').val(), "appName": name, "appFatherId": fatherNodeId, "systemType": systemId, "pagePath": pageAddress },
success: function (data) {
if (data == "success") {
var treeObj = $.fn.zTree.getZTreeObj("tree");
if (!(treeObj.getNodeByParam("appId", $('#FatherNodeID').val(), null) == null)) {
treeObj.reAsyncChildNodes(treeObj.getNodeByParam("appId", $('#FatherNodeID').val(), null), "refresh", false, refreshTwo(fatherNodeId));
}
//if (!(treeObj.getNodeByParam("appId", fatherNodeId, null) == null)) {
// treeObj.reAsyncChildNodes(treeObj.getNodeByParam("appId", fatherNodeId, null), "refresh");
//}
alert("修改成功!");
}
else {
alert("修改失败!");
}
pageInit();
}
});
}//修改
});//确定
确定按钮点击事件
treeObj.getNodeByParam() 官方文档中给的是如无结果,返回 [ ],实际测试返回的是null,不知道是什么问题。
$('#Add').click(function () {
var treeObj = $.fn.zTree.getZTreeObj("tree");
var nodes = treeObj.getSelectedNodes();
if (nodes.length < ) {
alert("请选择节点");
return;
} else {
$.ajax({
type: 'POST',
url: "handler/DeployZTree.ashx",
data: { "Type": "FatherNodes" },
success: function (data) {
for (var i = ; i < data.length; i++) {
if ($('#superiorNodeSelect').find("option[value='" + data[i].appId + "']").size() < ) {
$('<option value="' + data[i].appId + '">' + data[i].appName + '</option>').appendTo($('#superiorNodeSelect'));
}
}
$('#NodeID').val("");
$('#dvTxt').addClass("hid");
$('#dvSelect').removeClass("hid");
var node = nodes[];
$('#NodeName').val("");
$('#PageAddress').val("");
$('#FatherNodeID').val(node.appFatherId);
$('#superiorNodeSelect').val(node.appId);
if ($('#ok').hasClass("hid")) {
$('#ok').removeClass("hid");
}
if (!$('#nID').hasClass("hid")) {
$('#nID').addClass("hid");
}
if (!$('#fID').hasClass("hid")) {
$('#fID').addClass("hid");
}
},
dataType: "json"
});
}
});//添加
添加按钮点击事件
<body>
<div>
<ul id="tree" class="ztree"></ul>
</div>
<div class="message">
<div class="operatingStyle">
<input class="btn btn-default customize" type="button" id="Edit" value="编辑">
<input class="btn btn-default customize" type="button" id="Add" value="添加">
</div>
<form class="form-horizontal">
<div class="form-group">
<label for="NodeName" class="col-sm-2 control-label labStyle">节点名称:</label>
<div class="col-sm-10 textWidth">
<input type="text" class="form-control " id="NodeName" placeholder="节点名称">
</div>
</div>
<div id="dvTxt" class="form-group">
<label for="SuperiorNode" class="col-sm-2 control-label labStyle">上级节点:</label>
<div class="col-sm-10 textWidth">
<input type="text" class="form-control " id="SuperiorNode" placeholder="上级节点">
</div>
</div>
<div id="dvSelect" class="form-group hid">
<label for="superiorNodeSelect" class="col-sm-2 control-label labStyle">上级节点:</label>
<select id="superiorNodeSelect" class="form-control selectSystem">
<option value="-1">请选择</option>
</select>
</div>
<div class="form-group">
<label for="systemSelect" class="col-sm-2 control-label labStyle">所属系统:</label>
<select id="systemSelect" class="form-control selectSystem">
<option value="0">请选择</option>
<option value="1">数据质量</option>
<option value="4">日常开发</option>
<option value="5">移动端</option>
</select>
</div>
<div class="form-group">
<label for="PageAddress" class="col-sm-2 control-label labStyle">页面地址:</label>
<div class="col-sm-10 textWidth">
<textarea id="PageAddress" class="form-control textarea" rows="4" placeholder="页面地址"></textarea>
</div>
</div>
<div class="form-group" id="nID">
<label for="NodeID" class="col-sm-2 control-label labStyle">节点ID:</label>
<div class="col-sm-10 textWidth">
<input type="text" class="form-control " id="NodeID" disabled>
</div>
</div>
<div class="form-group" id="fID">
<label for="FatherNodeID" class="col-sm-2 control-label labStyle">父节点ID:</label>
<div class="col-sm-10 textWidth">
<input type="text" class="form-control " id="FatherNodeID" disabled>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input class="btn btn-default submit hid" type="button" id="ok" value="确定">
</div>
</div>
</form>
</div>
</body>
前台页面
后台代码就不贴了,都是业务的流程,没什么好看的