在 YII CGridView 中隐藏列

2022-01-04 00:00:00 php html yii

我有一个超过 5 列的表格,我想隐藏一些列,以便仅在选择或展开某行时才显示这些列.

I have a table with more than 5 columns , i want to hide some columns so that those column's are shown only if some row is selected or its expanded .

我正在使用 yiiframework 的 CGridView ,那么我该怎么做呢?

Am using yiiframework's CGridView , so how can i do this in that ?

任何帮助都是可观的..

Any help is appreciable ..

我想要这样的功能,以便在展开特定记录时我可以看到隐藏的列值

I want a feature like this, so that on expanding a particular record i can see the hidden column values

推荐答案

一种方法是:

 'columns'=>array(
        array(
          'name'=>'columnName',
          'visible'=>false
            ),
         )

所以你需要动态操作可见性属性:

So you need to manipulate visibility attributes dynamically :

 'visible'=>$this->checkVisible() //custom function 

像这样取决于您的要求

示例:views/user/admin.php

.....
.....
<?php
$toggleUDetails = <<<JS
 $('a.toggle').live('click',function(e){
    e.preventDefault();

    if(this.href.split('#')[1]=='loaded') return $(this).closest("tr").next('tr.toggle').toggle();

    trow=$(this).closest("tr");

   var ajaxOpts={type:"POST", url:this.href ,dataType:'json',success:function(data){
            $(trow).after(data.row);
      }
    };

   this.href=this.href+'#loaded';

   $.ajax(ajaxOpts);

  });
JS;
Yii::app()->clientScript->registerScript('toggleUD', $toggleUDetails, CClientScript::POS_READY); 

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'user-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
     array(
        'class'=>'CButtonColumn',
         'header'=>'Toggle Details',
          'template'=>'{toggle}',
            'buttons'=>array(
              'toggle'=>array(
                        'label'=>'Details',                        
                             'imageUrl'=>Yii::app()->request->baseUrl.'/images/expand.png',  
                             'url'=>'Yii::app()->createUrl("user/getExtra", array("id"=>$data->id))',
                             'options'=>array('class'=>'toggle',

                                      ),
                               ),
                        ),
          ),

        'id',
        'username',
        'password',
        'email',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); ?>

用户控制器

public function actionGetExtra($id){
        $model=User::model()->findByPk($id);
        echo json_encode(array('row'=> '<tr class="toggle"><td   colspan="6">'. $model->username.'</td></tr>'));

  }

启用访问权限:

array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions'=>array('create','update','getExtra'),
                'users'=>array('@'),
            ),

我能为你做的就这么多.记得改Java脚本函数来切换图片图标我没有这样做

that much I can do you for . Remember to change the Java script function to toggle the image icon I have not done that

相关文章