在YII CGridView中隐藏列 [英] Hiding column's in YII CGridView
问题描述
我有一个超过5列的表格,我想隐藏一些列,以便只有在某行被选中或展开时才显示这些列。
我使用yiiframework的CGridView,所以我该如何做到这一点?
任何帮助都是可观的。
我想要一个像这样的功能,以便扩展一个特定的记录,我可以看到隐藏的列值
一种方法是:
'columns'=>数组(
数组(
'name'=>'columnName',
'visible'=> false
),
)
需要动态操作可见性属性:
'visible'=> $ this-> checkVisible()//自定义函数
这取决于您的要求
编辑(使用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){$ b $(trow).after(data。行);
}
};
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}',
'按钮'=>数组
'toggle'=> array(
'label'=>'Details',
'imageUrl'=> Yii :: app() - > request-> baseUrl '/ images / expand.png',
'url'=>'Yii :: app() - > createUrl(user / getExtra,array(id=> $ data-> ;'b'b'options'=> array('class'=>'toggle',
),
),
),
),
'id',
'用户名',
'密码',
'email',
数组
'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>'));
$ b
启用访问权限:
数组('allow',//允许认证用户执行'创建'和'更新'动作
'动作'=>数组('create','update','getExtra'),
'users'=>数组('@'),
),
我可以为你做的事。请记住更改Java脚本函数以切换图像图标
我还没有做到这一点
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 .
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
One way is :
'columns'=>array(
array(
'name'=>'columnName',
'visible'=>false
),
)
So you need to manipulate visibility attributes dynamically :
'visible'=>$this->checkVisible() //custom function
sth like this depending on your requirement
Edit (using ajax +jquery)
example :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',
),
),
)); ?>
User Controller
public function actionGetExtra($id){
$model=User::model()->findByPk($id);
echo json_encode(array('row'=> '<tr class="toggle"><td colspan="6">'. $model->username.'</td></tr>'));
}
Enable access rights:
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update','getExtra'),
'users'=>array('@'),
),
that much I can do you for . Remember to change the Java script function to toggle the image icon I have not done that
这篇关于在YII CGridView中隐藏列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!