【问题标题】:How do you add a button to a CGridView?如何向 CGridView 添加按钮?
【发布时间】:2012-11-09 18:47:48
【问题描述】:

听起来很简单,对吧?我已经搜索了高低,我无法找到如何做到这一点。我有一个 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

有什么想法吗?

【问题讨论】:

  • 你也可以粘贴堆栈跟踪吗?

标签: yii cgridview cbuttoncolumn


【解决方案1】:

试试这个:

$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"/>'
     )
  ),
));

【讨论】:

    【解决方案2】:

    最好的方法是使用 CButtonColumn::template 和 CButtonColumn::buttons。

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

    在此示例中,有三个默认按钮和一个自定义按钮,即“复制”按钮。 如果您不想要某些默认按钮(即查看、更新和删除),您可以删除它们。然后,定义您添加的按钮的属性。我定义了 label、url、image 和 html 选项。

    【讨论】:

      猜你喜欢
      • 2021-04-19
      • 2015-03-29
      • 2011-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多