瀏覽代碼

优化权限规则列表显示

(cherry picked from commit 2c66c67d7152a8089721c7465f0da9715d03eb13)
Karson 1 年之前
父節點
當前提交
1066b4108b

+ 1 - 1
application/admin/controller/auth/Rule.php

@@ -36,7 +36,7 @@ class Rule extends Backend
             $v['title'] = __($v['title']);
         }
         unset($v);
-        Tree::instance()->init($ruleList);
+        Tree::instance()->init($ruleList)->icon = ['    ', '    ', '    '];
         $this->rulelist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
         $ruledata = [0 => __('None')];
         foreach ($this->rulelist as $k => &$v) {

+ 1 - 1
application/admin/controller/user/Rule.php

@@ -32,7 +32,7 @@ class Rule extends Backend
             $v['remark'] = __($v['remark']);
         }
         unset($v);
-        Tree::instance()->init($ruleList);
+        Tree::instance()->init($ruleList)->icon = ['    ', '    ', '    '];
         $this->rulelist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
         $ruledata = [0 => __('None')];
         foreach ($this->rulelist as $k => &$v) {

+ 4 - 1
application/admin/lang/zh-cn/user/rule.php

@@ -12,5 +12,8 @@ return [
     'Menu tips'       => '规则任意,请不可重复,仅做层级显示,无需匹配控制器和方法',
     'Node tips'       => '模块/控制器/方法名',
     'Weigh'           => '权重',
-    'Status'          => '状态'
+    'Status'          => '状态',
+    'Toggle all'      => '显示全部',
+    'Toggle menu visible' => '点击切换菜单显示',
+    'Toggle sub menu'     => '点击切换子菜单',
 ];

+ 1 - 0
application/admin/view/user/rule/index.html

@@ -14,6 +14,7 @@
                                 <li><a class="btn btn-link btn-multi btn-disabled disabled" href="javascript:;" data-params="status=hidden"><i class="fa fa-eye-slash"></i> {:__('Set to hidden')}</a></li>
                             </ul>
                         </div>
+                        <a href="javascript:;" class="btn btn-danger btn-toggle-all"><i class="fa fa-plus"></i> {:__('Toggle all')}</a>
                     </div>
                     <table id="table" class="table table-striped table-bordered table-hover table-nowrap"
                            data-operate-edit="{:$auth->check('user/rule/edit')}"

+ 28 - 23
public/assets/js/backend/auth/rule.js

@@ -38,12 +38,6 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function
                             formatter: Table.api.formatter.toggle
                         },
                         {
-                            field: 'id',
-                            title: '<a href="javascript:;" class="btn btn-success btn-xs btn-toggle" style="border-top:none;"><i class="fa fa-chevron-up"></i></a>',
-                            operate: false,
-                            formatter: Controller.api.formatter.subnode
-                        },
-                        {
                             field: 'operate',
                             title: __('Operate'),
                             table: table,
@@ -56,10 +50,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function
                 search: false,
                 commonSearch: false,
                 rowAttributes: function (row, index) {
-                    if (this.totalRows > 500) {
-                        return row.pid == 0 ? {} : {style: "display:none"};
-                    }
-                    return row.haschild == 1 || row.ismenu == 1 ? {} : {style: "display:none"};
+                    return row.pid == 0 ? {} : {style: "display:none"};
                 }
             });
 
@@ -104,13 +95,27 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function
             //显示隐藏子节点
             $(document).on("click", ".btn-node-sub", function (e) {
                 var status = $(this).data("shown") ? true : false;
-                $("a.btn[data-pid='" + $(this).data("id") + "']").each(function () {
+                $("a[data-pid='" + $(this).data("id") + "']").each(function () {
                     $(this).closest("tr").toggle(!status);
                 });
+                if (status) {
+                    $("a[data-pid='" + $(this).data("id") + "']").trigger("collapse");
+                }
                 $(this).data("shown", !status);
+                $("i", this).toggleClass("fa-caret-down").toggleClass("fa-caret-right");
                 return false;
             });
 
+            //隐藏子节点
+            $(document).on("collapse", ".btn-node-sub", function () {
+                if ($("i", this).length > 0) {
+                    $("a[data-pid='" + $(this).data("id") + "']").trigger("collapse");
+                }
+                $("i", this).removeClass("fa-caret-down").addClass("fa-caret-right");
+                $(this).data("shown", false);
+                $(this).closest("tr").toggle(false);
+            });
+
             //批量删除后的回调
             $(".toolbar > .btn-del,.toolbar .btn-more~ul>li>a").data("success", function (e) {
                 Fast.api.refreshmenu();
@@ -118,12 +123,11 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function
 
             //展开隐藏一级
             $(document.body).on("click", ".btn-toggle", function (e) {
-                $("a.btn[data-id][data-pid][data-pid!=0].disabled").closest("tr").hide();
+                $("a[data-id][data-pid][data-pid!=0].disabled").closest("tr").hide();
                 var that = this;
                 var show = $("i", that).hasClass("fa-chevron-down");
-                $("i", that).toggleClass("fa-chevron-down", !show);
-                $("i", that).toggleClass("fa-chevron-up", show);
-                $("a.btn[data-id][data-pid][data-pid!=0]").not('.disabled').closest("tr").toggle(show);
+                $("i", that).toggleClass("fa-chevron-down", !show).toggleClass("fa-chevron-up", show);
+                $("a[data-id][data-pid][data-pid!=0]").not('.disabled').closest("tr").toggle(show);
                 $(".btn-node-sub[data-pid=0]").data("shown", show);
             });
 
@@ -131,10 +135,10 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function
             $(document.body).on("click", ".btn-toggle-all", function (e) {
                 var that = this;
                 var show = $("i", that).hasClass("fa-plus");
-                $("i", that).toggleClass("fa-plus", !show);
-                $("i", that).toggleClass("fa-minus", show);
-                $(".btn-node-sub.disabled").closest("tr").toggle(show);
+                $("i", that).toggleClass("fa-plus", !show).toggleClass("fa-minus", show);
+                $(".btn-node-sub:not([data-pid=0])").closest("tr").toggle(show);
                 $(".btn-node-sub").data("shown", show);
+                $(".btn-node-sub > i").toggleClass("fa-caret-down", show).toggleClass("fa-caret-right", !show);
             });
         },
         add: function () {
@@ -147,17 +151,18 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function
             formatter: {
                 title: function (value, row, index) {
                     value = value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;');
-                    return !row.ismenu || row.status == 'hidden' ? "<span class='text-muted'>" + value + "</span>" : value;
+                    var caret = row.haschild == 1 || row.ismenu == 1 ? '<i class="fa fa-caret-right"></i>' : '';
+                    value = value.indexOf("&nbsp;") > -1 ? value.replace(/(.*)&nbsp;/, "$1" + caret) : caret + value;
+
+                    value = !row.ismenu || row.status == 'hidden' ? "<span class='text-muted'>" + value + "</span>" : value;
+                    return '<a href="javascript:;" data-toggle="tooltip" title="' + __('Toggle sub menu') + '" data-id="' + row.id + '" data-pid="' + row.pid + '" class="'
+                        + (row.haschild == 1 || row.ismenu == 1 ? 'text-primary' : 'disabled') + ' btn-node-sub">' + value + '</a>';
                 },
                 name: function (value, row, index) {
                     return !row.ismenu || row.status == 'hidden' ? "<span class='text-muted'>" + value + "</span>" : value;
                 },
                 icon: function (value, row, index) {
                     return '<span class="' + (!row.ismenu || row.status == 'hidden' ? 'text-muted' : '') + '"><i class="' + value + '"></i></span>';
-                },
-                subnode: function (value, row, index) {
-                    return '<a href="javascript:;" data-toggle="tooltip" title="' + __('Toggle sub menu') + '" data-id="' + row.id + '" data-pid="' + row.pid + '" class="btn btn-xs '
-                        + (row.haschild == 1 || row.ismenu == 1 ? 'btn-success' : 'btn-default disabled') + ' btn-node-sub"><i class="fa fa-sitemap"></i></a>';
                 }
             },
             bindevent: function () {

+ 61 - 5
public/assets/js/backend/user/rule.js

@@ -21,18 +21,16 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                 url: $.fn.bootstrapTable.defaults.extend.index_url,
                 pk: 'id',
                 sortName: 'weigh',
+                escape: false,
                 columns: [
                     [
                         {checkbox: true},
                         {field: 'id', title: __('Id')},
                         {field: 'pid', title: __('Pid'), visible: false},
-                        {field: 'title', title: __('Title'), align: 'left', formatter:function (value, row, index) {
-                                return value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;');
-                            }
-                        },
+                        {field: 'title', title: __('Title'), align: 'left', formatter: Controller.api.formatter.title},
                         {field: 'name', title: __('Name'), align: 'left'},
                         {field: 'remark', title: __('Remark')},
-                        {field: 'ismenu', title: __('Ismenu'), formatter: Table.api.formatter.toggle},
+                        // {field: 'ismenu', title: __('Ismenu'), formatter: Table.api.formatter.toggle},
                         {field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true, visible: false},
                         {field: 'updatetime', title: __('Updatetime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true, visible: false},
                         {field: 'weigh', title: __('Weigh')},
@@ -43,10 +41,57 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                 pagination: false,
                 search: false,
                 commonSearch: false,
+                rowAttributes: function (row, index) {
+                    return row.pid == 0 ? {} : {style: "display:none"};
+                }
             });
 
             // 为表格绑定事件
             Table.api.bindevent(table);
+
+            //显示隐藏子节点
+            $(document).on("click", ".btn-node-sub", function (e) {
+                var status = $(this).data("shown") ? true : false;
+                $("a[data-pid='" + $(this).data("id") + "']").each(function () {
+                    $(this).closest("tr").toggle(!status);
+                });
+                if (status) {
+                    $("a[data-pid='" + $(this).data("id") + "']").trigger("collapse");
+                }
+                $(this).data("shown", !status);
+                $("i", this).toggleClass("fa-caret-down").toggleClass("fa-caret-right");
+                return false;
+            });
+
+            //隐藏子节点
+            $(document).on("collapse", ".btn-node-sub", function () {
+                if ($("i", this).length > 0) {
+                    $("a[data-pid='" + $(this).data("id") + "']").trigger("collapse");
+                }
+                $("i", this).removeClass("fa-caret-down").addClass("fa-caret-right");
+                $(this).data("shown", false);
+                $(this).closest("tr").toggle(false);
+            });
+
+            //展开隐藏一级
+            $(document.body).on("click", ".btn-toggle", function (e) {
+                $("a[data-id][data-pid][data-pid!=0].disabled").closest("tr").hide();
+                var that = this;
+                var show = $("i", that).hasClass("fa-chevron-down");
+                $("i", that).toggleClass("fa-chevron-down", !show).toggleClass("fa-chevron-up", show);
+                $("a[data-id][data-pid][data-pid!=0]").not('.disabled').closest("tr").toggle(show);
+                $(".btn-node-sub[data-pid=0]").data("shown", show);
+            });
+
+            //展开隐藏全部
+            $(document.body).on("click", ".btn-toggle-all", function (e) {
+                var that = this;
+                var show = $("i", that).hasClass("fa-plus");
+                $("i", that).toggleClass("fa-plus", !show).toggleClass("fa-minus", show);
+                $(".btn-node-sub:not([data-pid=0])").closest("tr").toggle(show);
+                $(".btn-node-sub").data("shown", show);
+                $(".btn-node-sub > i").toggleClass("fa-caret-down", show).toggleClass("fa-caret-right", !show);
+            });
         },
         add: function () {
             Controller.api.bindevent();
@@ -55,6 +100,17 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
             Controller.api.bindevent();
         },
         api: {
+            formatter: {
+                title: function (value, row, index) {
+                    value = value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;');
+                    var caret = row.haschild == 1 || row.ismenu == 1 ? '<i class="fa fa-caret-right"></i>' : '';
+                    value = value.indexOf("&nbsp;") > -1 ? value.replace(/(.*)&nbsp;/, "$1" + caret) : caret + value;
+
+                    value = !row.ismenu || row.status == 'hidden' ? "<span class='text-muted'>" + value + "</span>" : value;
+                    return '<a href="javascript:;" data-toggle="tooltip" title="' + __('Toggle sub menu') + '" data-id="' + row.id + '" data-pid="' + row.pid + '" class="'
+                        + (row.haschild == 1 || row.ismenu == 1 ? 'text-primary' : 'disabled') + ' btn-node-sub">' + value + '</a>';
+                }
+            },
             bindevent: function () {
                 $(document).on('click', "input[name='row[ismenu]']", function () {
                     var name = $("input[name='row[name]']");