如何从GridView中隐藏条件的行?

时间:2017-02-24 13:05:43

标签: php gridview yii2

   $query =Cms::find();
   $dataProvider = new ActiveDataProvider([
    'query' => $query ,
    ]);

嗨,我正在尝试隐藏“Status_id”设置为“非活动”的某些行,这是使用我在“操作”列中创建的自定义操作按钮完成的。我想如果我可以在$ dataprovider中添加我自己的查询,我可能会这样做,但我也不知道该怎么做。请帮助,如果这是正确的方式,如果不是我该怎么办?在此先感谢。这是我的小部件。

echo GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,

    'columns' => [
        'Contract_id',
        'Status_id',
            ['header' => 'Actions',
            'class' => 'yii\grid\ActionColumn',
            'template' => '{new_action}',
            'buttons' => [
                'new_action' => function ($url) {
                    return Html::a('<span class="glyphicon glyphicon-fire"></span>', $url, [
                                'title' => Yii::t('app', 'Burn Contract'),
                                'data-confirm' => Yii::t('yii', 'Are you sure you want to burn this contract?'),
                                'data-method' => 'post', 'data-pjax' => '0',
                    ]);
                }
            ], 'urlCreator' => function ($action, $model) {
                if ($action === 'new_action') {
                    $url = Url::to(['cms/burn', 'id' => $model->Contract_id]);
                    return $url;
                }
            }
        ],
    ],
]);

1 个答案:

答案 0 :(得分:1)

我认为这可行:

echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,

'columns' => [
    'Contract_id',
    'Status_id',
        ['header' => 'Actions',
        'class' => 'yii\grid\ActionColumn',
        'template' => '{new_action}',
        'buttons' => [
            'new_action' => function ($url) {
                return Html::a('<span class="glyphicon glyphicon-fire"></span>', $url, [
                            'title' => Yii::t('app', 'Burn Contract'),
                            'data-confirm' => Yii::t('yii', 'Are you sure you want to burn this contract?'),
                            'data-method' => 'post', 'data-pjax' => '0',
                ]);
            }
        ], 'urlCreator' => function ($action, $model) {
            if ($action === 'new_action') {
                $url = Url::to(['cms/burn', 'id' => $model->Contract_id,'burn'=>true]);
                return $url;
            }
        }
    ],
],
]);

控制器:

$query =Cms::find();
if (\Yii::$app->request->post('burn')) 
{
    $query->where(['<>','Status_id', 'Inactive']);
}
$dataProvider = new ActiveDataProvider([
  'query' => $query
]);