Elegant Solution, Checkbox Ajax Update In Gridview

Hey all

I’m building a customized gridview with a lot of checkboxed…

I finally made this work - BUT… I’m pretty sure it’s a kind of a “hack” solution and not that elegant… Other than that, it takes about 2 sec from when i push the checkbox to the gridview is updated.

I would very much like to speed this process up :)

So here is my codes…

Please let me know what i can do to speed it all up:

Controller:




	public function actionCheckboxUpdate()

	{

		$model = RoskildeVagtTildeling::model()->findByAttributes(array('userId'=>$_POST['userId'],'vagtId'=>$_POST['vagtId']));

		

		if($model===null)

		{

			$modelForm=new RoskildeVagtTildeling;

			$modelForm->userId = $_POST['userId'];

			$modelForm->vagtId = $_POST['vagtId'];

			$modelForm->save(false);

		}

		else

		{

			$model->delete();

		}

		Yii::app()->end();

	}



My gridview in view file:




$this->widget('bootstrap.widgets.TbGroupGridView', array(

	'id'=>'Roskilde-grid',

	'dataProvider'=>$modelTilmeldte->scheduleList(),

	'enableSorting'=>false,

	'type'=>'striped bordered condensed',

	'template' => "{pager}\n{items}\n{pager}",

	'extraRowColumns'=> array('teams'),

	'extraRowExpression' => '"

		<h3 style=\"padding: 4px 5px; font-weight:bold;\">".orderTeam($data->teamId)."</h3>

		<td class=\"exstraRowTd\">x</td>

		<td class=\"exstraRowTd\">x</td>

	"',

	'extraRowHtmlOptions' => array('style'=>'float:left;width:260px;background-color:#e5e5e5;'),

	

	'selectableRows'=>0,

	'columns'=>$gridColumns,

));



And an example of some of the gridcolumns (these ones are made dynamically):




	function vagtChecked($userId, $vagtId){ if(RoskildeVagtTildeling::model()->findAll(array('condition'=>'userId=:userId AND vagtId=:vagtId','params'=>array(':userId'=>$userId,':vagtId'=>$vagtId)))){ return true; } else { return false; } }

	function vagtAfmeldt($userId, $vagtId){	if(preg_match("/\b".$vagtId."\b/", RoskildeTilmelding::model()->findByAttributes(array('id'=>$userId))->vagtAfmeld)){return true;}else{return false;}}

	

	foreach($modelVagter as $key => $value){

		

		$gridColumns[] = array(

			'name' => 'vagt',

			'type' => 'raw',

			'header' => '

				<div class="odd">'.$value->vagtId.'</div>

				<div class="even">'.$value->vagtDatoDag.'</div>

				<div class="odd">'.$value->vagtleder.'</div>

				<div class="even">'.$value->vagtPladser.'</div>

				<div class="odd" style="border-bottom:0;">'.$value->vagtPersoner.'</div>

			',

			'value' => '

				CHtml::checkBox("vagtId[]", vagtChecked($data->user->id,"'.$value->id.'"), array(

					"value"=>"'.$value->id.'",

					"id"=>"userId".$data->user->id."_vagtId'.$value->id.'",

					"disabled"=>vagtAfmeldt($data->id,"'.$value->vagtId.'"),

					"ajax" => array(

						"type"=>"POST", 

						"url"=>"'.Yii::app()->createUrl("/roskildeSchedule/checkboxUpdate").'",

						"data" => array(

							"userId"=>$data->user->id,

							"vagtId"=>"'.$value->id.'",

						),

						"success" => "js:function(html){

							$.fn.yiiGridView.update(\"Roskilde-grid\");

						}",

						"error"=>"function (xhr, ajaxOptions, thrownError){

							alert(xhr.statusText);

                            alert(thrownError);

						}",

						

            		),

				))

			',

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

			'htmlOptions' =>array('style'=>'text-align:center; width:50px; '),

		);

	}



So short story… When a checkbox is pushed a new row in table are created, or deleted if already exist…

Would be very nice if anyone could help me tidy this up a bit :)

Hi, I have the same problem, did you find any better solution ? Or maybe someone else could help ?

No I havent found any better solution yet…

It’s working as it is, but as i pointed out -> there must be a better and more smooth way to do this :)

bump again… :confused:

Thank you very much casperskou, you’re a life saver!!