简体   繁体   中英

How to put contrasting information into a CGridView column based on a condition?

I'm looking into showing/hiding specific column data on a CGridView widget for the Wii Framework.

I have a CButtonColumn which contains 3 buttons. However, on certain conditions, I want to display something different for a particular row. I have 3 different conditions which determin what gets displayed for particular row.

The following illustrates what I want to do:

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

What is my best approach to take here?

I can't use 'visible'=> $model->processingStatus != "processed" on the column because this will remove the whole column. I need to target each row insatead.

Should I use the 'visible' parameter on each individual button? I have tried this using the commented out code below but it breaks the page. FYI: I have successfully tried the 'visible' parameter on the CButtonColumn itself, but its not what I need. Plus not sure which row's status it is reading.

Or should I add a function to the controller? Have it do the if/else statements and return back what is to be displayed. How would this work?

Here is my code:

<?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)',
                )
            )
        )
    )
)); ?>

Hope I am making good enough sense here!

You should use visible button option, but it should be a PHP expression string, eg :

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

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

Extend CButtonColumn with your own class, then you should be able to change this function to whatever you need to render or hide buttons or do any changes you want.

/**
 * 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)

More details about the function http://www.yiiframework.com/doc/api/1.1/CButtonColumn#renderButton-detail

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM