我在view&id=n页面中显示了另一个相关模型的CGridView。必要的关系包含在模型文件中,一切工作正常。唯一的问题是,CButtonColumn中的按钮链接到正在打开的页面的模型的相应操作,而我希望它们链接到相关模型的操作。
为了清楚地解释我的意思,下面是我的代码。在Order模型的view.php中:
$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',
),
),
));因此,我希望CButtonColumn中的按钮链接到OrderContents模型的适当操作,而现在链接到Order模型的操作。有什么简单的方法可以做到这一点吗?我检查了CButtonColumn和CGridView的应用程序接口,看看是否能在那里获得任何灵感,但没有运气。
发布于 2012-06-14 23:11:59
实际上,如果您只想更改urls,则甚至不需要创建自定义按钮。看看用于CButtonColumn的viewButtonUrl、updateButtonUrl和deleteButtonUrl。
您可以按照另一个答案所示的方式调整urls
发布于 2012-11-27 05:48:03
下面是在CButtonColumn中使用viewButtonUrl属性的示例。我发现如何提供自定义URL并不简单,但经过一番争论,我最终得到了它。我想我应该把它分享给其他看到这个帖子的人。请注意,生成URL的PHP是作为字符串传递的。真的吗?
$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)',
),
),
));也许有一种更好的方法可以做到这一点。我很想看看!
发布于 2012-06-14 19:34:31
创建自定义按钮。
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
),完整的参数列表可以在here中找到。
https://stackoverflow.com/questions/11032058
复制相似问题