You are viewing revision #4 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.
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);
}
}
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/
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.
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
Ok now this code is useful
I've added a snippet of code. I hope you like it.
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?
Thank you
Thx, it helped a lot
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.