4

I'm showing a CGridView for another related model in view&id=n page. The necessary relations are included in model files and everything works just fine. The only thing is that the buttons in the CButtonColumn are linked to the appropriate actions of the model which page is being opened, while I want them to link to the actions of the related model.

To explain clear what I mean, here is my code. In view.php of 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 to link to the appropriate actions for OrderContents model, while there are now linked to the actions of Order model. Is there any easy way to achieve this? I checked the API for both CButtonColumn and CGridView to see if I can get any inspiration there but had no luck.

Kai
  • 38,985
  • 14
  • 88
  • 103
Azimuth
  • 2,599
  • 4
  • 26
  • 33

3 Answers3

3

Actually, you don't even need to create a custom button if all you want to do is change the urls. Look at viewButtonUrl, updateButtonUrl and deleteButtonUrl for CButtonColumn.

You'd adjust the urls as shown the other answer

acorncom
  • 5,975
  • 1
  • 19
  • 31
2

Here's an example use of the viewButtonUrl attribute with CButtonColumn. I didn't find it straightforward figuring out how to provide the custom URL, but with a little wrangling I eventually got it. I thought I'd share it for others who might see this thread. Note that the PHP to generate 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's a better way to do this out there. I'd love to see!

nikhiltri
  • 44
  • 8
  • +1 Cleaner solution by directly setting url inside viewButtonUrl, updateButtonUrl and deleteButtonUrl with CButtonColumn. – phemios Oct 11 '13 at 14:31
1

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
     ),

Complete parameter list can be found here.

adamors
  • 2,672
  • 5
  • 32
  • 45
  • any idea what should be the `url`? Should I use absolute URL? And where are the default icons for buttons are located? – Azimuth Jun 14 '12 at 11:38
  • `url` should be something like `Yii::app()->createUrl("controller/action")`. The default icons are loaded into the assets folder, but you can use any url for that, as long as it is absolute – adamors Jun 14 '12 at 11:42
  • Thanks. Will need to pass the id as well. Any idea how to get it? I mean `'url'=>'Yii::app()->createUrl("controller/action&id=somehow_get_current_id")'` – Azimuth Jun 14 '12 at 11:51
  • You can do `Yii::app()->createUrl("controller/action", array("id"=>$data->id)` if your model has an `id` property. `$data` represents your model when CGridView is parsing. – adamors Jun 14 '12 at 11:55