This topic to explain how I 've managed to:
in a gridview, for each row (model item), display a dropdownList (created from a xml file), and update the given model item via an ajax call while changing the dropdown.
A custom CGridColumn class:
Note: ‘#rem$row’ id is the one to be updated in the gridview.
Yii::import('zii.widgets.grid.CGridColumn');
class DropDownColumn extends CGridColumn
{
protected function renderDataCellContent($row,$data)
{
//building the ajax link and parameters
$ajax = array(
'type'=>'POST',
'url'=>'pdv/setRemorqueur',
'data'=>array('id_rem'=>'js:this.value','id_vol'=>$data->id),
"success"=> "function(responseText) {
$('#rem$row').html(responseText);
}"
);
//building options for the dropdown
($data->remorqueur!=null)?
$options=array('ajax'=>$ajax, 'class'=>'span2','id'=>'rem'.$row,'options'=>array($data->remorqueur=>array('selected'=>'selected')))
:$options=array('ajax'=>$ajax, 'class'=>'span2','id'=>'rem'.$row,'prompt'=>'Select');
// display the dropdown
echo CHtml::dropDownList('model', 'remorqueur', Vol::model()->getRemorqueurs(),$options);
}
}
In the gridview, for my column:
array(
'class'=>'DropDownColumn',
'header'=>'Remorqueur',
),
In the controller:
public function actionSetRemorqueur()
{
if(Yii::app()->request->isAjaxRequest)
{
//making the change
$id_vol=$_POST['id_vol'];
$id_rem=$_POST['id_rem'];
$model=$this->loadModel($id_vol);
$model->remorqueur=$id_rem;
$model->save();
//building the new dropdown from an xml file
$html='';
$aeronefs = simplexml_load_file('xml/aeronefs.xml');
foreach($aeronefs as $value)
{
if ((string) $value->remorqueur == '1' && $value->actif == '1')
{
if($value->id_aeronef != $id_rem)
{
$html=$html.'<option value="'.$value->id_aeronef.'">'.$value->immat.'</option>';
}
else $html=$html.'<option value="'.$value->id_aeronef.'" selected="selected">'.$value->immat.'</option>';
}
}
// render the updated dropdown
echo $html;
}
}
for a global use, I’ve imported my new DropDownColumn in the main config file in the “import” part.
All of this works fine, but I’m not sure if it fits the bests practices.
Next challenge: display the filter in the header to search through $model->remorqueur field … (help welcome ;-))