japheth
japheth

Reputation: 473

Yii2: How to use conditions inside GridView Widget

I have a Gridview Widget in my project with 'filterModel' => $searchModel, that shows the user a filter fields and the action column for loading action buttons. My problem is, I want to reuse this Gridview with two separate actions of the controller. One action i.e. the index action loads the data for the user and allows the user to filter using the filter fields and do various actions in the action column. The other controller action is for reports. This produces a pdf report and I don't want the filter fields and the action column to be there in the reports. Is there a way of passing a condition to the Gridview without having to rewrite the whole code using the if else because I find this ineffective in terms of amount of code used. Here is the code I have written so far but need a shorter form with minimal code.

index.php

<?php 

$action_id = Yii::$app->controller->action->id;

if ($action_id == 'index') {
    \yiister\adminlte\widgets\grid\GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        "condensed" => false,
        "hover" => true,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            'name',
            'mobile_number',
            'sex',
            [ 
                // the attribute 
                'attribute' => 'date', 
                // format the value 
                'value' => function ($model) { 
                    if (extension_loaded('intl')) { 
                        return Yii::t('app', '{0, date, MMMM dd, YYYY HH:mm}', [$model->date]); 
                    } else { 
                        return date($model->date); 
                    } 
                }, 
                // some styling? 
                'headerOptions' => [ 
                    'class' => 'col-md-2' 
                ], 
                // here we render the widget 
                'filter' => DateRangePicker::widget([ 
                    'model' => $searchModel, 
                    'attribute' => 'date_range',
                    'pluginOptions' => [ 
                        'format' => 'd-m-Y', 
                        'autoUpdateInput' => false 
                    ] 
                ]) 
            ],
            [
            'attribute' => 'officer',
            'value' => 'officer.first_name'
            ],
            ['class' => 'yii\grid\ActionColumn',
             'template' => '{view}&nbsp{update}',   //{view}&nbsp;
             'buttons' => [
                 'view' => function($url, $model)   {
                        return Html::a('<button class="btn btn-success"><i class="glyphicon glyphicon-eye-open"></i></button>',$url, [
                            'class' => 'showModalButton',
                            'id' => 'lead-view',
                            'title' => 'View Customer Details',
                            'value' => $url,
                            'data-toggle' => 'modal',
                            'data-target' => 'modal',
                          ]);
                    },
                 'update' => function($url, $model) {
                        return Html::a('<button class="btn btn-primary"><i class="glyphicon glyphicon-pencil"></i></button>',$url, [
                                'title' => 'Edit Customer Details',
                            ]);
                    },
                'urlCreator' => function($action, $model, $key, $index) {
                      if ($action == 'view') {
                          return Html::a('Action', $url);
                      }
                      if ($action == 'update') {
                         return Html::a('Action', $url);
                      }
                    } 
                ],            
            ],  // fin ActionColumn
        ],
    ]);
} else {
    \yiister\adminlte\widgets\grid\GridView::widget([
        'dataProvider' => $dataProvider,
        "condensed" => false,
        "hover" => true,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            'name',
            'mobile_number',
            'sex',
            [ 
                // the attribute 
                'attribute' => 'date', 
                // format the value 
                'value' => function ($model) { 
                    if (extension_loaded('intl')) { 
                        return Yii::t('app', '{0, date, MMMM dd, YYYY HH:mm}', [$model->date]); 
                    } else { 
                        return date($model->date); 
                    } 
                }, 
                // some styling? 
                'headerOptions' => [ 
                    'class' => 'col-md-2' 
                ], 
                // here we render the widget 
                'filter' => DateRangePicker::widget([ 
                    'model' => $searchModel, 
                    'attribute' => 'date_range',
                    'pluginOptions' => [ 
                        'format' => 'd-m-Y', 
                        'autoUpdateInput' => false 
                    ] 
                ]) 
            ],
            [
            'attribute' => 'officer',
            'value' => 'officer.first_name'
            ],
        ],
    ]);
}

Upvotes: 0

Views: 2845

Answers (1)

Yupik
Yupik

Reputation: 5031

To hide filters use short syntax if condition:

'filterModel' => $myCondition ? $searchModel : null,

To hide ActionColumn use visible attribute:

[
   'class' => 'yii\grid\ActionColumn',
   'visible' => $myCondition ? true : false,
   // here rest of your code
],

Upvotes: 1

Related Questions