如何使 GridView 按钮更新和删除仅对管理员可见? [英] How to make that GridView buttons update and delete just only visible for admins?
问题描述
我是 Yii2 的新手,我有 3 种用户权限
:
I am new to Yii2 and I have 3 kind of user rights
:
Admin
、moderator
和 user
.我有我的 GridView
,我不想向用户显示 Update
和 Delete
按钮,只显示 GridView代码>.我该怎么做?
Admin
, moderator
and user
. I have my GridView
and I don't want to show for the user the Update
and Delete
buttons, just only the GridView
. How should I do that?
这是我的actionCreate
,有一个输入表单:
Here is my actionCreate
, there is an input form:
public function actionCreate()
{
$model = new Project();
$model->scenario = Project::SCENARIO_CREATE;
if ($model->load(Yii::$app->request->post())) {
if ($model->save()) {
Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Skelbimas sėkmingai pridėtas!'));
return $this->redirect(['index']);
}
}
return $this->render('create', [
'model' => $model,
]);
}
我尝试根据此搜索信息,但找不到有用的信息.感谢您提供任何帮助或信息.
I've tried to search the information according to this, but couldn't find useful ones. Thanks for any help or information.
推荐答案
要实现这一点,您必须使用属性 $visibleButtons 你的 ActionColum 类.
To accomplish this, you have to use the property $visibleButtons of you ActionColum class.
所以:
'visibleButtons' = [
'update' => Yii::$app->user->can('update'), // or whatever condition
'delete' => Yii::$app->user->can('update')
]
等等.visibleButtons 数组上的每个 Key 都是按钮的名称.
and so on. Each Key on the visibleButtons array is the name of the button.
这篇关于如何使 GridView 按钮更新和删除仅对管理员可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!