菜单

因zTree树形菜单的施用实例

2018年11月15日 - Bootstrap

zTree参数设置

以每个节点上加 id 和 pid, id
表示目前节点编号,pid 代表父节点编号

var setting = {                view: {                  showIcon: false  //隐藏icon              },              data: {                  key: {                      children: "setSubArea"  //更加json设置子节点的名称json格式 {id:'1', name: '深圳', setSubArea[{}] }                  }              },              check : {                  enable: true,                  autoCheckTrigger: false,                  chkboxType: { "Y": "p", "N": "s" }                  }              //每个节点上是否显示 CheckBox            };  

第一步:在页面显示菜单位置,添加 ul设置 class=”ztree”

告加载数据

其次步:开启简单多少格式支持

 1 $(document).ready(function(){   2             //加载页面是发送异步请求   3             $.ajax({      4                 type: 'POST',      5                 dataType : "json",      6                 url: "queryAreaTreeJson.action",//请求的action路径       7                 success:function(data){ //请求成功后处理函数。    8                     zNodes = data;   9                     zTreeObj= $.fn.zTree.init($("#areaTree"), setting, data); 
               //根据input中的value值,是树形结构选中相应节点  10                     var treeObj = $.fn.zTree.getZTreeObj("areaTree");  11                     var inputArea = $("#areaNames").val().split();  12                     for (var i = 0; i < inputArea.length; i ++) {  13                         var node = treeObj.getNodeByParam("name", inputArea[i]);//$("#areaNames").val()  14                         treeObj.checkNode(node, true, true);  15                     }  16                 }     17            });   18             $("#area").blur(getSelectNodes);  //input获取焦点自动触发  19         });

老三步:编写树形菜单数据

依据zTree选中之值,显示在input框中

季步:生成树形菜单

function getSelectNodes() {          var treeObj = $.fn.zTree.getZTreeObj("areaTree");          var nodes = treeObj.getCheckedNodes(true);          var areaArr = [];          for (var i = 0; i < nodes.length; i ++){              var node = nodes[i];              console.log(node);              if (node.isParent == false || (node.check_Child_State == 0)) {  //选中的节点并且该节点的孩子节点没选中或者是叶子节点                  areaArr.push(node.name);              }          }                     $("#areaNames").val(areaArr.join(","));          $('#myModal').modal('hide');                }

<button class="btn btn-primary btn-xs" data-toggle="modal" data-target="#myModal" onclick="return false;">选择地区</button>    <!-- 模态框(Modal) -->          <div class="modal fade" id="myModal" tabindex="-1" role="dialog"              aria-labelledby="myModalLabel" aria-hidden="true">             <div class="modal-dialog">                <div class="modal-content">                   <div class="modal-header" style="padding:5px;">                      <!-- <button type="button"                           data-dismiss="modal" aria-hidden="true">                            &times;                      </button> -->                      <h4 class="modal-title" id="myModalLabel">                             选择地区                      </h4>                   </div>                   <div class="modal-body">                      <div class="zTreeDemoBackground left">                          <ul id="areaTree" class="ztree" style="max-height:300px;width:auto; overflow-y:scroll;"></ul>                      </div>                   </div>                   <div class="modal-footer" style="padding:5px;">                      <button type="button" class="btn btn-primary"  onclick="getSelectNodes();">                             确认                      </button>                      <button type="button" class="btn btn-default"                          data-dismiss="modal" onclick="getSelectNodes();">关闭                      </button>                   </div>                </div><!-- /.modal-content -->              </div><!-- /.modal -->          </div>
<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title>ztree树形菜单的使用</title>
  <!-- 导入jquery核心类库 -->
  <script type="text/javascript" src="../js/jquery-1.8.3.js"></script>
  <!-- 导入easyui类库 -->
  <link id="easyuiTheme" rel="stylesheet" type="text/css" href="../js/easyui/themes/default/easyui.css" rel="external nofollow" >
  <link rel="stylesheet" type="text/css" href="../js/easyui/themes/icon.css" rel="external nofollow" >
  <link rel="stylesheet" type="text/css" href="../css/default.css" rel="external nofollow" >
  <script type="text/javascript" src="../js/easyui/jquery.easyui.min.js"></script>
  <!--引入ztree-->
  <script src="../js/ztree/jquery.ztree.all-3.5.js" type="text/javascript" charset="utf-8"></script>
  <link rel="stylesheet" type="text/css" href="../js/ztree/zTreeStyle.css" rel="external nofollow" />
  <script type="text/javascript">
   //页面加载后执行
   $(function() {
    //1.进行ztree树形菜单设置,开启简单json数据支持
    var setting = {
     data:{
      simpleData:{
       enable:true//开启简单json数据格式支持
      }
     }
    };
    //2.提供ztree树形菜单数据
    var zNodes = [
     {id:1,pId:0,name:"父节点一"},
     {id:2,pId:0,name:"父节点二"},
     {id:11,pId:1,name:"子节点一"},
     {id:12,pId:1,name:"子节点二"},
     {id:13,pId:2,name:"子节点三"},
     {id:14,pId:2,name:"子节点四"}
    ];
    //3.生成树形菜单
    $.fn.zTree.init($("#baseMenu"),setting,zNodes);
   });
  </script>
 </head>
 <body class="easyui-layout">
  <div data-options="region:'north',title:'你我他学习吧-学习Java的好博客!'" style="height:100px"></div>
  <div data-options="region:'west',title:'菜单导航'" style="width:200px">
   <!--折叠面板-->
   <div class="easyui-accordion" data-options="fit:true">
    <div data-options="title:'基础菜单'">
     <!--通过ztree插件,制作树形菜单-->
     <ul id="baseMenu" class="ztree"></ul>
    </div>
    <div data-options="title:'系统菜单'">你我他学习吧</div>
   </div>
  </div>
  <div data-options="region:'center',title:'中部区域'">
   <!--选项卡面板-->
   <div id="mytabs" class="easyui-tabs" data-options="fit:true">
    <div data-options="title:'CSDN博客',closable:true">选项卡面板一</div>
    <div data-options="title:'博客园',closable:true">选项卡面板二</div>
   </div>
  </div>
  <div data-options="region:'east',title:'东部区域'" style="width:100px"></div>
  <div data-options="region:'south',title:'南部区域'" style="height:100px"></div>
 </body>
</html>

 

上述这篇基于zTree树形菜单的施用实例就是微编分享给大家之全部内容了,希望能于大家一个参考,也盼大家多多支持脚本的小。

图片 1

君或许感兴趣之章:


相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图