JToggleColumn
Column for CGridView which toggles the boolean ( TINYINT(1) ) value of model attribute. Tested with Yii 1.10.
Example

History
24.04.2012 - first release
25.04.2012 - added filter option and is now using assets
17.06.2012 - added ability to change action(two included: toggle(default) and switch), now using CActions
17.09.2012 - fixed bug with sorting, now sorts column in ajax way
19.02.2013 - New QtoggleAction allow toggles between range value!!!
Tutorial
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 CGridView widget:
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'language-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'name',
'lang_code',
'time_format',
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'=>'is_default', // boolean model attribute (tinyint(1) with values 0 or 1)
'filter' => array('0' => 'No', '1' => 'Yes'), // filter
'action'=>'switch', // other action, default is 'toggle' action
'checkedButtonLabel'=>'/images/toggle/yes.png', // Image,text-label or Html
'uncheckedButtonLabel'=>'/images/toggle/no.png', // Image,text-label or Html
'checkedButtonTitle'=>'Yes.Click to No', // tooltip
'uncheckedButtonTitle'=>'No. Click to Yes', // tooltip
'labeltype'=>'image',// New Option - may be 'image','html' or 'text'
'htmlOptions'=>array('style'=>'text-align:center;min-width:60px;')
),
array(
'class'=>'CButtonColumn',
),
),
));
?>
Add action(s) in your controller:
<?php
public function actions(){
return array(
'toggle'=>'ext.jtogglecolumn.ToggleAction',
'switch'=>'ext.jtogglecolumn.SwitchAction', // only if you need it
'qtoggle'=>'ext.jtogglecolumn.QtoggleAction', // only if you need it
);
}
?>
Don't forget to add this action to controllers accessRules:
<?php
public function accessRules()
{
return array(
array('allow',
'actions'=>array('toggle','switch','qtoggle'),
'users'=>array('admin'),
)
);
}
?>
New Qtoggle usage demo:
<?php $this->widget('zii.widgets.grid.CGridView', array( 'id'=>'language-grid', 'dataProvider'=>$model->search(), 'filter'=>$model, 'columns'=>array( 'id', 'name', 'lang_code', 'time_format', array( 'class'=>'JToggleColumn', 'action'=>'qtoggle', 'name'=>'answer', 'filter' => array('no' => 'No', 'yes' => 'Yes','dn' => 'Don`t know'), 'queue'=>array('no' => 'No', 'yes' => 'Yes','dn' => '???'),//key-label pairs 'queueTitles'=>array('no' => 'No', 'yes' => 'Yes','dn' => 'I don`t know'),//key-tooltips pairs 'labeltype'=>'html', 'htmlOptions'=>array('style'=>'text-align:center;min-width:60px;') ), array( 'class'=>'JToggleColumn', 'filter' => array('0' => 'Draft', '1' => 'Premod',2=>'Public','3'=>'Archive'), // filter 'name'=>'status', 'action'=>'qtoggle', 'queue'=>array('0' => '/images/toggle/draft.png', '1' => '/images/toggle/premod.png',2=>'/images/toggle/pub.png','3'=>'/images/toggle/arch.png'), 'queueTitles'=>array('0' => 'Draft. Toggle to premod', '1' => 'Premod. Toggle to public',2=>'Public. Toggle to archive','3'=>'Archive. Toggle to draft'), 'labeltype'=>'image', 'htmlOptions'=>array('style'=>'text-align:center;min-width:60px;') ), array( 'class'=>'JToggleColumn', 'filter' => array('0' => 'Draft', '1' => 'Premod',2=>'Public','3'=>'Archive'), // filter 'name'=>'varsel', 'action'=>'qtoggle', 'queueType'=>'select', //Show all options in dropdownlist 'queue'=>array('0' => 'Opt1', '1' => 'Opt2',2=>'Opt3','3'=>'Opt4'), 'queueTitles'=>array('0' => 'Opt1', '1' => 'Opt2',2=>'Opt3','3'=>'Opt4'), 'labeltype'=>'text', 'htmlOptions'=>array('style'=>'text-align:center;min-width:60px;') ), array( 'class'=>'CButtonColumn', ), ), )); ?>
Total 13 comments
In line 330:
if($this->action==qtoggle AND $this->queueType=='select')
should be:
if($this->action=='qtoggle' AND $this->queueType=='select')
@Patrigan the switch action doesn't do anything special. I'll give you an example; if you need to have boolean column and you would like only one row to be active, while others unactive, you'll use switch action. For example you have language table and column "is_default"; only one language can be default right, so you'll use this action (switch). When you check other language to be default, the default one would reset. If you would use toggle action you could potentionaly have, for example, three default languages. I hope I explained well, sorry for bad english ;)
Can you add an example of what the switch action precisely does?
Thanks, I'm glad you like it ;) I am quite busy, but I'll see what I can do! My code is at bitbucket, so if you anyone have time, please contribute ;)
I love this little extension, easy to work with. However, I do have one request, could there be any way to have a more flexible primary key detection/selection? (and with that, also taking away the requirement of activeDataProvider)
If you are experiencing validation errors in a JS alert box, you'll have to bypass validation. in the function actionToggle() replace:
by:
i can set $attribute at all of fields, and save 0 or 1 there. It's no good.
@nizsheanez
This isn`t bug of extension. You have to create some different scenarios for User model However you can try
so. i kill your password with it
@CyberMama Thanks! Forgot to update, fixed :)
Great idea! But endorse improvments of P**awel Kania **
I made small improvements:
I added actionToggle to Controller class (all my controllers extends this class) and I changed it to:
JToggleColumn.php
In admin column:
Now it's universal for different models.
Second improvement:
admin:
JToggleColumn.php
Now we can use filters.
Ahaaa...nice extension.. great works, master Yii...
Leave a comment
Please login to leave your comment.