Yii2 Gridview 動態顯示行或列和action列

咱們知道Yii中的GridView組件是很是好用的。php

某些狀況要動態顯示某列,這時候就要用到visible屬性html

'propString' => ['attribute' => 'propertyNamesStringV2','format' => 'raw', 'visible' => isset($_GET['pno']) ? true : false], 

注意rowOptions。 根據文檔他能夠接收數組或匿名函數。將判斷邏輯寫到匿名函數裏面。當知足就給當前行加個隱藏的class。至關於不顯示。web

給個例子,注意好像效率比較低,能夠經過一次查詢再判斷。數組

        <?=GridView::widget([
            'dataProvider' => $dataProvider,
            'rowOptions' => function ($model, $key, $index, $grid){
                if(!$res = OrderComment::findOne(['orderId' => $model->orderId, 'itemId' => $model->itemId ]) ){
                    return ['class' => 'hide'];
                }
            },
            'columns' => [
                Order::getOrderViewMoreLink($dataProvider),
                //'order.addTime:datetime',
                $gridViewButtonsHad,
            ],
        ]);?>

 

帖一個列完整代碼app

<?php

use yii\helpers\Html;
use yii\grid\GridView;

/* @var $this yii\web\View */
/* @var $dataProvider yii\data\ActiveDataProvider */
if(Yii::$app->request->url == '/user/staff' || isset($_GET['UserSearch']['sType'])){
    $this->title = '員工管理';
}else{
    $this->title = '用戶管理';
}

$this->params['breadcrumbs'][] = $this->title;

$this->params['actionButtons'] = Html::a('添加用戶', ['create'], ['class' => 'btn btn-success btn-sm',]);

$gridViewButtons = [
    'class' => 'yii\grid\ActionColumn',
    'template' => '{view} {be-promoter} {update} {qrdown}',
    'buttons' => [
        'view' => function($url, $model){
            return Html::a('<i class="fa fa-eye"></i> 查看', $url, ['class' => 'btn btn-primary btn-sm']);
        },
        'be-promoter' => function($url, $model){
            if(!empty($model->consumer)){
                if($model->consumer->isPromoter()){
                    return Html::a('<i class="fa fa-edit"></i> 取消推廣員', $url .'&isBe=0',
                        [
                            'class' => 'btn btn-danger btn-sm',
                            'data-confirm' => '該操做將清除全部推廣積分和等級,肯定取消嗎?',
                            'data-method' => "post"
                        ]);
                }else{
                    return Html::a('<i class="fa fa-edit"></i> 設置爲推廣員', $url .'&isBe=1', ['class' => 'btn btn-info btn-sm',]
                    );
                }
            }
        },
        'update' => function ($url, $model) {
            return Html::a('<i class="fa fa-edit"></i> 編輯', $url, ['class' => 'btn btn-default btn-sm']);
        },
        'qrdown' => function($url, $model){
            if(!empty($model->consumer)){
                if($model->consumer->isPromoter()){
                    return Html::a('下載二維碼', $url .'&consumerId='.$model->consumer->id,
                        [
                            'class' => 'btn btn-success btn-sm',
                        ]);
                }
            }
        },
    ],
];
?>

<div class="user-index">

    <?= $this->render('_search', ['model' => $searchModel, 'queryParams' => $queryParams]) ?>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            [
                'label'  => '頭像LOGO',
                'format' => ['image',['class' => 'thumbnail_image']],
                'value'  => 'avatarUrl',
            ],
            'username',
            'nickname',
            [
                'attribute' => 'position',
                'visible' => Yii::$app->request->pathInfo == 'user/staff'
            ],
            [
                'label' => '積分',
                'format' => 'html',
                'value' => function ($model, $key, $index, $column){
                    $consumer = $model->consumer;
                    if(empty($consumer->credit)) return 0;
                    return Html::a($consumer->credit,['user/credit-detail','consumerId' => $consumer->id]);
                },
            ],
            [
                'label' => '等級',
                'attribute' => 'consumer.levelName'
            ],
            [
                'label' => '幣',
                'format' => 'html',
                'value' => function ($model, $key, $index, $column){
                    $consumer = $model->consumer;
                    if(empty($consumer->belovedCoin)) return 0;
                    return Html::a($consumer->belovedCoin,['user/coin-detail','consumerId' => $consumer->id]);
                },
            ],
            [
                'label' => '推廣等級',
                'attribute' => 'consumer.promotionLevel'
            ],
            [
                'label' => '推廣積分',
                'format' => 'html',
                'value' => function ($model, $key, $index, $column){
                    $consumer = $model->consumer;
                    if(empty($consumer->promotionPoint)) return 0;
                    return Html::a($consumer->promotionPoint,['user/promotion-point-detail','consumerId' => $consumer->id]);
                },
            ],
            [
                'label' => '推廣連接',
                'attribute' => 'consumer.promoterCode'
            ],
            'mobile',
            'email:email',
            // 'role',
            // 'status',
            'created_at:datetime',
            //'updated_at:datetime',
            //['class' => 'yii\grid\ActionColumn'],
            $gridViewButtons
        ],
    ]); ?>

</div>
相關文章
相關標籤/搜索