0

我正在研究CGridView在 Wii 框架的小部件上显示/隐藏特定列数据。

我有一个CButtonColumn包含 3 个按钮的。但是,在某些情况下,我想为特定行显示不同的内容。我有 3 个不同的条件来确定特定行的显示内容。

以下说明了我想要做的事情:

| 1 | Title A | [hide][view][update]            <-- if (condition == 'a')
| 2 | Title B | [hide][view][update]            <-- if (condition == 'a')
| 3 | Title C | display text or link or button  <-- if (condition == 'b')
| 4 | Title D | display alternative buttons     <-- if (condition == 'c')

我在这里采取的最佳方法是什么?

我不能'visible'=> $model->processingStatus != "processed"在列上使用,因为这会删除整个列。我需要定位每一行。

我应该'visible'在每个单独的按钮上使用参数吗?我已经使用下面注释掉的代码尝试了这个,但它破坏了页面。仅供参考:我已经成功尝试了 CButtonColumn 本身的“可见”参数,但这不是我需要的。另外不确定它正在读取哪一行的状态。

或者我应该向控制器添加一个功能?让它执行 if/else 语句并返回要显示的内容。这将如何工作?

这是我的代码:

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'my-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(

        array(
            'name'=>'myid',
            'header'=>'ID',
            ),

        'Title',

        array(
            'class'=>'CButtonColumn',
            'visible'=> $model->status != "done",
            'template'=>'{hide}{view}{update}',
            'buttons'=>array(
                'hide'=>array(
                    'label'=>'Hide',                                                    //Text label of the button.
                    'imageUrl'=>Yii::app()->request->baseUrl . '/img/icons/bulb-off.png'    //Image URL of the button.
                    //'click'=>'function(){alert("Toggle Hide!");}',                    //A JS function to be invoked when the button is clicked.
                    //'options'=>array(),               //HTML options for the button tag.
                    //'url'=>'javascript:void(0)',              //A PHP expression for generating the URL of the button.
                    //'visible'=> $model->status == "done",     //A PHP expression for determining whether the button is visible.
                ),
                'view'=>array(
                    //Text label of the button.
                    'label'=>'View',
                    //Image URL of the button.
                    'imageUrl'=>Yii::app()->request->baseUrl . '/img/icons/view-record.png'
                ),
                'update'=>array(
                    'label'=>'Update/Edit',
                    'imageUrl'=>Yii::app()->request->baseUrl . '/img/icons/edit-pencil.png',
                    'url'=>'Yii::app()->createUrl("metadataandchapters/create?bookid=" . $data->bookid)',
                )
            )
        )
    )
)); ?>

希望我在这里有足够的意义!

4

2 回答 2

1

您应该使用visible按钮选项,但它应该是一个 PHP 表达式字符串,例如:

'visible'=> '$data->status == "done"',

http://www.yiiframework.com/doc/api/1.1/CButtonColumn#buttons-detail

于 2014-03-11T17:53:30.273 回答
0

使用您自己的类扩展 CButtonColumn,然后您应该能够将此函数更改为您需要呈现或隐藏按钮或进行任何更改的任何内容。

/**
 * Renders a link button.
 * @param string $id the ID of the button
 * @param array $button the button configuration which may contain 'label', 'url', 'data-icon', 'imageUrl' and 'options' elements.
 * @param integer $row the row number (zero-based)
 * @param mixed $data the data object associated with the row
 */
protected function renderButton($id, $button, $row, $data)

有关该功能的更多详细信息http://www.yiiframework.com/doc/api/1.1/CButtonColumn#renderButton-detail

于 2014-03-11T21:56:18.557 回答