每一年都奔走在自己热爱里

 
没有人是一座孤岛,总有谁爱着你

【】SpringBoot-LayUI之左侧菜单栏动态实现

SpringBoot后台管理项目之环境部署
SpringBoot后台管理之Mybatis-GeneratorConfig
SpringBoot后台管理项目之数据库
SpringBoot项目之Resuful接口+工具类设计
【待补】SpringBoot项目之shiro
【待补】SpringBoot项目之cache

<!DOCTYPE html> 

<html lang="en" xmlns:th="http://www.thymeleaf.org"> 

<head> 

        <meta charset="UTF-8">

        <title>管理平台</title>

        <meta name="renderer" content="webkit|ie-comp|ie-stand">

        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

        <meta name="viewport" content="width=device-width, 
initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, 
user-scalable=0">

        <meta http-equiv="Cache-Control" content="no-siteapp" />

        <link rel="stylesheet" href="../layui/css/layui.css" />

        <style>

            iframe{

                width: 100%;

                height: 100%;

            }

        </style>

    </head>

    <body class="layui-layout-body">

<div class="layui-layout layui-layout-admin">

  <div class="layui-header">

    <div class="layui-logo">后台管理系统</div>

  

    <ul class="layui-nav layui-layout-left">

   

        <li class="layui-nav-item  layui-this"><a href="">最新活动</a></li>

        <li class="layui-nav-item ">

          <a href="javascript:;">产品</a>

          <dl class="layui-nav-child">

            <dd><a href="">选项1</a></dd>

            <dd><a href="">选项2</a></dd>

            <dd><a href="">选项3</a></dd>

          </dl>

        </li>

        <li class="layui-nav-item"><a href="">大数据</a></li>

        <li class="layui-nav-item">

          <a href="javascript:;">解决方案</a>

          <dl class="layui-nav-child">

            <dd><a href="">移动模块</a></dd>

            <dd><a href="">后台模版</a></dd>

            <dd class="layui-this"><a href="">选中项</a></dd>

            <dd><a href="">电商平台</a></dd>

            

          </dl>

        </li>

        <li class="layui-nav-item"><a href="">社区</a></li>

      </ul>

    <ul class="layui-nav layui-layout-right">

      <li class="layui-nav-item">

        <a href="javascript:;">

          <img src="http://t.cn/RCzsdCq" class="layui-nav-img">

          

         用户名:  <span  th:text="'hello, ' + ${session.user.username} + 
'!'" ></span>

      

        </a>

        <dl class="layui-nav-child">

          <dd><a href="">基本资料</a></dd>

          <dd><a href="">安全设置</a></dd>

        

        </dl>

      </li>

      <li class="layui-nav-item"><a th:href="/logout">退出</a></li>

    </ul>

  </div>

  

  <div class="layui-side layui-bg-black">

    <div class="layui-side-scroll">

      <!-- 左侧导航区域(可配合<u>layui</u>已有的垂直导航) -->

      <ul id="left_nav" class="layui-nav layui-nav-tree"  
<u>lay-filter</u>="test">

      </ul>

    </div>

  </div>

  

  <div class="layui-body" style="overflow-y: hidden;">

    <!-- 内容主体区域 -->

    <div class="layui-tab" <u>lay-filter</u>="demo" <u>lay-allowClose</u>="true">

      <ul class="layui-tab-title">

      </ul>

      <div class="layui-tab-content" >

      </div>

    </div>

  </div>

  

  <div class="layui-footer">

    <!-- 底部固定区域 -->

    © layui.com - 底部固定区域

  </div>

</div>





      <script src="../layui/layui.js"></script>

<script>

layui.use(['element','layer'], function(){

      var element = layui.element

      ,layer = layui.layer

      ,element = layui.element

      ,$ = layui.jquery;

       var base="http://localhost:8080"; 

       var get_nav=base+"/permission/list";

       var layer;

    

    

     network();

      /*

       * @todo 重新计算iframe高度

       */

      function FrameWH() { 

          var h = $(window).height() - 164;

          //console.log("高度问题=="+h);

          $("iframe").css("height", h + "px");

      }

      

      function network(){

        $.ajax({

            url:"/permission/list",  

            type: "get",

            async: true,

            success: function(res) {

              //console.log("首页请求结果=="+JSON.stringify(res));

              if (res.code===0) {

                  console.log("请求数据成功");

                  loadnav(res.data);

              } else{

                  layer.msg('暂无数据');

              }

            },

            error:function() { 

              layer.msg("请求失败!");

            } 

          });        

      }

      

      

      function loadnav (navdata) {

          for (var i=0;i<navdata.length;i++) {

              var  first_menu=navdata[i];

               var  childMenus=first_menu.list; 

               //console.log("子菜单=="+JSON.stringify(childMenus));

               
if(childMenus==null||childMenus==undefined||childMenus==""){

                   //console.log("没有子菜单=="+first_menu.name);

                   loadTopMenu(first_menu);

               }else{

                    //console.log("有子菜单=="+first_menu.name);

                    loadHasChildMenu(first_menu);

                }

          }

         //列表添加完后再次执行渲染

         xuanran();

      }

      

      

      //加载带有子菜单的

      function loadHasChildMenu(data){

          var fragment = document.getElementById("left_nav");

          var list = document.createElement('li');

          list.className="layui-nav-item ";

          var child_one_Html='<a 
href="javascript:;">'+data.permissionName+'</a>';

          var childmenus=data.list;

          var child_two_Html='<dl class="layui-nav-child">';

          var childHtmls="";

          for (var i=0;i<childmenus.length;i++) {

              var childmenu=childmenus[i];

              //console.log("1.=多级菜单列表=="+childmenu.name+"=="); 
data-title

              var childHtml='<dd><a class="site-demo-active" 
data-id='+childmenu.id+' data-src='+childmenu.url+' 
data-title='+childmenu.permissionName+'>'+childmenu.permissionName+'</a></dd>';

              childHtmls+=childHtml; 

          }

          child_two_Html +=childHtmls+'</dl>';

          list.innerHTML=child_one_Html+child_two_Html;

          fragment.appendChild(list);

          //console.log("2.=多级菜单列表加载完毕");

      }

            

      //加载顶级菜单(没有子菜单)

      function loadTopMenu(data){

          var fragment = document.getElementById("left_nav");

          var list = document.createElement('li'); 

          list.className="layui-nav-item";

      //aa

          list.innerHTML='<a class="site-demo-active" 
data-id='+data.id+' data-src='+data.url+'>'+data.permissionName+'</a>';

          fragment.appendChild(list);

          //console.log("1.=顶级菜单列表加载完毕"); 

      }

      

      function xuanran() {

          layui.use(['element','layer'], function(){

            var element = layui.element;

            layer=layui.layer;

            

            var layFilter = $("#left_nav").attr('lay-filter');

            element.render('nav', layFilter); 

            

            var active = {

              //在这里给active绑定几项事件,后面可通过active调用这些事件

              tabAdd: function (url, id, name) {

                  //新增一个Tab项 传入三个参数,分别对应其标题,tab页面的地址,还有一个规定的id,是标签中data-id的属性值

                  var body_url=url+".html";

                  console.log("要切换的页面地址="+body_url);

                  element.tabAdd('demo', {

                      title: name,

                      content: '<iframe data-frameid="' + id + '" 
scrolling="auto" frameborder="0" src="' + body_url + '"></iframe>',

                      id: id //规定好的id

                  })

                  FrameWH();  //计算ifram层的大小

              },

              tabChange: function (id) {

                  //切换到指定Tab项

                  element.tabChange('demo', id); //根据传入的id传入到指定的tab项

              },

              tabDelete: function (id) {

                  element.tabDelete("demo", id);//删除

              }

            };    

            

       //当点击有site-demo-active属性的标签时,即左侧菜单栏中内容 ,触发点击事件

      $('.site-demo-active').on('click', function () {

           var dataid = $(this);

           if ($(".layui-tab-title li[lay-id]").length <= 0) {

               //如果比零小,则直接打开新的tab项

               //console.log("1.没有tab页,新建tab页");

               console.log("2.没有tab页,新建tab页=="+dataid.text()+"=="+dataid.attr("data-id")+"==="+dataid.attr("data-title"));

               active.tabAdd(dataid.attr("data-src"), 
dataid.attr("data-id"),dataid.attr("data-title"));

           } else {

              //否则判断该tab项是否以及存在

              var isData = false; //初始化一个标志,为false说明未打开该tab项 为true则说明已有

              $.each($(".layui-tab-title li[lay-id]"), function () {

                  //如果点击左侧菜单栏所传入的id 在右侧tab项中的lay-id属性可以找到,则说明该tab项已经打开

                  if ($(this).attr("lay-id") == dataid.attr("data-id")) {

                    console.log("如果点击左侧菜单栏所传入的id 在右侧tab项中的lay-id属性可以找到,则说明该tab项已经打开");

                    isData = true;                     

               }

              })

              if (isData == false) {

                //标志为false 新增一个tab项

                console.log("新增一个tab项")

                active.tabAdd(dataid.attr("data-src"), 
dataid.attr("data-id"),dataid.attr("data-title"));

              }

         }

        //最后不管是否新增tab,最后都转到要打开的选项页面上

        active.tabChange(dataid.attr("data-id"));

      });

    })

    console.log("==列表添加完后再次执行渲染");

}

})

</script>        

    </body>

</html>

posted @ 2021-04-13 17:31  与乐i  阅读(1061)  评论(0编辑  收藏  举报
Live2D
// 生成目录索引列表 //侧边栏目录索引