Yii 1.1: xbuttoncolumn

extends CButtonColumn adding an extra button {yesno} for updating db status-type field in the way of {delete} button via ajax
7 followers

Update

  • version 1.1 April 21, 2012 - support fliping yes/no image
  • version 1.0.1 April 19, 2012 - original version

Scenario

when you simply want to flip a status in gridview with a button - eg. prove a comment or post in your blog;

when you want add row in gridview to your favorite under your profile - eg. bookmark;

inspired by reading this post and this post

Requirements

Tested under Yii 1.1.10

Install & Usage

1) unzip XButtonColumn.php and folder /XButtonColumn/ to /protected/components/ folder

2) in CGridView widget: change your CButtonColumn's class name to XButtonColumn & specify properties for this new button

version 1.0

<?php $this->widget('zii.widgets.grid.CGridView', array(
 
    'id' => 'mymodel-grid',
    'dataProvider' => $model->search(),
    'columns' => array(
        'id',
        'name',
        'yesno',
        array(
            'class' => 'XButtonColumn', //<--new XButtonColumn class
            'template' => '{view}{update}{delete}{yesno}', //<-- new button {yesno}
            'yesnoAction' => 'flag', //<--name the new button
            'yesnoButtonLabel' => 'flip flag', //<--label the new button
            /* 
            * more properties {yesno} button related
            * please refer to deleteButton in CButtonColumn for usage
            *
            'yesnoButtonImageUrl' => null,
            'yesnoButtonUrl'=>null, //default to yesnoAction
            'yesnoButtonOptions=>null;
            'yesnoConfirmation'=>null, //confirmation message
            'afterYesno'=>null, 
            */
 
        ),
    ),
)); 
 
?>

updated: version 1.1

<?php $this->widget('zii.widgets.grid.CGridView', array(
 
    'id' => 'mymodel-grid',
    'dataProvider' => $model->search(),
    'columns' => array(
        'id',
        'name',
        'yesno',
        array(
            'class' => 'XButtonColumn', //<--new XButtonColumn class
            'template' => '{view}{update}{delete}{yesno}', //<-- new button {yesno}
            'yesnoAction' => 'flag', //<--action for this button
            'yesnoValue' => '$data->yesno',
            //'yesValue' => '1',
            //'noValue' => '0',
            //'yesButtonLabel' => 'Flip to No',
            //'yesButtonImageUrl' => null,
            //'noButtonLabel' => 'Flip to Yes',
            //'noButtonImageUrl' => null,           
 
        ),
    ),
)); 
 
?>

3) Add new action in your controller to response to the new button

//assuming: model => Mymodel & you have a field in db.table named 'yesno' value 0 or 1
public function actionFlag($id) {
 
    //simplified version, add more code here please
    $model = $this->loadModel($id, 'Mymodel');
    $model->yesno = 1 - $model->yesno;
    $model->save();
}

Total 3 comments

#7841 report it
softark at 2012/04/22 03:33am
Yeah !

Looks good to me. (You're so fast, btw)

#7819 report it
rootbear at 2012/04/20 12:31am
hmmm

sounds reasonable, not something right out of my head. bet i need a bit more time to figure that out.

[updated] @softark, thanks for the request. i made the change to support flipping yes/no images. check it out.

#7817 report it
softark at 2012/04/19 09:14pm
Nice !

Nice extension.

Just a request: I'd rather want the 'yesno' button to show the current state. Maybe not a single property of 'yesnoButtonImageUrl', but a pair of properties : 'yesButtonImageUrl' and 'noButtonImageUrl'.

Leave a comment

Please to leave your comment.

Create extension