Yii - How to customize a CGridView CbuttonColumn

Update - Delete - View

I'm sure you've already seen and used the basic buttons in a CGridView and maybe even used the template phrases to modify the behaviours


array(
     'class'=>'CButtonColumn',
     'template'=>'{delete}{update}',
     'buttons'=>array(
               'update'=>array(
                         'url'=>'$this->grid->controller->createUrl("/History/update", array("id"=>$data->primaryKey,"asDialog"=>1,"gridId"=>$this->grid->id))',
                         'click'=>'function(){$("#cru-frame").attr("src",$(this).attr("href")); $("#cru-dialog").dialog("open");  return false;}',
                         ),
               'delete'=>array(
                         'url'=>'$this->grid->controller->createUrl("/History/delete", array("id"=>$data->primaryKey,"asDialog"=>1,"gridId"=>$this->grid->id))',
                         ),
               ),

 

but sometimes we need to break out of this box and create custom buttons for slightly more exciting function like ordering with up and down buttons, or email, or add ... and many more!

Custom Buttons

So you can add custom templates (or re-define existing ones) by using further properties of the CButtonColumn object as follows:-


  array(
    'class'=>'CButtonColumn',
    'template'=>'{update}{add}{delete}',
    'buttons'=>array(
      'update'=>array(
          'url'=>'$this->grid->controller->createUrl("/Extras/update", array("id"=>$data->id,"asDialog"=>1,"gridId"=>$this->grid->id))',
          'click'=>'function(){$("#cru-frame").attr("src",$(this).attr("href")); $("#cru-dialog").dialog("open");  return false;}',
            'visible'=>'($data->id===null)?false:true;'
          ),
      'add' => array(
          'label'=>'Add',
          'imageUrl'=>Yii::app()->request->baseUrl.'/css/gridViewStyle/images/gr-plus.png',
          'url'=>'$this->grid->controller->createUrl("/Extras/create", array("eid"=>$data->extras_id, "bid"=>'.$model->id.', "asDialog"=>1,"gridId"=>$this->grid->id))',
          'click'=>'function(){$("#cru-frame").attr("src",$(this).attr("href")); $("#cru-dialog").dialog("open");  return false;}',
          'visible'=>'($data->id===null)?true:false;'
          ),
      'delete'=>array(
          'url'=>'$this->grid->controller->createUrl("/Extras/delete", array("id"=>$data->primaryKey,"asDialog"=>1,"gridId"=>$this->grid->id))',
          ),
    ),
  ),

Let's have closer look at how this works ... next »


Did you know you can hire me?

I take on projects of all sizes. From Consulting to large Development Projects.

If you're starting a new Yii project and would like some help to get setup and running or you need some help with a particular module or you just need someone to develop the whole dang thing, then just ask ...


Beginning Yii ...

I'm very excited about a new Yii Training course that has just been published.

The course is designed for anyone wanting to learn Yii, whether new to frameworks or converting from another.

Eight chapters, each having five videos of between 3 and 5 minutes, take you through the basics of the Yii Framework, building a web application step-by-step.

Save 40% - introductory offer $20.99 / £13.19 / €16.19 / A$20.99

Leave a Comment

twitterfacebookgooglelinkedin https://me.yahoo.com