<bdo id='Aq3xo'></bdo><ul id='Aq3xo'></ul>
    <legend id='Aq3xo'><style id='Aq3xo'><dir id='Aq3xo'><q id='Aq3xo'></q></dir></style></legend>
    <tfoot id='Aq3xo'></tfoot>
  1. <i id='Aq3xo'><tr id='Aq3xo'><dt id='Aq3xo'><q id='Aq3xo'><span id='Aq3xo'><b id='Aq3xo'><form id='Aq3xo'><ins id='Aq3xo'></ins><ul id='Aq3xo'></ul><sub id='Aq3xo'></sub></form><legend id='Aq3xo'></legend><bdo id='Aq3xo'><pre id='Aq3xo'><center id='Aq3xo'></center></pre></bdo></b><th id='Aq3xo'></th></span></q></dt></tr></i><div id='Aq3xo'><tfoot id='Aq3xo'></tfoot><dl id='Aq3xo'><fieldset id='Aq3xo'></fieldset></dl></div>
  2. <small id='Aq3xo'></small><noframes id='Aq3xo'>

    1. layui使用tree组件实现树形下拉菜单

      时间:2024-10-27
      <legend id='cAUpX'><style id='cAUpX'><dir id='cAUpX'><q id='cAUpX'></q></dir></style></legend>
    2. <tfoot id='cAUpX'></tfoot>

      • <bdo id='cAUpX'></bdo><ul id='cAUpX'></ul>

          <i id='cAUpX'><tr id='cAUpX'><dt id='cAUpX'><q id='cAUpX'><span id='cAUpX'><b id='cAUpX'><form id='cAUpX'><ins id='cAUpX'></ins><ul id='cAUpX'></ul><sub id='cAUpX'></sub></form><legend id='cAUpX'></legend><bdo id='cAUpX'><pre id='cAUpX'><center id='cAUpX'></center></pre></bdo></b><th id='cAUpX'></th></span></q></dt></tr></i><div id='cAUpX'><tfoot id='cAUpX'></tfoot><dl id='cAUpX'><fieldset id='cAUpX'></fieldset></dl></div>

          <small id='cAUpX'></small><noframes id='cAUpX'>

              <tbody id='cAUpX'></tbody>

                layui使用tree组件实现树形下拉菜单,下面是具体代码:
                html代码:
                  <link rel="stylesheet" href="~/layui/css/layui.css" media="all">
                    @*<link rel="stylesheet" type="text/css" href="~/layui/font/">*@
                    <style type="text/css">
                        .treeSelect .layui-select-title span {
                            line-height: 38px;
                        }
                
                        .layui-form-select dl {
                            padding: 0;
                        }
                    </style>
                <form class="layui-form layui-form-pane" style="margin: 50px;">
                        <div class="layui-form-item">
                            <label class="layui-form-label">菜单栏目</label>
                            <div class="layui-input-block" style="width: 300px;">
                                <div class="layui-unselect layui-form-select treeSelect">
                                    <div class="layui-select-title">
                                        <span class="layui-input layui-unselect" id="treeclass">选择栏目</span>
                                        <input type="hidden" name="selectID" value="12">
                                        <i class="layui-edge"></i>
                                    </div>
                                    @*<div id="meuntree" class="demo-tree-more"></div>*@
                                    <dl class="layui-anim layui-anim-upbit">
                                        <dd>
                                            <ul id="meuntree"></ul>
                                        </dd>
                                    </dl>
                                </div>
                            </div>
                        </div>
                    </form>
                
                JS代码部分:
                <script src="~/layui/layui.js"></script>
                <script type="text/javascript">
                    layui.config({
                        base: '~/layui/' //静态资源所在路径
                    });
                    layui.use(['layer', 'tree', 'form'], function () {
                        var $ = layui.jquery,
                            layer = layui.layer,
                            form = layui.form,
                            tree = layui.tree;
                        //初始化权限
                        function init_auth() {
                            console.log("权限菜单初始化");
                            var data = [];
                            $.ajax({
                                url: "http://localhost:8089/api/SysMenu/GetSysMenuList",    //后台数据请求地址
                                type: "get",
                                async: false,
                                success: function (resut) {
                                    data = resut;
                                    var formatdata = [];
                                    for (var i in data) {     // pId为0时表示为根节点
                                        if (data[i].pid == '-1') {
                                            var tempObject = {};
                                            tempObject.title = data[i].name;
                                            tempObject.id = data[i].id;
                                            tempObject.children = getChildren(tempObject.id);
                                            formatdata.push(tempObject);
                                        }
                                    }
                                    function getChildren(id) {    //递归体  即对每条data逐条递归找children
                                        var tempArray = [];
                                        for (var i in data) {
                                            if (data[i].pid == id) {
                                                var tempChild = {};
                                                tempChild.title = data[i].name;
                                                tempChild.id = data[i].id;
                                                if (selectChildren(data[i].id)) {   //若存在子节点,继续递归;否则为叶节点,停止递归
                                                    tempChild.children = getChildren(data[i].id);
                                                }
                                                tempArray.push(tempChild);
                                            }
                                        }
                                        return tempArray;
                                    }
                                    function selectChildren(id) {   // 是否存在子节点
                                        for (var i in data) {
                                            if (data[i].pid == id) {
                                                return true;
                                            }
                                        }
                                        return false;
                                    }
                
                                    //转成符合要求的格式
                                    console.log(formatdata);
                                    //基本演示
                                    tree.render({
                                        elem: '#meuntree'
                                        , data: formatdata
                                        
                                        , id: 'authlist'
                                        , isJump: true //是否允许点击节点时弹出新窗口跳转
                                        , click: function (obj) {
                                            var data = obj.data;  //获取当前点击的节点数据
                                            //layer.msg('状态:' + obj.state + '<br>节点数据:' + JSON.stringify(data));
                                            var $select = $($(this)[0].elem).parents(".layui-form-select");
                                            $select.removeClass("layui-form-selected").find(".layui-select-title span").html(obj.data.title).end().find("input:hidden[name='selectID']").val(obj.data.id);
                
                                            var selectId = $("input[name='selectID']").val();
                                            layer.msg("选择了id=" + selectId + "的数据" + '<br>节点数据:' + obj.data.title, {
                                                icon: 1,
                                                time: 5000,
                                                offset: '15px'
                                            });
                                        }
                                    });
                                }
                            });
                            //return data;
                        };
                
                    
                        init_auth();
                        // 默认选中
                        var checkNode = {
                            id: 4
                            ,title: '用户管理'
                        }
                        var $select = $(".layui-form-select");
                        $select.removeClass("layui-form-selected").find(".layui-select-title span").html(checkNode.title).end().find("input:hidden[name='selectID']").val(checkNode.id);
                        form.render();
                
                
                        $(".treeSelect").on("click", ".layui-select-title", function (e) {
                            $(".layui-form-select").not($(this).parents(".layui-form-select")).removeClass("layui-form-selected");
                            $(this).parents(".treeSelect").toggleClass("layui-form-selected");
                            layui.stope(e);
                        }).on("click", "dl i", function (e) {
                            layui.stope(e);
                        });
                        $(document).on("click", function (e) {
                            $(".layui-form-select").removeClass("layui-form-selected");
                        });
                
                    });
                </script>
                
                最终实现效果:
                上一篇:改造layui tree组件支持树的关键字搜索操作 下一篇:没有了

                相关文章

                • <bdo id='vii5J'></bdo><ul id='vii5J'></ul>
              • <small id='vii5J'></small><noframes id='vii5J'>

                1. <legend id='vii5J'><style id='vii5J'><dir id='vii5J'><q id='vii5J'></q></dir></style></legend>
                  <tfoot id='vii5J'></tfoot>

                  1. <i id='vii5J'><tr id='vii5J'><dt id='vii5J'><q id='vii5J'><span id='vii5J'><b id='vii5J'><form id='vii5J'><ins id='vii5J'></ins><ul id='vii5J'></ul><sub id='vii5J'></sub></form><legend id='vii5J'></legend><bdo id='vii5J'><pre id='vii5J'><center id='vii5J'></center></pre></bdo></b><th id='vii5J'></th></span></q></dt></tr></i><div id='vii5J'><tfoot id='vii5J'></tfoot><dl id='vii5J'><fieldset id='vii5J'></fieldset></dl></div>