Simple class for displaying CGridView columns as select elements which update the model via ajax
- Only tested with Yii 1.1.13
- Requires jQuery 1.7+
- Add the class file to protected/extensions/ajaxSelectColumn
- Add the line below to your config file in the import section: 'application.extensions.ajaxSelectColumn.*',
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
...
array (
'class' => 'AjaxSelectColumn',
'header' => 'Category',
'name' => 'catid',
'url' => array('photo/updateColumn'),
),
...
),
));
When you change the selected value, four values will be posted to the URL:
- id: the primary key of the model
- class: the model's class, capitalized (ignore if not needed)
- name: the name of the model's attribute
- value: the new value of the input field
You can then use the values in your controller action to update the model. Here's an example of how you could do that, although purification is recommended:
public function actionUpdateColumn()
{
$id = (int)$_POST['id'];
$class = $_POST['class'];
$name = $_POST['name'];
$value = $_POST['value'];
$model = $class::model()->findByPk($id);
$p = new CHtmlPurifier();
$model->{$name} = $p->purify($value);
$model->update();
}