很牛逼的一个树形菜单,树形下拉框插件。一年前用过,很好用。今天又有机会拿过来用,温故一下基本点,nice!!
官方文档说明的非常详细,按照API慢慢看,耐心解读,自然就可以解惑了。
核心代码展示:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>selectmenu.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../../frameworks/css/demo.css" type="text/css">//ztree的样式
<link rel="stylesheet" href="../../frameworks/css/zTreeStyle.css" type="text/css">//ztree的样式
<script type="text/javascript" src="../../frameworks/jquery-1.4.4.min.js"></script>//ztree必须的JS
<script type="text/javascript" src="../../frameworks/jquery.ztree.core-3.5.min.js"></script>//ztree必须的JS <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<SCRIPT type="text/javascript">
<!--
var setting = {
view: {
dblClickExpand: false
},
data: {
simpleData: {
enable: true,
idKey: "id",
pIdKey: "pid",
rootPId: 0
}
},
callback: {
beforeClick: beforeClick,
onClick: onClick
}
}; function beforeClick(treeId, treeNode) {
var check = (treeNode && !treeNode.isParent);
if (!check) alert("只能选择城市...");
return check;
} function onClick(e, treeId, treeNode) {
var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
nodes = zTree.getSelectedNodes(),
v = "";
nodes.sort(function compare(a,b){return a.id-b.id;});
for (var i=0, l=nodes.length; i<l; i++) {
v += nodes[i].name + ",";
$("#mydepId").val(nodes[i].id);
}
if (v.length > 0 ) v = v.substring(0, v.length-1);
var cityObj = $("#citySel");
cityObj.attr("value", v);
var selectValue=$("#mycompId").val();
alert("从ztree中选择的值是:"+selectValue); } function showMenu() {
var cityObj = $("#citySel");
var cityOffset = $("#citySel").offset();
console.log(cityOffset);
$("#menuContent").css({left:cityOffset.left + "px", top:cityOffset.top + cityObj.outerHeight() + "px"}).slideDown("fast"); $("body").bind("mousedown", onBodyDown);
}
function hideMenu() {
$("#menuContent").fadeOut("fast");
$("body").unbind("mousedown", onBodyDown);
}
function onBodyDown(event) {
if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
hideMenu();
}
} function createTree () {
var zNodes;
$.ajax({
url: '/omss/ztreeDepInfo', //url action是方法的名称
type: 'POST',
data:{ compId: "46" },
dataType: "json", //可以是text,如果用text,返回的结果为字符串;如果需要json格式的,可是设置为json
ContentType: "application/json; charset=utf-8",
success: function(data) {
zNodes = data;
console.log("ztreedata="+JSON.stringify(data));
$.fn.zTree.init($("#treeDemo"), setting, zNodes);
},
error: function(msg) {
alert("失败");
}
});
} $(document).ready(function() {
createTree();
});
//-->
</SCRIPT>
</head> <body> <input class="input-medium " id="citySel" type="text" readonly value="" style="width:200px;"/>
<div id="mydepId" class="hide"></div>
<button data-toggle="dropdown" id="menuBtn" onclick="showMenu(); return false;">
<span class="ace-icon fa fa-caret-down icon-only"></span>
</button >
<div id="menuContent" class="menuContent" style="display:none; position: absolute;">
<ul id="treeDemo" class="ztree" style="margin-top:0; width:240px;"></ul>
</div>
</body>
</html>
展示的效果如下: