Easyui和zTree兩種方式分別實現樹形下拉框,easyuiztree

來源:互聯網
上載者:User

Easyui和zTree兩種方式分別實現樹形下拉框,easyuiztree

最近工作中需要用到樹形下拉框,因為項目中樹形結構使用的是zTree,然後就百度,結果出來效果不好看,後來就試著用了easyui的comboTree,雖然比較好看,但是跟整體的bootstrap風格有點兒不搭。現在貼出來兩種方式及效果,以後備用。

方式一,使用zTree

前端代碼:

<div class="form-group">   <label>點擊事件:</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代碼:

/*    * 點擊事件下拉樹的設定    */   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     }   };   /*    * 點擊事件下拉樹的點擊事件    */   function actionTypeOnClick(e, treeId, treeNode) {     $("#actionTypeId").val(treeNode.id);     $("#selectActionType").val(treeNode.text);   }   /*    * 初始化點擊事件類型    *    */   function initActionType() {     $.ajax({       async: false,       cache: false,       type: 'POST',       dataType: "json",       url: localStorage.getItem("adminPath") + '/touch/typeTable/getActionList?businessTypeId=2',       error: function () {//請求失敗處理函數         alert('請求失敗');       },       success: function (data) { //請求成功後處理函數         $.fn.zTree.init($("#actionTypeTree"), actionTypeSetting, data);       }     });   }   /*    * 展示點擊事件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滑鼠按下事件回呼函數    */   function onBodyDownByActionType(event) {     if (event.target.id.indexOf('switch') == -1) {       hideActionTypeMenu();     }   }   /*    * 隱藏點擊事件Tree    */   function hideActionTypeMenu() {     $("#actionTreeContent").fadeOut("fast");     $("body").unbind("mousedown", onBodyDownByActionType);   } 

方式二:使用easyui

前端代碼:

<div class="form-group">   <label>點擊事件:</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) {    /**     * 當選中該節點時展開該節點,同時關閉其他節點     */    if (node.state == "closed") {      $(this).tree('expand', node.target);    }    else {      var isLeaf = $(this).tree('isLeaf', node.target);      if (!isLeaf) {        $(this).tree("collapse", node.target);      }    }  }});

總結

以上所述是小編給大家介紹的Easyui和zTree兩種方式分別實現樹形下拉框,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對幫客之家網站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.