Conditional buttons in CButtonColumn

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

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.

imageUrl is not an "evaluated expression" so you cannot use $data

Is there no other way to get this to work?

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);

	}

}



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.

I use two buttons one for added to basket and one to remove from basket. I do not want the buttons to disappear.

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(&#036;button['visible']) &amp;&amp; &#33;&#036;this-&gt;evaluateExpression(&#036;button['visible'], array('row' =&gt; &#036;row, 'data' =&gt; &#036;data)))


  return;


&#036;label = isset(&#036;button['label']) ? &#036;button['label'] : &#036;id;


&#036;url = isset(&#036;button['url']) ? &#036;this-&gt;evaluateExpression(&#036;button['url'], array('data' =&gt; &#036;data, 'row' =&gt; &#036;row)) : '#';


&#036;options = isset(&#036;button['options']) ? &#036;button['options'] : array();


if (&#33;isset(&#036;options['title']))


  &#036;options['title'] = &#036;label;





if (isset(&#036;button['imageUrl']) &amp;&amp; is_string(&#036;button['imageUrl'])) {


  &#036;image = &#036;this-&gt;evaluateExpression(&#036;button['imageUrl'], array('data' =&gt; &#036;data, 'row' =&gt; &#036;row));


  echo CHtml::link(CHtml::image(&#036;image, &#036;label), &#036;url, &#036;options);


} else {


  echo CHtml::link(&#036;label, &#036;url, &#036;options);


}

}

}