Condition in CgridView Column

Hi, I want to add a condition in a column of CGridView

I want to add a function in which the column is visible depending on a condition.

If status is equal to 10 column display. If not equal to 10, not shown

This is the column




'imageUrl'=>Yii::app()->baseUrl . '/img/view.png',

'header'=>'View', 

'class'=>'CLinkColumn',

					            			                          'linkHtmlOptions'=>array('title'=>'View'),

'visible'=>false,  

Help, i cant add if condition y CGridview

Why can’t you add a condition?




array(

'imageUrl'=>Yii::app()->baseUrl . '/img/view.png',

'header'=>'View', 

'class'=>'CLinkColumn',

                                                                                                  'linkHtmlOptions'=>array('title'=>'View'),

'visible'=> '$data->status == 10'

)



Hi, thanks for your answer. Im try your solution but doesn’t work.

The column is part of a table from a query in which two tables are used: files and user. The query displays a user uploaded files, what I want is that the column that says looks "View" when the file status is 10

So has $data in GridView $status field? Or not?

Yes, Im try this but not work.




array(

'imageUrl'=>Yii::app()->baseUrl . '/img/view.png',

'header'=>'View', 

'class'=>'CLinkColumn',

                                                                                                  'linkHtmlOptions'=>array('title'=>'View'),

'visible'=> '$data->status == 10?True:False'

)



But are you sure that $data has ‘status’ property ?

Yes, Im sure

Try to add a column with ‘status’ field, in order to see his value.




array(

   'name' => 'status'

)



What display?

THats show a new column

OK, but what values?

Not display anything

This means that you haven’t value in ‘status’,

and so your visibility condition can’t work.

Ok, Im sorry, the real name is estatus_idestatus. Show 10 in rows.

I put this real name in the condition, but doesnt work for me.

Post your column definition (with visibility check).


array( 

					      		'imageUrl'=>Yii::app()->baseUrl . '/img/view.png',   

					      		'header'=>'View',       

					            'class'=>'CLinkColumn',

					            'urlExpression'=>'"detalle?idArchivo=".$data->idarchivo',

					            'linkHtmlOptions'=>array('title'=>'View'),

						    'visible'=>'$data->estatus_idestatus==10'


					      ),

‘visible’ fields works at root level of CGridView.

You can’t have a row that shows a column and a row that doesn’t show.

So condition is on a more general parameter as:




$columnVisible = false;


<?php $this->widget('bootstrap.widgets.TbGridView', array(

    

	'columns'=>array(

        array(

          'name' => 'tipo',

           'visible' => $columnVisible

        ),      



Now, there are two solutions:

  1. Use ‘value’ member to display a value when specific variable is 10, as



array(

'header' => 'Column name',

'value' => function($data) {

    if($data->estatus_idestatus==10) 

    {

        return '';

    }

    else

    {

         return 'content to return';

     }

}

....



  1. Use a more general variable to hide column;

Hi, Im use a function.


function isVisible($status){

            if($status == 10) 

            {    

            return true;}

            else {return false;}

    }


array(  

                                                

					      		'imageUrl'=>Yii::app()->baseUrl . '/img/view.png',   

					      		'header'=>'Visualizar',       

					            'class'=>'CLinkColumn',

					            'urlExpression'=>'"detalle?idArchivo=".$data->idarchivo',

					            'linkHtmlOptions'=>array('title'=>'Visualizar'),

                                                    'visible'=> isVisible($value->estatus_idestatus),


					      ),

In the column i’m call function, and works fine only for one row. If the result its more than 1 show me that error

Fatal error: Cannot redeclare isVisible() (previously declared in…