在YII CGridView中隐藏列

Akh*_*yil 9 html php yii

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

我正在使用yiiframework的CGridView,那我怎么能这样做呢?

任何帮助都很明显..

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

在此输入图像描述

sak*_*zai 20

一种方法是:

 'columns'=>array(
        array(
          'name'=>'columnName',
          'visible'=>false
            ),
         )
Run Code Online (Sandbox Code Playgroud)

因此,您需要动态操作可见性属性:

 'visible'=>$this->checkVisible() //custom function 
Run Code Online (Sandbox Code Playgroud)

这样取决于你的要求

编辑(使用ajax + jquery)

示例: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',
        ),
    ),
)); ?>
Run Code Online (Sandbox Code Playgroud)

用户控制器

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

  }
Run Code Online (Sandbox Code Playgroud)

启用访问权限:

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

我可以为你做多少.请记住更改Java脚本功能以切换我没有这样做的图像图标