本文介绍了如何在yii2 gridview中根据条件隐藏列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在索引页面上放置了条件语句.

I have placed a conditional statement in my index page.

控制器

$type ="402"; // type can me 401 and 403
 $searchModel = new MdcmetersdataSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'type'  => $type
        ]);

Index.php

<?php
if($type == '401')
{
    $columns = [
        ['class' => 'yii\grid\SerialColumn'],

        'device_id',
        'cust_id',
        'msn',
        'current_p1',
        'current_p2',
        'current_p3',
        'data_date_time',

        ['class' => 'yii\grid\ActionColumn'],
    ];



}else if($type == '402')
{
    $columns = [
        ['class' => 'yii\grid\SerialColumn'],

        'device_id',
        'cust_id',
        'msn',
        'voltage_p1',
        'voltage_p2',
        'voltage_p3',
        'data_date_time',

        ['class' => 'yii\grid\ActionColumn'],
    ];
}
else if($type == "403")
{
    $columns = [
        ['class' => 'yii\grid\SerialColumn'],

        'device_id',
        'cust_id',
        'msn',
        'kwh',
        'data_date_time',

        ['class' => 'yii\grid\ActionColumn'],
    ];

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

        'device_id',
        'cust_id',
        'msn',
        'voltage_p1',
        'voltage_p2',
        'voltage_p3',
        'current_p1',
        'current_p2',
        'current_p3',
        'device_id',
        'kwh',


        'data_date_time',

        ['class' => 'yii\grid\ActionColumn'],
    ];

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

如上所述,$type的值可以为401, 402 and 403.因此,我正在尝试检查我的状况是否正常.因此,我通过了402,这意味着只显示具有voltages值的列,但得到了以下结果

As mentioned above that the value of $type can be 401, 402 and 403. So I am trying to check whether my condition is working or not. So I pass 402 which means only the columns with voltages value should be shown, but I got the following result

我想隐藏红色圆圈,即我只想显示该特定$type值的数据.

I want to hide the red circled columns, i.e. I just want to show the data of that particular $type value.

任何帮助将不胜感激.

推荐答案

如果您要隐藏/不显示402的无电压值的行,则简单的方法是避免在搜索查询中进行选择.为此,您可以在搜索模型中添加适当的402搜索功能

If you want hide/don't show the rows without volt value for 402 the simples way is aavoid selection in search query .For this you could add a proper 402 search funtion in the search model

/**
 * Creates data provider instance with search query applied
 *
 * @param array $params
 *
 * @return ActiveDataProvider
 */
public function search($params)
{
   ........

    return $dataProvider;
}


public function search402($params)
{
    $query = YourModel::find();

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

    if (!($this->load($params) && $this->validate())) {
        return $dataProvider;
    }

    $query->andWhere(['not', ['volt1' => null]]);

    ;
    return $dataProvider;
}

并在控制器中管理您真正需要的搜索功能

and in controller manage the search function you really need

$searchModel = new MdcmetersdataSearch();
switch($type){
    case '402':
    $dataProvider = $searchModel->search402(Yii::$app->request->queryParams);
    break;
    ....
}

这篇关于如何在yii2 gridview中根据条件隐藏列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:01