Rudra
Rudra

Reputation: 739

How to use radiobuttons to modify the columns of Gridview in Yii

I have a gridview(admin.php) of test table. Here, I have two colums Contact Approval and Greetings Approval. I need to put two radio buttons ('Contact' and 'Greetings')in admin page. When I click on 'Contact' radio button all the columns except for Greetings Approval should be displayed in gridview and When I click on 'Greetings' radio button all the columns except for Contact Approval should be displayed in gridview. The update.php should also have the same effect.i.e when I click on edit option the update.php should have the fields based on the radiobutton data.How can I do this. enter image description here

Below is the code of radiobuttonlist

    <div class="ContactGreetingGroup">        
<?php echo CHtml::radioButtonList('approval','',array('0'=>'Greetings Approval','1'=>'Contact Approval'),array(
    'labelOptions'=>array('style'=>'display:inline'),
    'onclick' => 'this.form.submit()', 
    'separator'=>''));     
?>
   </div>

Upvotes: 1

Views: 1018

Answers (1)

ippi
ippi

Reputation: 10167

CDataColumn has a 'visible' property. You just need to set it depending on your parameter.

$approval = Yii::app()->request->getParam('approval');

'columns'=>[
    [
       'name'    => 'greetings',
       'visible' => ($approval)? false : true,
    ],
    [
       'name'    => 'contact',
       'visible' => ($approval)? true : false,
    ],
],

As for changing the behavior of the edit button, (since you need a parameter to say if you want "contact-mode" or "greetings-mode") You must overwrite the view-buttons url property.

[
    'class'=>'CButtonColumn',
    'buttons'=>[
        'view' => [
            'url'=>'Yii::app()->createUrl(
                "Something/Update", 
                ["id"=>$data->id, "approval"->$approval ])',
        ],
    ],
],

After that you need to make a similar check in your update.php (_form.php) to decide what to display.

if( $approval ) :
....

Added:

If your radiobuttonlist is inside form-tags, you can submit it like this:

<form>  <!-- You need a form to use form.submit! -->
    <?php echo CHtml::radioButtonList('approval','',
        array('0'=>'Greetings Approval','1'=>'Contact Approval'),
        array('labelOptions'=>array('style'=>'display:inline'),
        'onclick' => 'this.form.submit()',   // Added this.
        'separator'=>'')
); ?>
</form>

I also edited the above code to match your variable names. I haven't tested it, but it should give you an idea I hope.

Upvotes: 1

Related Questions