CGridView - conditional CLinkColumns

I have a CGridView in which I would like a particular column to display a hyperlink or standard unclickable text depending on a data field for that row. I tried using a CLinkColumn with the urlExpression property set. The problem is that this creates a hyperlink even when I want to revert to standard text (the best I can do is use a ‘#’ URL for what should be standard text).




    'labelExpression'=>'($data->player->underChallenge==1 ? "Under Challenge" : "Challenge Player")',

    'urlExpression'=>'($data->player->underChallenge!=1) ? Yii::app()->createUrl("player/view",array("id"=>$data->player->id)) : "#"',

    'cssClassExpression'=>'($data->player->underChallenge==1 ? " challenged" : "")',  


Is there a standard Yii way to achieve this? Or does it require a creative workaround?

Any input would be much appreciated.

I think you can use the regular CDataColumn with the ‘type’ property set to ‘raw’, and give the ‘value’ property the expression to be evaluated.


    /* 'class'=>'CDataColumn', */ /* it's the default */




        ? CHtml::link("ChalengePlayer", array("player/view", "id"=>$data->player->id))

        : "Under Challenge"',


Fantastic! That’s just what I was looking for. I knew what I wanted to do, but not how to do it!

Many thanks.

Hi there,

is there any way to set conditional content for CButtonColumn(s)?

I’m trying something like this in defining a button named ‘advance’:




			        'advance' => array


			            'label'=>'$data["division"]->canDivisionAdvance() ? Yii::t("app", "Advance division") : Yii::t("app", "Cannot advance division")',


			            'url'=>'Yii::app()->createUrl("/tournamentdivision/advance", array("id"=>$data["id"]))',



What I get as label for my button is:

$data["division"]->canDivisionAdvance() ? Yii::t("app", "Advance division") : Yii::t("app", "Cannot advance division")

That is, I get the whole line, as if the expression wouldn’t be evaluated.

Thanks for any hint or suggestion.