Yii 1.1: jtogglecolumn

Column for CGridView which toggles the boolean value of model attribute.
37 followers

JToggleColumn

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

Example

JToggleColumn

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!!!

04.07.2013 - Bug fix form KKK

GITHUB PAGE:
https://github.com/uniconstructor/jtogglecolumn

BITBUCKET PAGE: https://bitbucket.org/johonunu/jtogglecolumn/overview (less maintained for now)

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 17 comments

#15024 report it
Ravi Bhalodiya at 2013/09/30 02:48am
Jtoggle column with Giix
#13899 report it
__construct() at 2013/07/04 05:37pm
@johonunu

@johonunu

Oh, excuse me, I didn't noticed that your code is located in the bitbucket. I am subscribed to your bitbucket now and will keep my repo syncronized with yours.

#13898 report it
johonunu at 2013/07/04 05:08pm
@__construct()

@__construct()

Hi I didn't noticed tha last two comments on this page. I have a github account, but the source code is located at bitbucket: https://bitbucket.org/johonunu/jtogglecolumn/overview

Anyway I am quite busy, so I missed the last bug :( It is nice that you want to continue maintaining and improving my extension. Thnak you very much and keep up the good work ;)

P.S. I will add your github page in the info section.

#13897 report it
__construct() at 2013/07/04 03:52pm
GitHub page

Hello, johonunu.

Thank you for this extension.

Unfortunatly I was not found official github repository for this project, but I see so many code improvements here.

For now, I've created my own repo: https://github.com/uniconstructor/jtogglecolumn

I will try to add all bugfixes and improvements from this thread to here. Everyone: feel free to contribute.

#13100 report it
Kkk at 2013/05/04 09:14pm
BUG in JToggleColumn

In line 330:

if($this->action==qtoggle AND $this->queueType=='select')

should be:

if($this->action=='qtoggle' AND $this->queueType=='select')

#8673 report it
johonunu at 2012/06/18 04:54pm
Switch action

@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 ;)

#8666 report it
Patrigan at 2012/06/18 06:43am
Switch action

Can you add an example of what the switch action precisely does?

#8243 report it
johonunu at 2012/05/21 05:55am
@Patrigan

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 ;)

#8239 report it
Patrigan at 2012/05/21 03:13am
primaryKey

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)

#8126 report it
saegeek at 2012/05/12 03:43am
bypass validation

If you are experiencing validation errors in a JS alert box, you'll have to bypass validation. in the function actionToggle() replace:

$model->save();

by:

$model->save(false);
#7590 report it
nizsheanez at 2012/04/01 04:25pm
so

i can set $attribute at all of fields, and save 0 or 1 there. It's no good.

#7571 report it
Lastik at 2012/03/30 07:35pm
@nizsheanez

@nizsheanez

so. i kill your password with it

This isn`t bug of extension. You have to create some different scenarios for User model However you can try

$model = $this->loadModel($id);
$state = ($model->$attribute==0)?1:0;
$model->saveAttributes(array($attribute=>$state));
#7550 report it
nizsheanez at 2012/03/29 08:53am
so

so. i kill your password with it

$model = $this->loadModel($id);
$model->$attribute = ($model->$attribute==0)?1:0;
$model->save();
#7502 report it
johonunu at 2012/03/26 03:22pm
Thanks

@CyberMama Thanks! Forgot to update, fixed :)

#7501 report it
Lastik at 2012/03/26 03:11pm
Respect!!!

Great idea! But endorse improvments of P**awel Kania **

public function actionToggle($id,$attribute)
{
    if(Yii::app()->request->isPostRequest)
    {
        $model = $this->loadModel($id);
       $model->$attribute = ($model->$attribute==0)?1:0;
        $model->save();
 
        //.......
    }
#7478 report it
aquasite.pl at 2012/03/25 08:38am
very nice extension

I made small improvements:

I added actionToggle to Controller class (all my controllers extends this class) and I changed it to:

public function actionToggle($id, $attribute, $model) {
        if (Yii::app()->request->isPostRequest) {
            $model = $this->loadModel($id, $model);
            //loadModel($id, $model) from giix
            ($model->$attribute == 1) ? $model->$attribute = 0 : $model->$attribute = 1;
            $model->save();
... //

JToggleColumn.php

public $model;
 
//in protected function initDefaultButtons()
 
   $this->toggle_button = array(
            'url' => 'Yii::app()->controller->createUrl("toggle",array("id"=>$data->primaryKey,"attribute"=>"' . $this->name . '","model"=>"'.$this->model.'"))',
            'options' => array('class' => $this->name . '_toggle'),
        );

In admin column:

'model' => get_class($model),

Now it's universal for different models.

Second improvement:

admin:

'filter' => array('0' => Yii::t('app', 'No'), '1' => Yii::t('app', 'Yes'))

JToggleColumn.php

public $filter;
 
    protected function renderFilterCellContent() {
 
        if ($this->filter !== null) {
            if (is_string($this->filter))
                echo $this->filter;
            else if ($this->filter !== false && $this->grid->filter !== null && $this->name !== null && strpos($this->name, '.') === false) {
                if (is_array($this->filter))
                    echo CHtml::activeDropDownList($this->grid->filter, $this->name, $this->filter, array('id' => false, 'prompt' => ''));
                else if ($this->filter === null)
                    echo CHtml::activeTextField($this->grid->filter, $this->name, array('id' => false));
            }
            else
                parent::renderFilterCellContent();
        }
    }

Now we can use filters.

#7477 report it
Peter JK at 2012/03/25 07:54am
nice extension

Ahaaa...nice extension.. great works, master Yii...

Leave a comment

Please to leave your comment.

Create extension