Yii Framework Forum: How To Hide/show Columns In Cgridview Via Ajax - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How To Hide/show Columns In Cgridview Via Ajax Rate Topic: ***** 1 Votes

#1 User is offline   Age 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 20-February 13

Posted 20 February 2013 - 01:34 AM

Hey guyz
I am kind of stuck right now. I have many columns for a grid. I want to give user an option that he can See only columns that he selected to show on runtime via Ajax
I know we use "$.fn.yiiGridView.update" to update grid on runtime but how can i update Grid to show/hide certain columns only.
0

#2 User is offline   rootbear 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 222
  • Joined: 17-June 11

Posted 20 February 2013 - 10:32 AM

* (i did not have time to put code together, leave it to yourself)

try this:

1) in your model, add

public static $visible_flag;


2) in your view (gridView), add visible property to each of your column

'visible' => $model->visible_flag['field_name'],


3) in between your view and model->search function, pass js version of this array when user select show and hide column before calling, in your search function reset $this->visible_flag before render 'admin' or 'list' page

** if you need persistence of the column selection, you may need using db or cookie trick, then each column's visible property read such value from db or cookie
I enjoy the Yii.sy coding life here.
0

#3 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 20 February 2013 - 01:39 PM

Dear Friend

This is quet intresting. Following is one simple implementation.

I have got a model.User(id,name,age,sex,email,address).

CONTROLLER.
public function actionAdmin()
{   
        $model=new User('search');
	$columns=array_keys($model->getAttributes());//we are getting in a array.array('id','name','age','sex','email','address'),
	if(isset($_GET["Columns"]))
		$columns=$_GET["Columns"];//If user chooses the column, column names changes accordingly.
	$model->unsetAttributes();  // clear any default values
	if(isset($_GET['User']))
		$model->attributes=$_GET['User'];

	$this->render('admin',array(
		'model'=>$model,
		'columns'=>$columns,
		));
}


VIEW.
<?php
echo CHtml::checkBoxList('Columns',$columns,array_combine($columns,$columns),array('id'=>'columns','separator'=>''));
?>
</div>
<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'user-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
	'columns'=>array_merge($columns,array(array('class'=>'CButtonColumn'))),
)); ?>

<?php
Yii::app()->clientScript->registerScript('column','
        $("input[name=\"Columns[]\"]").change(function()
           {
                var data=$("input[name=\"Columns[]\"]:checked").serialize();
                $("#user-grid").yiiGridView("update",{data:data});
           }
       );
');


1.We are rendering a checkBoxList of attributes.By default I made all the checkboxes checked.
2.We are declaring columns inside the widget and merging with other columns.
3.We are registering a script to get the values from checkbox list and updating the grid with selected columns.

Here I made myself comfortable by calling attribute names as column.It becomes bit complex if you make a column
in an array format(name,value,type,etc).That also can easily be solved.

This is the screenshot from my localhost.
Attached File  columns.png (20.95K)
Number of downloads: 66
Regards.
0

#4 User is offline   le_top 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 326
  • Joined: 08-June 10
  • Location:France

Posted 20 February 2013 - 03:02 PM

You can also set a class for each of the HTML elements and do a hide/show on the class:
I set this in some column definitions for the CGridView:
                'headerHtmlOptions'=>array('class'=>'colclass',),
                'footerHtmlOptions'=>array('class'=>'colclass',),
                'filterHtmlOptions'=>array('class'=>'colclass',),
                'htmlOptions'=>array('class'=>'colclass',),

0

#5 User is offline   Age 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 20-February 13

Posted 22 February 2013 - 04:28 AM

Thanks seenivasan And rootbear
I sorted that out with your suggestions :rolleyes:
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users