<link href="https://www.jb51.net/${base}/template/ztree/css/demo.css" type="text/css"> <link href="https://www.jb51.net/${base}/template/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css"> <script type="text/javascript" src="https://www.jb51.net/${base}/template/ztree/js/jquery.ztree.core.js"></script>
这里只有demo.css是自己添加的,其他都是官方制定的,demo.css是将官方的demo用到的css修改的,如下(这里有冗余样式没有删除掉);
div.content_wrap {width: 400px;} div.content_wrap div.left{float: left;} div.content_wrap div.right{float: right;width: 340px;} div.zTreeDemoBackground {text-align:left;} ul.ztree {margin-top: 10px;border: 1px solid #617775;background: #fefefe;width:220px;height:360px;overflow-y:scroll;overflow-x:auto;} ul.log {border: 1px solid #617775;background: #f0f6e4;width:300px;height:170px;overflow: hidden;} ul.log.small {height:45px;} ul.log li {color: #666666;list-style: none;padding-left: 10px;} ul.log li.dark {background-color: #E3E3E3;} /* ruler */ div.ruler {height:20px; width:220px; background-color:#f0f6e4;border: 1px solid #333; margin-bottom: 5px; cursor: pointer} div.ruler div.cursor {height:20px; width:30px; background-color:#3C6E31; color:white; text-align: right; padding-right: 5px; cursor: pointer}
然后,就是对应的下拉框:
<div> <div> <input type="text" readonly value=""/> <input type="hidden" > <input type="button" value="∨"> </div> </div> 8<div> <ul></ul> </div>
这里有一个隐藏的文本框用来存放下拉框选择内容对应的id。
对应的脚本如下:
<SCRIPT type="text/javascript"> var setting = { view: { dblClickExpand: false }, data: { simpleData: { enable: true } }, callback: { onClick: onClick }, view: { // 不显示对应的图标 showIcon: false } }; function onClick(e, treeId, treeNode) { var zTree = $.fn.zTree.getZTreeObj("treeDemo"), nodes = zTree.getSelectedNodes(), v = ""; ids = ""; 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 + ","; ids += nodes[i].id + ","; } if (v.length > 0 ) v = v.substring(0, v.length-1); var cityObj = $("#citySel"); cityObj.attr("value", v); // 将选中的id放到隐藏的文本域中 if (ids.length > 0 ) ids = ids.substring(0, ids.length-1); var treeids = $("#treeids"); treeids.attr("value", ids); } function showMenu() { var cityObj = $("#citySel"); var cityOffset = $("#citySel").offset(); $("#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(); } } var zNodes ; $(document).ready(function(){ // 加载数据 $.ajax({ async : false, cache:false, type: 'POST', dataType : 'json', url: '${base}/admin/goods!getGoodsCategoryTreeJson.action', error: function () { alert('请求失败'); }, success:function(data){ zNodes = data; } }); $.fn.zTree.init($("#treeDemo"), setting, zNodes); }); </SCRIPT>
这样,一个下拉框就做完了。
如下图所示:
如果,需要在修改页面中回写相应的下拉列表数据,添加如下的脚本:
<script type="text/javascript"> $(document).ready(function(){ if ("${goods.goodsCategory.id}"!="") { var treeObj = $.fn.zTree.getZTreeObj("treeDemo"); var node = treeObj.getNodeByParam("id", "${goods.goodsCategory.id}" , null); treeObj.selectNode(node,false , false); onClick(event,"${goods.goodsCategory.id}",node,true); } }); </script>