仅为用户登录Gridview Yii2显示actionCoumn

时间:2015-08-27 01:45:21

标签: php yii2 yii2-advanced-app yii2-basic-app

我正在尝试为想要为我的网络发送反馈的用户制作反馈页面,我正在使用gridview来显示反馈列表。这是我制作的gridview代码。

<a href="before">link</a>
<a href="before">link</a>
<script>
    var links = document.getElementsByTagName('a');
    for(var i = 0; i < links.length; i++) {
        var href = links[i].getAttribute('href');
        href = href.replace('before', '#');
        links[i].setAttribute('href', href);
    }
</script>

在我的gridview中,它会在发送的每个反馈中显示操作列。但我想要的是,动作列应该只显示仅由用户登录发送的反馈。那我应该在哪里定制呢?

3 个答案:

答案 0 :(得分:2)

尝试这种方式为actionColumn

设置true或false可见optioon
   //  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
    ],
 ]); ?> 

答案 1 :(得分:0)

只需尝试:

<?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
    ],
]); ?> 

答案 2 :(得分:0)

如果您只想在用户登录时显示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 '';
    },
],