$. fn.yiiGridView.getSelection returns empty

Hi guys.

I can not understand why my code does not work. I want to get all lines that were selected.

This is my cgridview:




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

            'id'=>'grid_aceite',

			'ajaxUpdate'=>true,

			'dataProvider' => $myValue,

           	'summaryText'=>'',

			'selectableRows'=>NULL,//select more than one

	

			'columns' => array(

           

			 array(

                      'header'=>'Hora Retificada',

                      //'name'=>'obs',

                       'value'=>'$data->hora_retificada== null?"":date("H:i:s",strtotime($data->hora_retificada))',

                     

					  'htmlOptions'=>array('class'=>'hora_retificada'),	

                ),	 

				 array(

                      'header'=>'Justificativa',

                      'name'=>'$data->justificativa',

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

                     

					  'htmlOptions'=>array('class'=>'justificativa'),	

                ),	

				array

				(

					'header'=>'Aceitar',

					'class'=>'CCheckBoxColumn',

					'htmlOptions'=>array('style'=>'text-align:center'),	

					'id'=>'pontos_aceitos',


				),

				),

              	));				




Here my button that call the js function to get all rows selected:




echo CHtml::Button ("Aceitar justificativas",

                             				 $htmlOptions=array

							 (

							 	'id'=>'btn_aceitar',

								'onclick'=>'aceitar_just();'

								

							 )

							

						)



And here my js function:




function aceitar_just()

	{

		

		var sel_row=$.fn.yiiGridView.getSelection('grid_aceite');

		alert(sel_row); 

	}



And the result is empty or comma when rows selected > 1.

What i doing wrong?

Thank you!

Do you want selected rows or checked rows ?

In the above code you set selectableRows to NULL that means no selection at all.

Hi mdomba!

Thank you very much for your reply.

The property selectableRows = NULL or selectableRows= 2 the result is the same.

I want the checked rows but if they are checked they will be selected as well or am I wrong?

Thank you again.

there are 2 selectableRows properties…

one is for the CGridView, and this one by documentation does not have NULL value - http://www.yiiframework.com/doc/api/1.1/CGridView#selectableRows-detail

the other is for CCheckBoxColumn and this one has the NULL value - http://www.yiiframework.com/doc/api/1.1/CCheckBoxColumn#selectableRows-detail

Humm, interesting.

Ok, the code looks like this:




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

            'id'=>'grid_aceite',

			'ajaxUpdate'=>true,

			'dataProvider' => $myValue,

           	'summaryText'=>'',

			'selectableRows'=>2,//select more than one

			

			

			'columns' => array(

           

			 array(

                      'header'=>'Data',

                      'name'=>'data_registro',

                      //'value'=>'data::data($dados_ponto->data_registro,\'P\')',   

                      'value'=>'date("d/m/Y",strtotime($data->data_registro))',

					  //'htmlOptions'=>array('style'=>'text-align:center;width:20%'),

					  'htmlOptions'=>array('class'=>'dataregistro'),	

					  

					  

                ),

			    array(

                      'header'=>'Hora',

                      'name'=>'hora_registro',

					  'id'=>'hora_registro',

                      'value'=>'date("H:i:s",strtotime($data->hora_registro))',  

                      //'htmlOptions'=>array('style'=>'text-align:right;width:20%'),

					  'htmlOptions'=>array('class'=>'hora','id'=>'id'),	

                ),

				 array(

                      'header'=>'Hora Retificada',

                      //'name'=>'obs',

                       'value'=>'$data->hora_retificada== null?"":date("H:i:s",strtotime($data->hora_retificada))',

                     

					  'htmlOptions'=>array('class'=>'hora_retificada'),	

                ),	 

				 array(

                      'header'=>'Justificativa',

                      'name'=>'$data->justificativa',

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

                     

					  'htmlOptions'=>array('class'=>'justificativa'),	

                ),	

				array

				(

					'header'=>'Aceitar',

					'class'=>'CCheckBoxColumn',

					'htmlOptions'=>array('style'=>'text-align:center'),	

					'id'=>'pontos_aceitos',

					'selectableRows'=>2

				),

			

				),

              

			));			




But the funcion $.fn.yiiGridView.getSelection(‘grid_aceite’) still return empty value.

Thank you again!

With this code you can check multiple checkboxes… but when you check a checkbox the row will not be selected… to select a row you need to click anywhere else on the row… is it working like that for you ?

If you want that the row gets selected by checking then you just need to remove the second selectableRows.

Note that getSelection() returns the IDs of the selsected rows, there is getChecked that can be used to return IDs of the checked rows.

I just saw that the selection of the normal lines of the grid is not working. I removed the checkbox column and selected rows by clicking on them and the function still returns empty value.




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

            'id'=>'grid_aceite',

			'ajaxUpdate'=>true,

			'dataProvider' => $myValue,

           	'summaryText'=>'',

			'selectableRows'=>2,//select more than one

			

			

			'columns' => array(

           

			 array(

                      'header'=>'Data',

                      'name'=>'data_registro',

                      //'value'=>'data::data($dados_ponto->data_registro,\'P\')',   

                      'value'=>'date("d/m/Y",strtotime($data->data_registro))',

					  //'htmlOptions'=>array('style'=>'text-align:center;width:20%'),

					  'htmlOptions'=>array('class'=>'dataregistro'),	

					  

					  

                ),

			    array(

                      'header'=>'Hora',

                      'name'=>'hora_registro',

					  'id'=>'hora_registro',

                      'value'=>'date("H:i:s",strtotime($data->hora_registro))',  

                      //'htmlOptions'=>array('style'=>'text-align:right;width:20%'),

					  'htmlOptions'=>array('class'=>'hora','id'=>'id'),	

                ),

				 array(

                      'header'=>'Hora Retificada',

                      //'name'=>'obs',

                       'value'=>'$data->hora_retificada== null?"":date("H:i:s",strtotime($data->hora_retificada))',

                     

					  'htmlOptions'=>array('class'=>'hora_retificada'),	

                ),	 

				 array(

                      'header'=>'Justificativa',

                      'name'=>'$data->justificativa',

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

                     

					  'htmlOptions'=>array('class'=>'justificativa'),	

                ),	

				/*array

				(

					'header'=>'Aceitar',

					'class'=>'CCheckBoxColumn',

					'htmlOptions'=>array('style'=>'text-align:center'),	

					'id'=>'pontos_aceitos',

					

				),*/

			

				),

              

			));			



What makes me mad is that I’ve used this feature and it worked perfectly.

Thank you very much for your attention!

It should work, probably you are getting some JS error… try to check with a tool like firebug to see if there is any JS error…

To exclude some possible JS problems

instead of


'onclick'=>'aceitar_just();'

try with


'onclick'=>'alert($.fn.yiiGridView.getSelection("grid_aceite"));'

and don’t forget to click on some rows, if no rows are selected the returned value is empty.

Same result.

Here is a scrrenshot to prove the selection of the items grid.

I think the additional information could help us to solve this problem.

The grid is populated with ajax.

Here is where wmy grid is called using renderPartial:




...


<div id="data">

  <?php $this->renderPartial('grid_aceite', array('myValue'=>$myValue)); ?>

			

</div>



My grid is located in this view(‘grid_aceite’):




if ($myValue)

	{

		 

		 /*$this->widget('ext.selgridview.SelGridView', array(

			  'id'=>'grid_aceite',

			  'dataProvider'=>$myValue,

			  'ajaxUpdate'=>true, // Do you need this line? It's by default I think

			  'columns'=>array(

				'codigo_cadfu',

				'hora_registro',

			  ), // You need this line

			  

			));*/

		//$this->widget('ext.selgridview.SelGridView', array(


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

            'id'=>'grid_aceite',

			'ajaxUpdate'=>true,

			'dataProvider' => $myValue,

           	'summaryText'=>'',

			'selectableRows'=>2,//select more than one

			

			

			'columns' => array(

           

			 array(

                      'header'=>'Data',

                      'name'=>'data_registro',

                      //'value'=>'data::data($dados_ponto->data_registro,\'P\')',   

                      'value'=>'date("d/m/Y",strtotime($data->data_registro))',

					  //'htmlOptions'=>array('style'=>'text-align:center;width:20%'),

					  'htmlOptions'=>array('class'=>'dataregistro'),	

					  

					  

                ),

			    array(

                      'header'=>'Hora',

                      'name'=>'hora_registro',

					  'id'=>'hora_registro',

                      'value'=>'date("H:i:s",strtotime($data->hora_registro))',  

                      //'htmlOptions'=>array('style'=>'text-align:right;width:20%'),

					  'htmlOptions'=>array('class'=>'hora','id'=>'id'),	

                ),

				 array(

                      'header'=>'Hora Retificada',

                      //'name'=>'obs',

                       'value'=>'$data->hora_retificada== null?"":date("H:i:s",strtotime($data->hora_retificada))',

                     

					  'htmlOptions'=>array('class'=>'hora_retificada'),	

                ),	 

				 array(

                      'header'=>'Justificativa',

                      'name'=>'$data->justificativa',

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

                     

					  'htmlOptions'=>array('class'=>'justificativa'),	

                ),	

				/*array

				(

					'header'=>'Aceitar',

					'class'=>'CCheckBoxColumn',

					'htmlOptions'=>array('style'=>'text-align:center'),	

					'id'=>'pontos_aceitos',

					

				),*/

				

				

				

				

				),

              	

			));	

	}

	 echo CHtml::Button ("Aceitar justificativas",

                             

							 

							 $htmlOptions=array

							 (

							 	'id'=>'btn_aceitar',

								'onclick'=>'alert($.fn.yiiGridView.getSelection("grid_aceite"));'

								

							 )

							

						)



And my action that populate the grid through ajax:




public function actionAceitarJustificativa()

{

		

		$model = new AceitaJustificativa();

		

		//Here is the code that populate the grid through ajax

		if(Yii::app()->request->isAjaxRequest)

		{

			$data = array();

			$model->attributes=$_POST['AceitaJustificativa'];				

			$model->codigo_cadfu =$_POST['AceitaJustificativa']['codigo_cadfu'];	

			$model->datainicio=$_POST['AceitaJustificativa']['datainicio'];	

			$model->datafim=$_POST['AceitaJustificativa']['datafim'];	

				

			$data["myValue"] = VwPontoRegistro::model()->getListaPontos($model->codigo_cadfu,$model->datainicio,$model->datafim);

					

			$this->renderPartial('grid_aceite', $data, false, true);

			

		}

		else

		{

			$data = VwPontoFuncionario::model()->GetListaSubordinados($_GET['term']);

			$this->render('aceitar_justificativa',array('model'=>$model,'data'=>$data));

		}

		

		

		

			

	}




I discover the problem, but i don´t know how to solve this.

My model was generated by gii and this model is a representation of a view of my database. When i changed to a "normal" model(database table) it worked beautifully. Now, how can i solve this? I need my database view to feed my grid.

Thank you!

Yes, the view is probably the problem… Yii does not know what is the primary key there as the view structure does not define that… and thats why there are no primary key values returned for the selected rows…

You need to define the field whose value will be used as primary key for the view. In your view model add a method like this


public function primaryKey()

{

    return 'enter column name to be used as primary key here';

    // For composite primary key, return an array like the following

    // return array('column name 1', 'column name 2');

}

YES!

Thank you very much mdomba! It worked beautifully.

Maybe, in the gii module, when a model will generated and that is a database view, show a message to remember to add the primary key to avoid possible problems like this. I know this advise is found at documentation but this may go unnoticed as i.

I think this feature will pretty cool and useful.

Thanks again.

problem is that gii does not know it’s a view.

I see.

So, this topic can be closed.

:P