bambinou
(Benoitrivaux)
April 15, 2015, 7:57pm
1
Hello,
I would like to know if there is a way to remove the buttons in the grid(update, delete) based on the access rules set in the controller as:
[php]
'access' => [
'class' => AccessControl::className(),
'only' => ['create', 'read', 'update', 'delete'],
'rules' => [
[
'allow' => true,
'actions' => ['create', 'read', 'update'],
'roles' => ['@'],
],
],
],
/php]
Also if you have another example based on the user role please.
I found a few examples on the internet but still do not understand, if you have an easy way of explaining it , I would be grateful!
Thank you so much.
Ben
epulgaron
(Jokercrazy05544)
April 15, 2015, 8:05pm
2
well i did this:
in vendor\yiisoft\yii2\grid\actioncolumn
protected function initDefaultButtons()
{ if(\Yii::$app->user->can('admin'))
{
if (!isset($this->buttons['view'])) {
$this->buttons['view'] = function ($url, $model, $key) {
return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, [
'title' => Yii::t('yii', 'View'),
'data-pjax' => '0',
]);
};
}
if (!isset($this->buttons['update'])) {
$this->buttons['update'] = function ($url, $model, $key) {
return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, [
'title' => Yii::t('yii', 'Update'),
'data-pjax' => '0',
]);
};
}
if (!isset($this->buttons['delete'])) {
$this->buttons['delete'] = function ($url, $model, $key) {
return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, [
'title' => Yii::t('yii', 'Delete'),
'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
'data-method' => 'post',
'data-pjax' => '0',
]);
};
}
}
else {
if (!isset($this->buttons['view'])) {
$this->buttons['view'] = function ($url, $model, $key) {
return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, [
'title' => Yii::t('yii', 'View'),
'data-pjax' => '0',
]);
};
}
}
}
I don,t if this the best way but it works for me, an this is global too
bambinou:
Hello,
I would like to know if there is a way to remove the buttons in the grid(update, delete) based on the access rules set in the controller as:
[php]
'access' => [
'class' => AccessControl::className(),
'only' => ['create', 'read', 'update', 'delete'],
'rules' => [
[
'allow' => true,
'actions' => ['create', 'read', 'update'],
'roles' => ['@'],
],
],
],
/php]
Also if you have another example based on the user role please.
I found a few examples on the internet but still do not understand, if you have an easy way of explaining it , I would be grateful!
Thank you so much.
Ben
rahif
(Rahif)
April 15, 2015, 9:00pm
3
In general you have to extend a class not overwrite.
Create a file wherever you want, for example in components/grid/ActionColumn.php
<?php
namespace app\components\grid;
use yii\helpers\Url;
class ActionColumn extends \yii\grid\ActionColumn {
//here you can overwrite the functions of the parent class
}
and in you gridview call to this actionColumn
...
'class' => 'app\components\grid\ActionColumn',
...
Vendor folder can be changed by composer update and youll lose everithing there.
mmbalaa
(Mmbalasundaram)
April 16, 2015, 5:32am
4
Better customize the button in gridview and actions in controller based on roles
Maybe you could also try to use the "visible" attribute of the ActionColumn?
Example:
[
'class' => 'yii\grid\ActionColumn',
'visible' => Yii::$app->user->isGuest ? false : true,
],
Regards
bambinou
(Benoitrivaux)
April 16, 2015, 6:41pm
6
Hello all,
I have found a very way of doing this:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'title',
'author',
['class' => 'yii\grid\ActionColumn', 'template' => '{view} {delete} {update} '], <--------remove the one you don't want.
],
]); ?>
This will make it global of course.
It works very well!
Thanks again,
Ben