CGridView questions

I have two questions. Thanks for help.

  1. One column is CCheckBoxColumn. I would like to initially check all boxes. By setting the ‘checked’ attribute to ‘true’, all boxes are checked except the header. I read the source code. None of the cases in renderHeaderCellContent() would initially set ‘checked’ to true. If there any approach to do this on PHP side?

  2. I want to enable pagination and sorting. I use CArrayDataProvider because my data source is from external API. The data is fetched according to user’s input, which is passed with POST method in ajax call. The problem is, the pagination ajax call is done in GET. In this call, I do not have the user input. How do I reconstruct the CArrayDataProvider? Should I save the user input somewhere (session? cache?), or save the data retrieved from API?

Below is a pseudo code of the function




function actionShow()

{

  if (isset($_POST['user_input']))

  {

    $data = call_api($_POST['user_input']);

    $data_provider = new CArrayDataProvider(data, array(

      'keys' => array('data'),

      'pagination' => array(

        'pageSize' => 10,

      ),

    ));

    $this->renderPartial('index', array('data_provider' => $data_provider), false, true);

  }

  else

    throw new CHttpException(500);

}



You may just override method renderHeaderCellContent() in subclass, for example:




Yii::import('zii.widgets.grid.CCheckBoxColumn');

class MyCheckBoxColumn extends CCheckBoxColumn {

	protected function renderHeaderCellContent()

	{

		if($this->selectableRows===null && $this->grid->selectableRows>1)

			echo CHtml::checkBox($this->id.'_all',$this->checked,array('class'=>'select-on-check-all'));

		else if($this->selectableRows>1)

			echo CHtml::checkBox($this->id.'_all',$this->checked);

		else

			parent::renderHeaderCellContent();

	}

}



and configure your grid:




	'columns' => array(

/* some code here */

		array(

			'class' => 'path.to.class.MyCheckBoxColumn',

			'checked' => 'true',

			'value' => '$data->id',

			'selectableRows' => 2,

		),

/* some code here */

	),



Thanks for answes. I personally think subclassing is not a good idea, since I need to maintain the code every time I update Yii. Would it be possible that Yii officially adopts your code in future releases? Hard coding "false" is not flexible, right?

subclassing is the way to achieve it.

Next releases should maintain the compatiblity most of the time, if not you only need to change a bit your code.

I personally extend every class that I need to change a bit its functionality

Yes, open a ticket in google code and qiang will consider to add it to the core

I will. Thanks. Do you have any suggestion to my second question?

For reference, the corresponding Google Code issue is here: http://code.google.com/p/yii/issues/detail?id=2230

change it a bit to accept GET parameters




  if (isset($_POST['user_input']) || isset($_GET['gridId']))

  {

    $data = call_api(isset($_POST['user_input']?$_POST['user_input'] : $_GET['gridId'] );



I dont see the need to save it

PS.: I just checked, and your request ticket was accepted already

Sorry. I was restricted for the first-day post quota.

I do not understand your code. What is $_GET[‘gridId’]? The only GET parameters I receive in my case are ajax = “yw0” and “page = <page_number>”. The API is provided by 3rd-party (e.g. Twitter, Facebook). I cannot reconstruct the “user_input” from $_GET[‘gridId’].

I also do not plan to store the user input in session because of its sensitivity (password related).

My first problem has been solved. Refer to the issue. Just wait for Yii 1.1.7. Anyone help on the CArrayProvider question? Thank you!

where you posted


/* some code here */



what does it contain ?

if you specify the columns name and also give the grid an id, so you can colect data like $_GET[‘gridId’]

Hi Gustavo, are you asking migel, because the "some code here" is posted by him.

I define my grid as follows:




$this->widget('zii.widgets.grid.CGridView', array(

	'id' => 'test',

	'dataProvider' => $data_provider,

	'selectableRows' => 0,

	'columns' => array(

		array(

			'name' => 'TestForm[testfield][]',

			'class' => 'CCheckBoxColumn',

			'value' => '$data->email',

			'checked' => 'true',

			'selectableRows' => 2,

		),

		array(

			'name' => 'email',

			'header' => 'Email',

		),

		array(

			'name' => 'name',

			'header' => 'Name',

		),

	),

));



When I navigate to the second page, the only GET parameters I get are




ajax => test

page => 2



$_GET[‘test’] does not work.

Is there some way I can access the data in the grid, given its grid ID? Or is there some way I can temporarily store the grid data in full, and automatically destroy after the user leave the web page?