如何在yii2 gridview中根据条件隐藏列?

编程入门 行业动态 更新时间:2024-10-28 12:23:20
本文介绍了如何在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中根据条件隐藏列?

本文发布于:2023-08-03 15:34:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1288007.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条件   如何在   gridview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!