jhonnynho
(Jdag1992)
March 25, 2015, 9:02pm
1
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'
)
jhonnynho
(Jdag1992)
March 25, 2015, 11:34pm
3
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?
jhonnynho
(Jdag1992)
March 25, 2015, 11:49pm
5
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 ?
Try to add a column with ‘status’ field, in order to see his value.
array(
'name' => 'status'
)
What display?
This means that you haven’t value in ‘status’,
and so your visibility condition can’t work.
jhonnynho
(Jdag1992)
March 26, 2015, 1:07am
13
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).
jhonnynho
(Jdag1992)
March 26, 2015, 1:28am
15
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:
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';
}
}
....
Use a more general variable to hide column;
jhonnynho
(Jdag1992)
March 26, 2015, 6:39pm
17
‘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:
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';
}
}
....
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…