Column for CGridView which toggles the boolean ( TINYINT(1) ) value of model attribute. Tested with Yii 1.10.
www.yiiframework.com/extension/jtogglecolumn
Download: http://www.yiiframework.com/extension/jtogglecolumn/files/jtogglecolumn.zip
[size="5"]Usage[/size]
Extract downloaded zip to your components or extensions directory.
If you extracted to extensions directory add this line to import array in your /config/main.php :
<?php
'import'=>array(
...
'application.extensions.jtogglecolumn.*',
)
?>
Define a JToggleColumn in your CGrid widget:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'username',
'password',
'email',
'role',
array(
'class'=>'JToggleColumn',
'name'=>'is_active', // boolean model attribute (tinyint(1) with values 0 or 1)
'filter' => array('0' => 'No', '1' => 'Yes'), // filter
'htmlOptions'=>array('style'=>'text-align:center;min-width:60px;')
),
array(
'class'=>'JToggleColumn',
'name'=>'visible', // boolean model attribute (tinyint(1) with values 0 or 1)
'filter' => array('0' => 'No', '1' => 'Yes'), // filter
'checkedButtonImageUrl'=>'/auth/images/check.png', // checked image
'uncheckedButtonImageUrl'=>'/auth/images/uncheck.png', // unchecked image
'checkedButtonLabel'=>'Uncheck this', // tooltip
'uncheckedButtonLabel'=>'Check this', // tooltip
'htmlOptions'=>array('style'=>'text-align:center;min-width:60px;')
),
array(
'class'=>'CButtonColumn',
),
),
)); ?>
Create action in your controller:
public function actionToggle($id,$attribute)
{
if(Yii::app()->request->isPostRequest)
{
// we only allow deletion via POST request
$model = $this->loadModel($id);
$model->$attribute = !$model->$attribute;
$model->save();
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
else
throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
}
?>
Don’t forget to add this action to controllers accessRules.
This is my first extension.
All suggestions are welcome!
GIT Repository