Yii Framework Forum: Elegant Solution, Checkbox Ajax Update In Gridview - Yii Framework Forum

Jump to content

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

Elegant Solution, Checkbox Ajax Update In Gridview Rate Topic: -----

#1 User is offline   casperskou 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 06-April 12

Posted 11 May 2013 - 10:43 AM

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 :)
0

#2 User is offline   mirunho 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 170
  • Joined: 19-December 12
  • Location:Gdansk, Poland

Posted 13 May 2013 - 03:51 AM

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

#3 User is offline   casperskou 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 06-April 12

Posted 14 May 2013 - 06:26 AM

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 :)
0

#4 User is offline   casperskou 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 06-April 12

Posted 06 June 2013 - 06:53 AM

bump again.. :/
0

#5 User is offline   Thiago Arruda 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 27-May 14

Posted 27 May 2014 - 12:15 PM

Thank you very much casperskou, you're a life saver!!
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