Yii2:如何在GridView Widget中使用条件

时间:2017-08-02 08:18:02

标签: gridview model yii2 action

我的项目中有一个Gridview Widget 'filterModel' => $searchModel,,它向用户显示了一个过滤字段和一个用于加载操作按钮的操作列。我的问题是,我想重用这个Gridview与控制器的两个独立的操作。一个操作,即index操作为用户加载数据,并允许用户使用过滤器字段进行过滤,并在操作列中执行各种操作。另一个controller action用于报告。这会生成pdf报告,我不希望报告中包含过滤字段和操作列。 有没有办法将条件传递给Gridview而不必使用if else重写整个代码,因为我发现这在使用的代码量方面无效。这是我到目前为止编写的代码,但需要一个代码最少的简短形式。

的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'
            ],
        ],
    ]);
}

1 个答案:

答案 0 :(得分:1)

如果条件:

,要隐藏过滤器,请使用短语法
'filterModel' => $myCondition ? $searchModel : null,

隐藏ActionColumn使用visible属性:

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