CButtonColumn for another model in CGridView

I am showing a CGridView for another related model in the view&id=n page. The necessary relationships are included in the model files, and everything works fine. The only thing is that the buttons in CButtonColumn are associated with the corresponding actions of the model whose page opens, and I want them to be associated with the actions of the corresponding model.

To explain what I mean, here is my code. In the view.php the Order model:

 $dataProvider=new CActiveDataProvider('OrderContents', array( 'criteria'=>array( 'condition'=>'order_id='.$model->id, 'with'=>array('order'), ), 'pagination'=>array( 'pageSize'=>20, ), )); $this->widget('zii.widgets.grid.CGridView', array( 'id'=>'orders-contents-grid', 'dataProvider'=>$dataProvider, 'columns'=>array( 'id', 'comp_name', 'quantity', 'comment', array( 'class'=>'CButtonColumn', ), ), )); 

Thus, I want the buttons in CButtonColumn refer to the corresponding actions for the OrderContents model, and now they are associated with the actions of the Order model. Is there an easy way to achieve this? I checked the API for both CButtonColumn and CGridView to see if I could get inspiration there, but no luck.

+4
source share
3 answers

In fact, you don’t even need to create a custom button if all you want to do is change the URLs. See viewButtonUrl , updateButtonUrl and deleteButtonUrl for CButtonColumn.

You must configure the urls as shown in another answer

+3
source

Here's an example of using the viewButtonUrl attribute with CButtonColumn. I did not find this straightforward, figuring out how to provide a custom URL, but with a bit of controversy, I ended up getting it. I thought I would share it with others who can see this topic. Note that PHP for generating the URL is passed as a string. Really:

  $this->widget('zii.widgets.grid.CGridView', array( 'id'=>'artwork-grid', 'dataProvider'=>$dataProvider, 'columns'=>array( 'id', 'artwork_id', 'description', array( 'class'=>'CButtonColumn', 'viewButtonUrl'=>'Yii::app()->createUrl(\'admin/artwork/\'. $data->id)', 'updateButtonUrl'=>'Yii::app()->createUrl(\'admin/artwork/update/\'. $data->id)', 'deleteButtonUrl'=>'Yii::app()->createUrl(\'admin/artwork/delete/\'. $data->id)', ), ), )); 

Maybe there is a better way to do it there. I would love to see!

+2
source

Create a custom button.

 array( 'class'=>'CButtonColumn', 'buttons'=>array( 'myButton'=>array( 'label'=>'label of the button', //hover text 'imageUrl'=> 'link to an image',//icon of the button 'url'=>'Yii::app()->createUrl("controller/action")', //target of the button ), 'template'=>'{myButton}' //and others ), 

A complete list of options can be found here .

+1
source

All Articles