我试图为那些想为我的网站发送反馈信息的用户制作反馈页面,并且我正在使用网格视图来显示反馈列表。这是我做的网格视图代码。
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'layout'=>"{items}{pager}",
'tableOptions' => ['class' => 'table table-bordered table-hover'],
'showFooter'=>false,
'showHeader' => false,
'pager' => [
'firstPageLabel' => 'First',
'lastPageLabel' => 'Last',
],
'columns' => [
[ 'attribute' => 'iduser.photo',
'format' => 'html',
'value'=> function($data) { return Html::img($data->imageurl) . " <p class='feedback-username'>" . $data->username . "</p>"; },
'contentOptions'=>['style'=>'max-width: 10px; max-height: 10px'],
],
[ 'attribute' => 'KOMENTAR',
'format' => 'raw',
'value' => function($model) { return $model->KOMENTAR ."<br><p class='feedback-date'>". $model->TANGGAL ."</p>";},
],
[ 'class' => 'yii\grid\ActionColumn',
'contentOptions'=>['style'=>'width: 5px;'],
'template' => '{update} {delete}'
],
],
]); ?> 在我的网格视图中,它在发送的每一个反馈中都显示了动作列。但是我想要的是,action列应该只显示用户登录发送的反馈信息。那么我应该在哪里定制它呢?
发布于 2015-08-27 16:16:48
如果您只想在用户登录时显示ActionColumn,您可以这样做:
[
'class' => 'yii\grid\ActionColumn',
'template' => '{update} {delete}',
'contentOptions' => ['style' => 'width:34px; font-size:18px;']
'visible' => !Yii::$app->user->isGuest,
],如果您只想为用户创建的反馈显示ActionColumn,那么您必须这样做:
[
'format' => 'html',
'value' => function($model) {
if($model->user_id == Yii::$app->user->identity->id) {
return Html::a('<i class="glyphicon glyphicon-pencil"></i>', ['update', 'id' => $model->id])
.' '.Html::a('<i class="glyphicon glyphicon-trash"></i>', ['delete', 'id' => $model->id], [
'data' => ['confirm' => 'Do you really want to delete this element?','method' => 'post']
]
);
}
return '';
},
],发布于 2015-08-27 06:39:54
尝试以这种方式设置actionColumn的可见optioon为true或false
// for guest
if(Yii::$app->user->isGuest)
{
$actionColumn = [ 'class' => 'yii\grid\ActionColumn',
'template' => '{update} {delete}',
'contentOptions' => ['style' => 'width:34px; font-size:18px;']
'visible' => true,
],
}
// for users
else
{
$actionColumn = [ 'class' => 'yii\grid\ActionColumn',
'template' => '{update} {delete}',
'contentOptions' => ['style' => 'width:34px; font-size:18px;']
'visible' => false,
],
}
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'layout'=>"{items}{pager}",
'tableOptions' => ['class' => 'table table-bordered table-hover'],
'showFooter'=>false,
'showHeader' => false,
'pager' => [
'firstPageLabel' => 'First',
'lastPageLabel' => 'Last',
],
'columns' => [
[ 'attribute' => 'iduser.photo',
'format' => 'html',
'value'=> function($data) { return Html::img($data->imageurl) . " <p class='feedback-username'>" . $data->username . "</p>"; },
'contentOptions'=>['style'=>'max-width: 10px; max-height: 10px'],
],
[ 'attribute' => 'KOMENTAR',
'format' => 'raw',
'value' => function($model) { return $model->KOMENTAR ."<br><p class='feedback-date'>". $model->TANGGAL ."</p>";},
],
$actionColumn
],
]); ?> 发布于 2015-08-27 04:05:23
只需尝试:
<?php
// non logged in users
if(Yii::$app->user->isGuest)
{
$actionColumn = [];
}
// logged in users
else
{
$actionColumn = [ 'class' => 'yii\grid\ActionColumn',
'contentOptions'=>['style'=>'width: 5px;'],
'template' => '{update} {delete}'
];
}
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'layout'=>"{items}{pager}",
'tableOptions' => ['class' => 'table table-bordered table-hover'],
'showFooter'=>false,
'showHeader' => false,
'pager' => [
'firstPageLabel' => 'First',
'lastPageLabel' => 'Last',
],
'columns' => [
[ 'attribute' => 'iduser.photo',
'format' => 'html',
'value'=> function($data) { return Html::img($data->imageurl) . " <p class='feedback-username'>" . $data->username . "</p>"; },
'contentOptions'=>['style'=>'max-width: 10px; max-height: 10px'],
],
[ 'attribute' => 'KOMENTAR',
'format' => 'raw',
'value' => function($model) { return $model->KOMENTAR ."<br><p class='feedback-date'>". $model->TANGGAL ."</p>";},
],
$actionColumn
],
]); ?> https://stackoverflow.com/questions/32239383
复制相似问题