Easyui和zTree兩種方式分別實(shí)現(xiàn)樹形下拉框
最近工作中需要用到樹形下拉框,因?yàn)轫?xiàng)目中樹形結(jié)構(gòu)使用的是zTree,然后就百度,結(jié)果出來效果不好看,后來就試著用了easyui的comboTree,雖然比較好看,但是跟整體的bootstrap風(fēng)格有點(diǎn)兒不搭?,F(xiàn)在貼出來兩種方式及效果,以后備用。
方式一,使用zTree
前端代碼:
<div class="form-group"> <label>點(diǎn)擊事件:</label> <input id="selectActionType" class="form-control" onfocus="showActionTypeTree()" onclick="showActionTypeTree()" readonly="readonly" style="border-radius:5px;background-color: white;cursor: default;"/> <input type="hidden" name="actionTypeId" id="actionTypeId"/> <div id="actionTreeContent" class="menuContent" style="border-radius:5px;display: none; z-index:9999;position: absolute; border: 1px #CCC solid; background-color:#f9f9f9;"> <ul id="actionTypeTree" class="ztree" style="margin-top:0;height: 200px;overflow: auto"></ul> </div> </div>
js代碼:
/* * 點(diǎn)擊事件下拉樹的設(shè)置 */ var actionTypeSetting = { view: { dblClickExpand: true, showIcon: false, fontCss : {"font-weight":"400","font-size":"20px"} }, data: { key: { name: "text", children: "children" }, simpleData: { enable: true } }, callback: { onClick: actionTypeOnClick } }; /* * 點(diǎn)擊事件下拉樹的點(diǎn)擊事件 */ function actionTypeOnClick(e, treeId, treeNode) { $("#actionTypeId").val(treeNode.id); $("#selectActionType").val(treeNode.text); } /* * 初始化點(diǎn)擊事件類型 * */ function initActionType() { $.ajax({ async: false, cache: false, type: 'POST', dataType: "json", url: localStorage.getItem("adminPath") + '/touch/typeTable/getActionList?businessTypeId=2', error: function () {//請(qǐng)求失敗處理函數(shù) alert('請(qǐng)求失敗'); }, success: function (data) { //請(qǐng)求成功后處理函數(shù) $.fn.zTree.init($("#actionTypeTree"), actionTypeSetting, data); } }); } /* * 展示點(diǎn)擊事件SelectTree */ function showActionTypeTree() { $.ajax({ url: localStorage.getItem("adminPath") + '/touch/typeTable/getActionList?businessTypeId=2', type: 'POST', dataType: "json", async: false, success: function (data) { $.fn.zTree.init($("#actionTypeTree"), actionTypeSetting, data); var deptObj = $("#selectActionType"); var deptOffset = $("#selectActionType").offset(); $("#actionTreeContent").css({ left: deptOffset.left - 26 + "px", top: deptOffset.top + "px" }).slideDown("fast"); $('#actionTypeTree').css({width: deptObj.outerWidth() + "px"}); var zTree = $.fn.zTree.getZTreeObj("actionTypeTree"); var node = zTree.getNodeByParam("id", $('#actionTypeId').val(), null); zTree.selectNode(node); $("body").bind("mousedown", onBodyDownByActionType); } }); } /* * Body鼠標(biāo)按下事件回調(diào)函數(shù) */ function onBodyDownByActionType(event) { if (event.target.id.indexOf('switch') == -1) { hideActionTypeMenu(); } } /* * 隱藏點(diǎn)擊事件Tree */ function hideActionTypeMenu() { $("#actionTreeContent").fadeOut("fast"); $("body").unbind("mousedown", onBodyDownByActionType); }
方式二:使用easyui
前端代碼:
<div class="form-group"> <label>點(diǎn)擊事件:</label> <input id="actionTypeId2" name="actionTypeId2" class="form-control" /> </div>
js代碼:
$("#actionTypeId2").combotree({ url: localStorage.getItem("adminPath") + '/touch/typeTable/getActionList?businessTypeId=2', textField:'name', onClick: function (node) { $("#actionTypeId").val(node.id); }, onSelect: function (node) { /** * 當(dāng)選中該節(jié)點(diǎn)時(shí)展開該節(jié)點(diǎn),同時(shí)關(guān)閉其他節(jié)點(diǎn) */ if (node.state == "closed") { $(this).tree('expand', node.target); } else { var isLeaf = $(this).tree('isLeaf', node.target); if (!isLeaf) { $(this).tree("collapse", node.target); } } } });
總結(jié)
以上所述是小編給大家介紹的Easyui和zTree兩種方式分別實(shí)現(xiàn)樹形下拉框,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
jquery.boxy彈出框(后隔N秒后自動(dòng)隱藏/自動(dòng)跳轉(zhuǎn))
對(duì)于 Boxy彈出框的使用之前寫過一些文章(查看jquery.boxy基礎(chǔ)),今天主要是在解決一個(gè)需要之后,覺得值得把它記錄下來,所以就再寫一篇,主要功能是,在彈出對(duì)話框后,隔N秒后自動(dòng)隱藏,還有就是自動(dòng)跳轉(zhuǎn)2013-01-01單擊按鈕發(fā)送驗(yàn)證碼,出現(xiàn)倒計(jì)時(shí)的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)硪黄獑螕舭粹o發(fā)送驗(yàn)證碼,出現(xiàn)倒計(jì)時(shí)的簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03JQuery擴(kuò)展插件Validate 3通過參數(shù)設(shè)置錯(cuò)誤信息
最終顯示在頁(yè)面上的錯(cuò)誤分為兩種:第一種是默認(rèn)錯(cuò)誤信息,該信息已經(jīng)被定義在插件中了,可以手動(dòng)修改。2011-09-09JQuery EasyUI 日期控件如何控制日期選擇區(qū)間
這篇文章主要介紹了JQuery EasyUI 日期控件如何控制日期選擇區(qū)間,需要的朋友可以參考下2014-05-05