原文 https://ask.fastadmin.net/article/38468.html

一级菜单和二级菜单动态刷新角标方法

  1. 首先在/application/admin/controller/Index.php文件里面的index方法把角标加上去

    $num1 = Db::name('love_user_avatar')->where(['status'=>0])->count();
    $num2 = Db::name('love_report')->where(['status'=>0])->count();
    //左侧菜单
    list($menulist, $navlist, $fixedmenu, $referermenu) = $this->auth->getSidebar([
         'audit'            => [$num1+$num2, 'red', 'badge'], //审核管理
         'love/user/avatar' => [$num1, 'red', 'badge'], //头像审核
         'love/report'      => [$num2, 'red', 'badge'], //举报审核
    ], $this->view->site['fixedpage']);
    
  2. js里面定义一个变量change,监听删除、审核按钮点击操作,根据change来判断是否需要刷新菜单。

     var change = 0;
     //删除后刷新菜单角标
     $('.btn-del').click(function(){
         change = 1;
     })
     //审核后刷新菜单角标
     $('.btn-multi').click(function(){
         change = 1;
     })
     var table = $("#table");
     // 初始化表格
     table.bootstrapTable({
         url: $.fn.bootstrapTable.defaults.extend.index_url,
         pk: 'id',
         sortName: 'id',
         search: false,
         columns: [
             [
                 {checkbox: true},
                 {field: 'id', title: __('Id'), sortable: true},
                 {field: 'user_id', title: __('User_id')},
                 {field: 'avatar', title: __('Avatar'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                 {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"-1":__('Status -1')}, formatter: Table.api.formatter.status},
                 {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime, sortable: true},
                 {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime, sortable: true},
                 {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
             ]
         ],
         onLoadSuccess: function (data) {
             if(change == 1){
                 Backend.api.refreshmenu(); //刷新菜单
                 change = 0;
             }
             // Backend.api.sidebar({
             //     'audit': [data.total, 'red', 'badge'],  //无效,一级菜单的角标刷新不了
             //     'love/user/avatar': [data.total, 'red', 'badge']  //有效,可以刷新二级菜单的角标
             // });
         }
     });
    

效果

af2423ee7e114054fe234441abf68d43.gif