I have a model where you can select some tasks of a list and for each task a company.
What I’ve done is a CGridView with a CheckBox column and a DropDown. This is the _form:
$criteria = new CDbCriteria();
//Queremos mostrar las Acciones Promocionadas de este año
$criteria->condition = "ano=$añoActual";
$datos=new CActiveDataProvider('AccionPromocionada', array('criteria'=>$criteria));
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$datos,
'columns'=>array(
array('class'=>'CCheckBoxColumn',
'header'=>'',
'checked'=>'',
'selectableRows'=>8,
'id'=>'accionPromocionada',
),
array('header'=>'Acciones promocionadas',
'name'=>'descripcion',
'value'=>'$data->descripcion'),
//Agregamos la columna con todas las empresas homologadas. ->DropDown
array('name' =>'Empresa Seleccionada',
'type' => 'raw',
'value'=>function($data) {
return '<div id="homologada[]">'.CHtml::dropDownList("empresaHomologada[]","empresaHomologada",CHtml::listData(EmpresaHomologada::model()->findAll(),"id", "nombre")).'</div>';
},
))));
In the Create action there is no problem: I can get the checked tasks and selected companys; but in the Update, I don’t know how to check the selected items and the dropdown.
If I could update with the actual model it would be great, but I think I could change to a CheckBoxList or ActiveCheckboxList (I don’t know the difference).
I found a solution for the first question: select the items in the update.
<?php
$arraySolicitadas="";
//Queremos mostrar las Acciones Promocionadas de este año
$datos=new CActiveDataProvider('AccionPromocionada', array('criteria'=>array('condition'=>'ano='.$añoActual)));
//Si estamos haciendo una Actualizacion, obtenemos una condición para marcar las seleccionadas,
//accediendo a la tabal de AccionesSolicitadas.
if (!$model->isNewRecord){
$criteria = new CDbCriteria();
$criteria->condition = 'solicitudId='.$model->id;
$accionesSolicitadas = AccionSolicitada::model()->findAll ($criteria);
//Creamos un array para poder comparar en el CGridView
$arraySolicitadas = CHtml::listData( $accionesSolicitadas, 'id', 'accionPromocionadaId');
}
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$datos,
'columns'=>array(
array('class'=>'CCheckBoxColumn',
'header'=>'',
'checked'=>function($data) use($arraySolicitadas) {
return in_array($data->id, (array)$arraySolicitadas);},
'selectableRows'=>8,
'id'=>'accionPromocionada',
),
array('header'=>'Acciones promocionadas',
'name'=>'descripcion',
'value'=>'$data->descripcion'),
//Agregamos la columna con todas las empresas homologadas.
array('name' =>'Empresa Seleccionada',
'type' => 'raw',
'value'=>function($data) {
return '<div id="homologada[]">'.CHtml::dropDownList("empresaHomologada[]","empresaHomologada",CHtml::listData(EmpresaHomologada::model()->findAll(),"id", "nombre"), array('prompt'=>'')).'</div>';
},
))));
?> <!-- Fin listado Acciones Promocionadas -->
Now I’m going to prove the second point: select the correct company in the DropDown List.
One solution I think could solve this is using AfterRender o AfterAction: after generate the view code with the DropDown, set the selected items where needed using javascript.
Another solution I could do is changing CGridView and using other different widget or more flexible construction.
Which I finally want is a list of selectable actions and for each action you can pick into a list of options. My problem is with the Update. Here I show a screen.
I was thinking about YiiBooster and its TbEditableColumn using, but I couldn’t see how obtain the selected values in the update, so finally I wrote the code.