我是Yii的初学者,并尝试了解CGridView小部件。
首先,我理解所有这些代码:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
"url",
"allowRedirect",
array(
"name"=>"Kod",
"value"=>array($model, "returnHTTPCode"),
"type"=>"html"
),
array(
"class"=>"CButtonColumn",
"template"=>"{update} {delete}",
),
),
));
现在我想将删除按钮的网址更改为http://domain.com/?r=pano/deleteSite
。 In documentation,有一个deleteButtonUrl属性,我正在尝试使用它
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
"url",
"allowRedirect",
array(
"name"=>"Kod",
"value"=>array($model, "returnHTTPCode"),
"type"=>"html"
),
array(
"class"=>"CButtonColumn",
"template"=>"{update} {delete}",
"deleteButtonUrl"=>"deleteSite"
),
),
));
但我收到Use of undefined constant deleteSite - assumed 'deleteSite'
错误。
然后我尝试使用CButtonColumn的buttons
属性。
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
"url",
"allowRedirect",
array(
"name"=>"Kod",
"value"=>array($model, "returnHTTPCode"),
"type"=>"html"
),
array(
"class"=>"CButtonColumn",
"template"=>"{update} {delete}",
"buttons"=>array(
"delete"=>array(
"url"=>"http://localhost/sitelerimacikmi/index.php?r=panos"
)
),
),
),
));
并得到同样的错误。
如何使用CButtonColumn的属性?
注意,英语不是我的母语,所以请尽可能使用简单的语法。
答案 0 :(得分:1)
您可以尝试使用:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
"url",
"allowRedirect",
array(
"name"=>"Kod",
"value"=>array($model, "returnHTTPCode"),
"type"=>"html"
),
array(
"class"=>"CButtonColumn",
"template"=>"{update} {delete}",
"deleteButtonUrl" => "Yii::app()->createUrl('pano/deleteSite')" // <- changes here
),
),
));
另外,您可以阅读这篇文章:http://www.yiiframework.com/wiki/106/using-cbuttoncolumn-to-customize-buttons-in-cgridview/
答案 1 :(得分:0)
如果需要指定URL,则必须将其指定为PHP表达式。以下是CButtonColumn
类中删除URL的定义默认值
public $deleteButtonUrl = 'Yii::app()->controller->createUrl(
"delete",array("id"=>$data->primaryKey)
)';
使用php eval函数评估此url,在此表达式中$data
是正在呈现的当前模型。
您应该使用:
$this->widget('zii.widgets.grid.CGridView', array(
...
'columns'=>array(
...
array(
'class'=>'CButtonColumn',
'deleteButtonUrl' => 'Yii::app()->controller->createUrl(
"pano/deleteSite", array("id" => $data->primaryKey)
)',
),
),
);