如何在GridView Yii2中创建条件

时间:2018-08-24 06:52:44

标签: php yii yii2

我在这里有问题。
我想在GridView

中添加一个条件
<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        'table_column1',
        'table_column2',
        //CONDITION HERE
        if(condition){
            //BUTTON HERE
        } else {
            //TOOLS COLUMN HERE
        }
    ],
]); ?>

我该怎么做?

3 个答案:

答案 0 :(得分:2)

如果您只想根据指定条件显示/隐藏某些列,则可以使用$visible属性:

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        'table_column1',
        'table_column2',
        [
            'name' => 'some_column1',
            'visible' => $condition,
        ],
        [
            'name' => 'some_column2',
            'visible' => !$condition,
        ],
    ],
]); ?>

基于$condition,只有两列(some_column1some_column1)之一可见。

答案 1 :(得分:1)

如果您需要两个gridview,那么一个简单的方法是基于替换的$ column内容

if ( condition) {

    $columns  = [
        ['class' => 'yii\grid\SerialColumn'],
        'table_column1',
        'table_column2',
          [
            'attribute'=>'your_att',
            'header'=>'your_header',
            'format'=>'raw',    
            'value' => function($model, $key, $index)
            {   
                    return '<button class="btn green">Y</button>';
            },
        ],
        ],

    } else {
        $columns  = [
        ['class' => 'yii\grid\SerialColumn'],
        'table_column1',
        'table_column2',
         ['class' => 'yii\grid\ActionColumn',
        ],
    }
}

echo GridView::widget([
  'dataProvider' => $dataProvider,
  'filterModel' => $searchModel,
  'columns' => $columns
]); ?>

否则使用原始列类型的value函数,您可以返回每个特定行所需的代码。

答案 2 :(得分:0)

 [
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'status',
            'label'=>'Status',
             'filterType' => \kartik\grid\GridView::FILTER_SELECT2,
                'filter' => ArrayHelper::map(\app\models\PropertyStatus::find()->all(), 'status', 'status'),
                'filterWidgetOptions' => [
                    'theme' => Select2::THEME_BOOTSTRAP,
                    'pluginOptions' => [
                        'allowClear' => true,
                    ],
                ],
                'filterInputOptions' => ['placeholder' => 'All...'],
                'format'=>'raw',
            //'options' => ['style' => 'width: 8%'],

            'editableOptions'=> function ($model, $key, $index, $widget) {
                if(Yii::$app->user->can('property/status')){
                    $appttypes = ArrayHelper::map(\app\models\PropertyStatus::find()->all(), 'status', 'status');
                    return [
                    'header' => 'Status',
                    'attribute' => 'status',
                    'asPopover' => false,
                    'inlineSettings' => [
                        'closeButton' => '<button class="btn btn-sm btn-danger kv-editable-close kv-editable-submit" title="Cancel Edit"><i class="fa fa-times-circle"></i></button>'
                    ],

                    'type' => 'primary',
                    //'size'=> 'lg',
                    'size' => 'md',
                    'options' => ['class'=>'form-control', 'placeholder'=>'Enter person name...'],
                    'inputType' => Editable::INPUT_DROPDOWN_LIST,
                    'displayValueConfig' => $appttypes,
                    'data' => $appttypes,
                    'formOptions'=> ['action' => ['/ajax/update-property-status']] // point to new action
                ];
                }
            },

        ],