2012-11-21 114 views
2

听起来很简单吧?我搜索了高和低,我无法找到如何做到这一点。我有一个CGridView:如何添加一个按钮到CGridView?

$dataProvider = new CArrayDataProvider ($auctions); 
$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider, 
    'columns'=>array(
    'id::ID', 
    'product.title::Title', 
    'state::Status', 
), 
)); 

我想添加第四列,只包含一个简单的按钮,将按下时将执行JavaScript。我已经试过:

array(
    'class' => 'CButtonColumn', 
), 

这只是给了我一个错误:

Undefined property: stdClass::$primaryKey 

任何想法?

+0

你还可以粘贴堆栈跟踪吗? –

回答

8

试试这个:

$dataProvider = new CArrayDataProvider ($auctions); 
$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$dataProvider, 
    'columns'=>array(
    'id::ID', 
    'product.title::Title', 
    'state::Status', 
    array(
     'type' => 'raw', 
     'value' => '<button onclick=\'alert("It works!")\' value="clickme"/>' 
    ) 
), 
)); 
7

最好的方式做到这一点是CButtonColumn ::模板和CButtonColumn ::按钮。

array(
    'class' => 'CButtonColumn', 
    'template' => '{view} {update} {delete} {copy}', 
    'buttons'=>array(
     'copy' => array(
      'label'=>'Copy', // text label of the button 
      'url'=>"CHtml::normalizeUrl(array('copy', 'id'=>\$data->id))", 
      'imageUrl'=>'/path/to/copy.gif', // image URL of the button. If not set or false, a text link is used 
      'options' => array('class'=>'copy'), // HTML options for the button 
     ), 
    ), 
), 

在这个例子中有三个默认的按钮和一个自定义的'复制'按钮。 如果您不想使用某些默认按钮(即查看,更新和删除),则可以将其删除。然后,定义您添加的按钮的属性。我定义了标签,网址,图片和html选项。

相关问题