PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, February 20, 2022

[FIXED] Hiding column's in YII CGridView

 February 20, 2022     html, php, yii     No comments   

Issue

I have a table with more than 5 columns , i want to hide some columns so that those column's are shown only if some row is selected or its expanded .

Am using yiiframework's CGridView , so how can i do this in that ?

Any help is appreciable ..

I want a feature like this, so that on expanding a particular record i can see the hidden column values

enter image description here


Solution

One way is :

 'columns'=>array(
        array(
          'name'=>'columnName',
          'visible'=>false
            ),
         )

So you need to manipulate visibility attributes dynamically :

 'visible'=>$this->checkVisible() //custom function 

sth like this depending on your requirement

Edit (using ajax +jquery)

example :views/user/admin.php

.....
.....
<?php
$toggleUDetails = <<<JS
 $('a.toggle').live('click',function(e){
    e.preventDefault();

    if(this.href.split('#')[1]=='loaded') return $(this).closest("tr").next('tr.toggle').toggle();

    trow=$(this).closest("tr");

   var ajaxOpts={type:"POST", url:this.href ,dataType:'json',success:function(data){
            $(trow).after(data.row);
      }
    };

   this.href=this.href+'#loaded';

   $.ajax(ajaxOpts);

  });
JS;
Yii::app()->clientScript->registerScript('toggleUD', $toggleUDetails, CClientScript::POS_READY); 

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'user-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
     array(
        'class'=>'CButtonColumn',
         'header'=>'Toggle Details',
          'template'=>'{toggle}',
            'buttons'=>array(
              'toggle'=>array(
                        'label'=>'Details',                        
                             'imageUrl'=>Yii::app()->request->baseUrl.'/images/expand.png',  
                             'url'=>'Yii::app()->createUrl("user/getExtra", array("id"=>$data->id))',
                             'options'=>array('class'=>'toggle',

                                      ),
                               ),
                        ),
          ),

        'id',
        'username',
        'password',
        'email',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); ?>

User Controller

public function actionGetExtra($id){
        $model=User::model()->findByPk($id);
        echo json_encode(array('row'=> '<tr class="toggle"><td   colspan="6">'. $model->username.'</td></tr>'));

  }

Enable access rights:

array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions'=>array('create','update','getExtra'),
                'users'=>array('@'),
            ),

that much I can do you for . Remember to change the Java script function to toggle the image icon I have not done that



Answered By - sakhunzai
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing