Primate
(Sandergroen27)
September 14, 2011, 9:31am
1
Hello everyone,
I like to use some conditions in the GridView if have this button and i like to change the url and the imageUrl when the id of that item is in a session. How can this be done?
I tried something like this:
'imageUrl'=> (isset(Yii::app()->session['added']['$data->Id'])) ? '/images/basket_checked.png' : '/images/basket.png',
But unfortunately it does not work.
Any ideas?
Thanks in advanced
Haensel
(Johannes)
September 14, 2011, 9:35am
2
I never used that approach, but I think you shouldn’t pass $data->Id as a string. Try
'imageUrl'=> (isset(Yii::app()->session['added'][$data->id])) ? '/images/basket_checked.png' : '/images/basket.png',
Also check if Id isn’t ment to be id (lower case)
Primate
(Sandergroen27)
September 14, 2011, 10:15am
3
Haensel:
I never used that approach, but I think you shouldn’t pass $data->Id as a string. Try
'imageUrl'=> (isset(Yii::app()->session['added'][$data->id])) ? '/images/basket_checked.png' : '/images/basket.png',
Also check if Id isn’t ment to be id (lower case)
When not using $data var as a string i get an error: Undefined variable: data
What i need is the current Id.
mbi
(mbi)
September 14, 2011, 10:56am
4
imageUrl is not an "evaluated expression" so you cannot use $data
Primate
(Sandergroen27)
September 14, 2011, 11:06am
5
Is there no other way to get this to work?
Primate
(Sandergroen27)
September 16, 2011, 9:38am
6
Yes there is I extended the CButtonColumn:
<?php
Yii::import('zii.widgets.grid.CButtonColumn');
class SButtonColumn extends CButtonColumn
{
protected function renderDataCellContent($row,$data)
{
$tr=array();
ob_start();
foreach($this->buttons as $id=>$button)
{
if(isset(Yii::app()->session['download'][$data->Id])){
$button['imageUrl'] = "/images/basket_checked.png";
}
$this->renderButton($id,$button,$row,$data);
$tr['{'.$id.'}']=ob_get_contents();
ob_clean();
}
ob_end_clean();
echo strtr($this->template,$tr);
}
}
dniznick
(Dniznick)
September 16, 2011, 1:44pm
7
Can you just use the ‘visible’ property for that?
I do that - set buttons as visible or not depending on return value from a function using the $data in that row.
Primate
(Sandergroen27)
September 16, 2011, 2:04pm
8
I use two buttons one for added to basket and one to remove from basket. I do not want the buttons to disappear.
cxuanyi
(Cxuanyi)
August 29, 2013, 4:06pm
9
Here’s the solution. After creating this file, just specify the condition similar to Visible.
components/SButtonColumn.php
<?php
Yii::import(‘zii.widgets.grid.CButtonColumn’);
class SButtonColumn extends CButtonColumn {
protected function renderButton($id, $button, $row, $data) {
if (isset($button['visible']) && !$this->evaluateExpression($button['visible'], array('row' => $row, 'data' => $data)))
return;
$label = isset($button['label']) ? $button['label'] : $id;
$url = isset($button['url']) ? $this->evaluateExpression($button['url'], array('data' => $data, 'row' => $row)) : '#';
$options = isset($button['options']) ? $button['options'] : array();
if (!isset($options['title']))
$options['title'] = $label;
if (isset($button['imageUrl']) && is_string($button['imageUrl'])) {
$image = $this->evaluateExpression($button['imageUrl'], array('data' => $data, 'row' => $row));
echo CHtml::link(CHtml::image($image, $label), $url, $options);
} else {
echo CHtml::link($label, $url, $options);
}
}
}