FastAdmin 权限判断,自定义table操作按钮,列表按钮,隐藏按钮,隐藏自定义按钮

发布时间 2023-04-24 10:04:17作者: 晨光浅夏

一、JS自定义按钮 + 按钮隐藏

1、FastAdmin JS自定义按钮

效果图:

 自定义video视频按钮

{
    field: 'operate',
    title: __('Operate'),
    table: table,
    events: Table.api.events.operate,
    buttons: [
        {
            name: 'video',
            text: __('视频'),
            icon: 'fa fa-list',
            classname: 'btn btn-xs btn-primary  btn-addtabs',
            url: 'major/chapter/index/subject_id/{ids}',
        }
    ],
    formatter: Table.api.formatter.operate
}

2、隐藏video视频按钮

效果图:

隐藏自定义按钮

{
    field: 'operate',
    title: __('Operate'),
    table: table,
    events: Table.api.events.operate,
    buttons: [
        {
            name: 'video',
            text: __('视频'),
            icon: 'fa fa-list',
            classname: 'btn btn-xs btn-primary  btn-addtabs',
            url: 'major/chapter/index/subject_id/{ids}',
        }
    ],
    formatter: function (value, row, index) { //隐藏自定义的视频按钮
    var that = $.extend({}, this);
    var table = $(that.table).clone(true);
    $(table).data("operate-video", null);  //隐藏按钮操作 video为自定义按钮的name 
    that.table = table;
    return Table.api.formatter.operate.call(that, value, row, index);
    }
}

这样自定义按钮的显示和隐藏就可以控制了,当然也可以通过这种方式来隐藏FastAdmin生成的按钮,但是不推荐这样做,FastAdmin生成的按钮可以通过权限控制来隐藏。

二、根据自定义按钮的权限,控制显示与隐藏

1、首先后台判断对应权限

在后台初始化方法中,判断是否有按钮的权限,并通过assignconfig()来保存,然后在JS中判断权限。

$this->auth->check(''); 判断是否具有对应路径的权限

public function _initialize()
{
    parent::_initialize();
    $this->assignconfig('chapter', $this->auth->check('major/chapter/index')); //为了JS能获取到,同时判读权限 判断是否有major/chapter/index权限
}

2、对应JS中

通过Config.chapter  来获取后台的权限状态,然后根据状态显示与隐藏按钮。

{
    field: 'operate',
    title: __('Operate'),
    table: table,
    events: Table.api.events.operate,
    buttons: [
        {
            name: 'video',
            text: __('视频'),
            icon: 'fa fa-list',
            classname: 'btn btn-xs btn-primary  btn-addtabs',
            url: 'major/chapter/index/subject_id/{ids}',
        }
    ],
    formatter: function (value, row, index) { //隐藏自定义的视频按钮
    var that = $.extend({}, this);
    var table = $(that.table).clone(true);
    //权限判断
    if(Config.chapter != true){  //通过Config.chapter 获取后台存的chapter
        console.log('没有视频权限');
        $(table).data("operate-video", null); 
        that.table = table;
    }else{
        console.log('有视频权限');
    }
    return Table.api.formatter.operate.call(that, value, row, index);
    }
}

最终效果图: