Yii 1.1: Change buttons on our CGridView extending yii

8 followers

In this tip, I'll help you to change in few second buttons of your CGridView. I hope you like it =).

This is our standard grid created by yii (with gii, or console, ...)

$this->widget('zii.widgets.grid.CGridView', array(
        'id' => 'standard-grid',
        'dataProvider' => $model->search(),
        'filter' => $model,
        'columns' => array(
            'id',
            'field',
            array(
                'class' => 'CButtonColumn',
            ),
        ),
    ));

What if we want to easy upgrade columns of all our grid? we can extend CButtonColumn with a new class. We can create this class and call it MyCButtonColumn (for example). Then change column class in our CGridView:

$this->widget('zii.widgets.grid.CGridView', array(
        'id' => 'standard-grid',
        'dataProvider' => $model->search(),
        'filter' => $model,
        'columns' => array(
            'id',
            'field',
            array(
                'class' => 'MyCButtonColumn',
            ),
        ),
    ));

Second, create MyCButtonColumn class in /protected/components/MyCButtonColumn.php

class MyCButtonColumn extends CButtonColumn {
        public $template = '{update} {delete}';
    }

In this example, we simply replace standard template deleting '{view}' button. But what if you want hide delete button in 21 december of 2012?

class MyCButtonColumn extends CButtonColumn {
 
        protected function checkButtons($data, $id) {
            return !(date('Y-m-d') == '2012-12-21' && $id == 'delete');
        }
 
        protected function renderDataCellContent($row, $data) {
            $tr = array();
            ob_start();
            foreach ($this->buttons as $id => $button) {
                if ($this->checkButtons($data, $id)) {
                    $this->renderButton($id, $button, $row, $data);
                }
                $tr['{' . $id . '}'] = ob_get_contents();
                ob_clean();
            }
            ob_end_clean();
            echo strtr($this->template, $tr);
        }
 
    }

Total 5 comments

#13866 report it
Ivo Ribeiro at 2013/07/03 06:17am
Cgrid Desepear

I have a cgridview in a view from another model, its working fine but when i press the delete button the grid desapear, what i should do?

#7886 report it
sensorario at 2012/04/24 07:04pm
Ok now this code is useful

I've added a snippet of code. I hope you like it.

#7452 report it
alex.p at 2012/03/23 06:57am
even simpler way to remove {view} button

i do not know i am no expert in yii both solution illustrated bellow will work but is is more simple to use widget skins: a set of predefined options for widgets here is the full tutorial Widget skins

#6272 report it
sensorario at 2011/12/23 04:45am
Are you sure?

The purpose of this wiki, is not to show you, how to change template. The purpose is to show you, how to create a class re-usable in all your CGridView and/or in all your yii application. You solution work only in one CGridView. You can create a lot of SpecialCButtonColumn AnotherSpecialCButtonColumn ... and so on. You can create them and re-use them. Just changing class name in you CGridView.

The mistake is mine: I do not explain this in my wiki. You can export environment, icons, all in one step: with your own class.

#6271 report it
Mariusz W. at 2011/12/23 04:39am
Simpler way to remove {view} button

You don't have to extend class to just remove this button. This code is enought:

$this->widget('zii.widgets.grid.CGridView', array(
....
array
(
    'class'=>'CButtonColumn',
    'template'=>'{update} {delete}',
)
....

The reason to extend CButtonColumn is adding new buttons. When we extend base class, we have clear code. Of course we don't have to extend. Read this wiki, if you want to add some buttons: http://www.yiiframework.com/wiki/106/using-cbuttoncolumn-to-customize-buttons-in-cgridview/

Leave a comment

Please to leave your comment.

Write new article