动态生成三级菜单

动态生成三级菜单

1.html部分,我需要一个容器用于渲染菜单

<div class="layui-side layui-bg-black" id="admin-side">
                <div class="layui-side-scroll">
                    <ul class="layui-nav layui-nav-tree" id="nav"  lay-filter="demo"></ul>
                </div>
 </div>


接下来是插件以及相关JS,css引入 ,注意:路径问题,换成自己本地的路径

<link rel="stylesheet" href="../layui/css/layui.css">
<script src="../lib/jquery-1.12.2.js" type="text/javascript" charset="utf-8"></script>
<script src="../layui/layui.js"></script>


2.js部分

<script>
    //监听选中页签添加样式
    layui.config({
        base: '../layui/'   //navbar组件js所在目录
    }).use('navbar', function() {
        var navbar = layui.navbar();
        navbar.set({
            elem: '#nav',
            url: "../layui/nav2.json"   //数据源地址,我用了本地写的json数据
        });
        navbar.render();
        //下面的部分不是必须的
        navbar.on('click(demo)', function(data) {
            console.log(data.elem);
            console.log(data.field.title);//标题
            console.log(data.field.icon);//图标
            console.log(data.field.href);//调转地址
            layer.msg(data.field.href);
        });
        
       //给选中的页签添加选中样式(解决刷新失效问题)
       var url = window.location.href.replace("//", "");
        var relUrl = url.substring(url.lastIndexOf("/") + 1);
        //去掉参数部分
        if (relUrl.indexOf("?") != -1) {
            relUrl = relUrl.split("?")[0];
        }
        $("#leftNavbar a").each(function () {
            var that = this;
        if ($(that).attr("href") == relUrl) {
            $(that).parent().addClass("layui-this");
            $(that).parents("li:eq(0)").addClass("layui-nav-itemed");
            var nodes = $(that).parents("li:eq(0)").find("a .layui-nav-more");
            if (nodes.length > 0) {
                nodes.each(function () {
                    if ($(this).parents("dd:eq(0)").find("[href='" + relUrl + 
            "']").length > 0) {
                        $(this).parent().parent().addClass("layui-nav-itemed");
                    }
                });
             }
            }
        });
       
    });
    </script>


重点来了:navbar,js

/**
 * navbar.js
 * @author 御风 <1945199284@qq.com>
 */
layui.define(['element', 'common'], function (exports) {
    "use strict";
    var $ = layui.jquery,
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        element = layui.element,
        common = layui.common,
        cacheName = 'tb_navbar';
 
    var Navbar = function () {
        /**
         *  默认配置
         */
        this.config = {
            elem: undefined, //容器
            data: undefined, //数据源
            url: undefined, //数据源地址
            type: 'GET', //读取方式
            cached: false, //是否使用缓存
            spreadOne: false //设置是否只展开一个二级菜单
        };
        this.v = '1.0.0';
    };
    //渲染
    Navbar.prototype.render = function () {
        var _that = this;
        var _config = _that.config;
        if (typeof (_config.elem) !== 'string' && typeof (_config.elem) !== 'object') {
            common.throwError('Navbar error: elem参数未定义或设置出错,具体设置格式请参考文档API.');
        }
        var $container;
        if (typeof (_config.elem) === 'string') {
            $container = $('' + _config.elem + '');
        }
        if (typeof (_config.elem) === 'object') {
            $container = _config.elem;
        }
        if ($container.length === 0) {
            common.throwError('Navbar error:找不到elem参数配置的容器,请检查.');
        }
        if (_config.data === undefined && _config.url === undefined) {
            common.throwError('Navbar error:请为Navbar配置数据源.')
        }
        if (_config.data !== undefined && typeof (_config.data) === 'object') {
            var html = getHtml(_config.data);
            $container.html(html);
            element.init();
            _that.config.elem = $container;
        } else {
            if (_config.cached) {
                var cacheNavbar = layui.data(cacheName);
                if (cacheNavbar.navbar === undefined) {
                    $.ajax({
                        type: _config.type,
                        url: _config.url,
                        async: false, //_config.async,
                        dataType: 'json',
                        success: function (result, status, xhr) {
                            //添加缓存
                            layui.data(cacheName, {
                                key: 'navbar',
                                value: result
                            });
                            var html = getHtml(result);
                            $container.html(html);
                            element.init();
                        },
                        error: function (xhr, status, error) {
                            common.msgError('Navbar error:' + error);
                        },
                        complete: function (xhr, status) {
                            _that.config.elem = $container;
                        }
                    });
                } else {
                    var html = getHtml(cacheNavbar.navbar);
                    $container.html(html);
                    element.init();
                    _that.config.elem = $container;
                }
            } else {
                //清空缓存
                layui.data(cacheName, null);
                $.ajax({
                    type: _config.type,
                    url: _config.url,
                    async: false, //_config.async,
                    dataType: 'json',
                    success: function (result, status, xhr) {
                        var html = getHtml(result);
                        $container.html(html);
                        element.init();
                    },
                    error: function (xhr, status, error) {
                        common.msgError('Navbar error:' + error);
                    },
                    complete: function (xhr, status) {
                        _that.config.elem = $container;
                    }
                });
            }
        }
 
        //只展开一个二级菜单
        if (_config.spreadOne) {
            var $ul = $container.children('ul');
            $ul.find('li.layui-nav-item').each(function () {
                $(this).on('click', function () {
                    $(this).siblings().removeClass('layui-nav-itemed');
                });
            });
        }
        return _that;
    };
    /**
     * 配置Navbar
     * @param {Object} options
     */
    Navbar.prototype.set = function (options) {
        var that = this;
        that.config.data = undefined;
        $.extend(true, that.config, options);
        return that;
    };
    /**
     * 绑定事件
     * @param {String} events
     * @param {Function} callback
     */
    Navbar.prototype.on = function (events, callback) {
        var that = this;
        var _con = that.config.elem;
        if (typeof (events) !== 'string') {
            common.throwError('Navbar error:事件名配置出错,请参考API文档.');
        }
        var lIndex = events.indexOf('(');
        var eventName = events.substr(0, lIndex);
        var filter = events.substring(lIndex + 1, events.indexOf(')'));
        if (eventName === 'click') {
            if (_con.attr('lay-filter') !== undefined) {
                _con.children('ul').find('li').each(function () {
                    var $this = $(this);
                    if ($this.find('dl').length > 0) {
                        var $dd = $this.find('dd').each(function () {
                            $(this).on('click', function () {
                                var $a = $(this).children('a');
                                var href = $a.data('url');
                                var icon = $a.children('i:first').data('icon');
                                var title = $a.children('cite').text();
                                var data = {
                                    elem: $a,
                                    field: {
                                        href: href,
                                        icon: icon,
                                        title: title
                                    }
                                }
                                callback(data);
                            });
                        });
                    } else {
                        $this.on('click', function () {
                            var $a = $this.children('a');
                            var href = $a.data('url');
                            var icon = $a.children('i:first').data('icon');
                            var title = $a.children('cite').text();
                            var data = {
                                elem: $a,
                                field: {
                                    href: href,
                                    icon: icon,
                                    title: title
                                }
                            }
                            callback(data);
                        });
                    }
                });
            }
        }
    };
    /**
     * 清除缓存
     */
    Navbar.prototype.cleanCached = function () {
        layui.data(cacheName, null);
    };
    /**
     * 获取html字符串
     * @param {Object} data
     */
    function getHtml(data) {
        var ulHtml = '<ul class="layui-nav layui-nav-tree beg-navbar">';
        for (var i = 0; i < data.length; i++) {
            if (data[i].spread) {
                ulHtml += '<li class="layui-nav-item layui-nav-itemed">';
            } else {
                ulHtml += '<li class="layui-nav-item">';
            }
            if (data[i].children !== undefined && data[i].children !== null && data[i].children.length > 0) {
                ulHtml += '<a href="javascript:;">' + data[i].title;
                ulHtml += '<span class="layui-nav-more"></span>';
                ulHtml += '</a>';
                ulHtml += '<dl class="layui-nav-child">';
                //二级菜单
                for (var j = 0; j < data[i].children.length; j++) {
                    //是否有孙子节点
                    if (data[i].children[j].children !== undefined && data[i].children[j].children !== null && data[i].children[j].children.length > 0) {
                        ulHtml += '<dd>';
                        ulHtml += '<a href="javascript:;">' + data[i].children[j].title;
                        ulHtml += '<span class="layui-nav-more"></span>';
                        ulHtml += '</a>';
                        //三级菜单
                        ulHtml += '<dl class="layui-nav-child">';
                            var grandsonNodes = data[i].children[j].children;
                            for (var k = 0; k < grandsonNodes.length; k++) {
                                ulHtml += '<dd>';
                                ulHtml += '<a href="'+ grandsonNodes[k].href +'">' + grandsonNodes[k].title + '</a>';
                                ulHtml += '</dd>';
                            }
                        ulHtml += '</dl>';
                        ulHtml += '</dd>';
                    }else{
                        ulHtml += '<dd>';
                        ulHtml += '<a href="'+data[i].children[j].href+'">' + data[i].children[j].title;
                        ulHtml += '</a>';
                        ulHtml += '</dd>';
                    }
                    //ulHtml += '<dd title="' + data[i].children[j].title + '">'; 
                }
                ulHtml += '</dl>';
            } else {
                var dataUrl = (data[i].href !== undefined && data[i].href !== '') ? 'data-url="' + data[i].href + '"' : '';
                //ulHtml += '<a href="javascript:;" ' + dataUrl + '>';
                ulHtml += '<a href="' + data[i].href + '"' + dataUrl + '>';
                if (data[i].icon !== undefined && data[i].icon !== '') {
                    if (data[i].icon.indexOf('fa-') !== -1) {
                        ulHtml += '<i class="fa ' + data[i].icon + '" aria-hidden="true" data-icon="' + data[i].icon + '"></i>';
                    } else {
                        ulHtml += '<i class="layui-icon" data-icon="' + data[i].icon + '">' + data[i].icon + '</i>';
                    }
                }
                ulHtml += '<cite>' + data[i].title + '</cite>';
                ulHtml += '</a>';
            }
            ulHtml += '</li>';
        }
        ulHtml += '</ul>';
 
        return ulHtml;
    }
 
    var navbar = new Navbar();
 
    exports('navbar', function (options) {
        return navbar.set(options);
    });
});


公共配置common.js

/**
 * common.js
 * @author 御风 <1945199284@qq.com>
 */
layui.define(['layer'], function(exports) {
    "use strict";
 
    var $ = layui.jquery,
        layer = layui.layer;
 
    var common = {
        /**
         * 抛出一个异常错误信息
         * @param {String} msg
         */
        throwError: function(msg) {
            throw new Error(msg);
            return;
        },
        /**
         * 弹出一个错误提示
         * @param {String} msg
         */
        msgError: function(msg) {
            layer.msg(msg, {
                icon: 5
            });
            return;
        }
    };
 
    exports('common', common);
});


3.返回数据json格式

[
  {
    "title": "首页",
    "icon": " ",
    "spread": true,
    "href": ""
  },
  {
    "title": "一级导航",
    "icon": "fa-stop-circle",
    "spread": true,
    "href": "http://www.baidu.com",
    "children": [
      {
        "title": "二级导航",
        "icon": "",
        "href": "lala.html",
        "spread": true,
        "children": [
          {
            "title": "三级导航",
            "icon": " ",
            "href": "button.html"
          },
          {
            "title": "三级导航",
            "icon": " ",
            "href": "buttwswon.html"
          }
        ]
      }
    ]
  },
  {
    "title": "一级导航",
    "icon": "fa-stop-circle",
    "spread": true,
    "href": "http://www.baidu.com"
 
  },
  {
    "title": "一级导航",
    "icon": "fa-stop-circle",
    "spread": true,
    "href": "http://www.baidu.com"
 
  },
  {
    "title": "一级导航",
    "icon": "fa-stop-circle",
    "spread": true,
    "href": "http://www.baidu.com"
 
  }
]


原文链接:https://blog.csdn.net/yufengaotian/article/details/81566148

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的三级菜单代码示例: ```java import javax.swing.*; import java.awt.event.*; public class ThreeLevelMenuExample implements ActionListener { JFrame frame; JMenuBar menuBar; JMenu menuLevel1, menuLevel2, menuLevel3; JMenuItem menuItem1, menuItem2, menuItem3, menuItem4, menuItem5, menuItem6, menuItem7, menuItem8; public ThreeLevelMenuExample() { frame = new JFrame("Three Level Menu Example"); menuBar = new JMenuBar(); menuLevel1 = new JMenu("Level 1"); menuLevel2 = new JMenu("Level 2"); menuLevel3 = new JMenu("Level 3"); menuItem1 = new JMenuItem("Item 1"); menuItem2 = new JMenuItem("Item 2"); menuItem3 = new JMenuItem("Item 3"); menuItem4 = new JMenuItem("Item 4"); menuItem5 = new JMenuItem("Item 5"); menuItem6 = new JMenuItem("Item 6"); menuItem7 = new JMenuItem("Item 7"); menuItem8 = new JMenuItem("Item 8"); menuItem1.addActionListener(this); menuItem2.addActionListener(this); menuItem3.addActionListener(this); menuItem4.addActionListener(this); menuItem5.addActionListener(this); menuItem6.addActionListener(this); menuItem7.addActionListener(this); menuItem8.addActionListener(this); menuLevel3.add(menuItem1); menuLevel3.add(menuItem2); menuLevel3.add(menuItem3); menuLevel3.add(menuItem4); menuLevel2.add(menuItem5); menuLevel2.add(menuItem6); menuLevel2.add(menuLevel3); menuLevel1.add(menuItem7); menuLevel1.add(menuItem8); menuLevel1.add(menuLevel2); menuBar.add(menuLevel1); frame.setJMenuBar(menuBar); frame.setSize(300, 300); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { new ThreeLevelMenuExample(); } public void actionPerformed(ActionEvent e) { // Handle menu item events here } } ``` 该示例创建了一个包含三个级别的菜单,其中第三个级别作为第二个级别的子菜单,第二个级别作为第一个级别的子菜单。可以根据需要添加更多的菜单菜单项,并在 `actionPerformed()` 方法中处理单击事件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值