CGridView with CCheckBoxColumn

hello

I’m using CGridView with a CCheckBoxColumn to select records but i don’t know how to execute an action for all the checked records only.

any ideas.

Thnak you

Like this in view:




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

	'id'=>'grid-id',

	'dataProvider'=>$dataProvider,

	'selectableRows'=>2,

	'columns'=>array(

		array(

			'class'=>'CCheckBoxColumn',

			'id'=>'check-boxes',

			'checked'=>"0",

		),

                ...

	),

)); ?>


<?php

   echo CHtml::ajaxLink('Submit', Yii::app()->createUrl('controller/action'),

        array(

           'type'=>'POST',

           'data'=>'js:{ids : $.fn.yiiGridView.getChecked("grid-id","check-boxes")}'

        ),


   );

?>



and in controller




public function actionAction()

{

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

    {

        foreach($_POST['ids'])

        {

            //Top secret code of yours

        }

    }

} 



I Did that but still one problem that there is no link related to Submit it shows




http://www.....com/controller/list/#



and i found out that

The CSRF token could not be verified.

The code provided by fellow member posts needed data not in url. Check the ajax request in Firebug, you’ll see the checked ids are indeed posted.

You can enclose your button in a CActiveForm of its own, and add the field name (here I’m using CSRF-field-name, but change with your own).


…

'data'=>'js:{CSRF-field-name : $("CSRF-field-name").val(), ids : $.fn.yiiGridView.getChecked("grid-id","check-boxes")}'

…

You can also directly include the CSRF token in the data, like Jay_69 does here:

http://www.yiiframework.com/forum/index.php/topic/30992-ccheckboxcolumn-and-csrf/

Thank you very much for your help it works.

Hey

There is something else, actually the action is to delete some records from the database.

And when Submit i can see that the record was deleted but i have to refrech manually the page to see the changes. so is that a way with yii to do it manually ???

Thank you in advance. :)

Try calling




$.fn.yiiGridView.update('gridId');



after receiving your ajax response.

I’m new with yii, I don’t know where to put it. Is there any examples to follow please.

thank you

Something like this maybe:




<?php

   echo CHtml::ajaxLink('Submit', Yii::app()->createUrl('controller/action'),

        array(

           'type'=>'POST',

           'data'=>'js:{ids : $.fn.yiiGridView.getChecked("grid-id","check-boxes")}',

           'success'=>'js:$.fn.yiiGridView.update("grid-id")'

        ),


   );

?>



I’ve never used these facilities, so that might not work. The options available to you are all documented here though, under settings:

http://api.jquery.com/jQuery.ajax/

Oh thank you very much, the problem I don’t have experience with JQuery AJAX .

I think it’s time to start to work on, with your support.

Thanks again

Hi,

I am completely new to Yii. But really interested about it.

I developed a Ccheckboxcolumn as explained in this example and success on halfway. ( The Ids are passing correctly but controller action not work).

My situation is:

I have two tables User and attendance. Attendance is linked to user table.

  1. How can I insert selected user ids to attendance table.

  2. Can I avoid duplicate entries?

It’s a great help if anyone can show me the sample code for above controller action.

Hi

I’m also new with Yii, but i think that maybe you can add the users as this




foreach($ids as $val){

   $attendance = Attendance::model()->findbyPublicID($val)

   if(empty($Attendance)){

      $model = new Attendance;

      $model->userid = $val;

      .

      .

      //you add all the attributes to the new record

      .

      .

      if($model->validate()){

                $bSaveResult = $model->save(); 

      }

   }

}


$this->render('view', 'model'=>$model);




In this way you add users IDs in attendance table and check if the user already present in the table or not.

Dear Maher,

Thank you very much for the quick reply. I spent clueless about three days in this issue. It’s a great release to see your reply.

I tested the code. But ended with an error,

"Attendance and its behaviors do not have a method or closure named "findbyPublicID"."

Can you kindly tell me why I received this error?

Thank you

Yes in your model (table attendance, you have to create a static function as below :




/**

	 * Find by the public visible identifier defined

	 * 

	 * @param type $id

	 */

	public static function findByPublicId($publicId)

	{

return self::model()->find('id=:publicId',array(':publicId'=>$publicId));

	}



You are a life saver. Thank you very much. Now I can update records after two days struggling. Thank you again for the help.

Now Only I have to do is checking the validation. Hope I can do it without bothering you anymore.

Thanks for the third time. :)

Don’t worry, 2 months ago, I was in the same situation as you right now, and it was my first codes with yii and with php5 also, and thanks to all the memebrs of Yii forum I’m proud that I could help you, and I hope that after some time you will do the same, and help others. ;D

@maher Is your findByPublic($id) function not the same as findByPk()?

Yes it’s not the same actually, i use different function for security reasons i use i special property to look for record in data base different than primary key.

Ok, so maybe you could use findByAttributes() then.