Yii Framework Forum: CDetailView value expression - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CDetailView value expression Rate Topic: *---- 1 Votes

#1 User is offline   peasandcarrots 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 28-March 11

Posted 23 April 2011 - 05:08 PM

In CGridView, I am able to apply an expression to the value of a column like this:

    
  array(  'name'=>'f_user_admin',
          'header'=>'Admin?',
	  'value'=>'($data->f_user_admin===0)?"No":"Yes"',
  ),


Which allows me to display Yes or No, according to the database value of 1 or 0. Now it seems that the same functionality is not available in CDetailView. I have tried the code below, and the value comes out as, literally, ($data->f_user_admin===0)?"No":"Yes".

  array(  'name'=>'f_user_admin',
          'label'=>'Admin?',
	  'value'=>'($data->f_user_admin===0)?"No":"Yes"',
  ),


Does anyone have a suggestion as to how I can accomplish applying an expression within CDetailView? One would think the the same functionality that works in CGridView would work here.
0

#2 User is offline   peasandcarrots 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 28-March 11

Posted 24 April 2011 - 06:02 AM

View Postpeasandcarrots, on 23 April 2011 - 05:08 PM, said:

In CGridView, I am able to apply an expression to the value of a column like this:

    
  array(  'name'=>'f_user_admin',
          'header'=>'Admin?',
	  'value'=>'($data->f_user_admin===0)?"No":"Yes"',
  ),


Which allows me to display Yes or No, according to the database value of 1 or 0. Now it seems that the same functionality is not available in CDetailView. I have tried the code below, and the value comes out as, literally, ($data->f_user_admin===0)?"No":"Yes".

  array(  'name'=>'f_user_admin',
          'label'=>'Admin?',
	  'value'=>'($data->f_user_admin===0)?"No":"Yes"',
  ),


Does anyone have a suggestion as to how I can accomplish applying an expression within CDetailView? One would think the the same functionality that works in CGridView would work here.


Nothing? So what are my options here?
0

#3 User is offline   Haensel 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 444
  • Joined: 14-January 11
  • Location:Vienna (Austria)

Posted 24 April 2011 - 06:10 AM

Maybe it is too obvious, but why are you using quotes when passing the value parameter?

$name="Haensel";  
value=>'$name';


should literally return "$name", while

value=>$name;


should return "Haensel"
0

#4 User is offline   peasandcarrots 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 28-March 11

Posted 24 April 2011 - 08:37 AM

View PostHaensel, on 24 April 2011 - 06:10 AM, said:

Maybe it is too obvious, but why are you using quotes when passing the value parameter?

$name="Haensel";  
value=>'$name';


should literally return "$name", while

value=>$name;


should return "Haensel"


In hindsight, you're right, it is obvious! The fact that the example I had found for the CGridView contained the quotes, and worked, is what threw me off.

Thanks for responding, despite my silly oversight. It has saved me needless headaches.
0

#5 User is offline   peasandcarrots 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 28-March 11

Posted 27 April 2011 - 01:26 PM

View Postpeasandcarrots, on 24 April 2011 - 08:37 AM, said:

In hindsight, you're right, it is obvious! The fact that the example I had found for the CGridView contained the quotes, and worked, is what threw me off.

Thanks for responding, despite my silly oversight. It has saved me needless headaches.


Actually, this does not work. I thought it did at first, but I just realized it's not printing the correct value.

Please see this post for more information. Any help in figuring this one out would be greatly appreciated.
0

#6 User is offline   peasandcarrots 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 28-March 11

Posted 27 April 2011 - 07:11 PM

So is there no way of applying an expression to a CDetailView value???
0

#7 User is offline   bglee 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 152
  • Joined: 21-November 10
  • Location:Oak Park, IL

Posted 27 April 2011 - 10:14 PM

I just found an interesting, related thread: CDetailView with Function

The bottom line: it should work without single quotes on CDetailView and with single quotes on CGridView. You can try a function in place of the ternary (see link for example).
It takes 2 points to draw a straight line, but at least 3 to draw a conclusion.
0

#8 User is offline   Scott Deagan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 71
  • Joined: 04-February 10

Posted 18 December 2011 - 07:40 PM

I know this post is quite old now, but I thought I'd just add that you can use 'type' => 'boolean' to get a 'Yes|No' value:

  array(  'name'=>'f_user_admin',
          'label'=>'Admin?',
          'type'=>'boolean',

0

#9 User is offline   CeBe 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 554
  • Joined: 16-July 10
  • Location:Berlin. Germany

Posted 18 December 2011 - 11:29 PM

Or if it is not just boolean you simply do not need any expression since $model is available in your template:

$this->widget('zii.widgets.CDetailView', array(
    'data'=>$model,
    'attributes'=>array(
        // ...
        array(
            'name'=>'f_user_admin',
            'label'=>'Admin?',
            'value'=>(($model->f_user_admin===0)?"No":"Yes"),
        ),
    ),
));

0

#10 User is offline   senseijack 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 17-June 11

Posted 09 February 2012 - 04:09 PM

Is there a reason for using the identical operator (===) in your expression rather than just using the equal operator (==)? If you don't have to verify the type of the value, then it's not necessary. I set up a test with code similar to yours and once I switched the expression to remove the quotes and use the equal operator it worked with no issues:

array(  'name'=>'f_user_admin',
              'label'=>'Admin?',
              'value'=>(($data->f_user_admin==0)? 'No' : 'Yes' )),

0

#11 Guest_remya_*

  • Group: Guests

Posted 30 April 2012 - 04:23 AM

How set null value in Cgridview.
0

#12 User is offline   Pravin Gajera 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 137
  • Joined: 25-October 11
  • Location:India

Posted 30 April 2012 - 04:40 AM

View Postremya, on 30 April 2012 - 04:23 AM, said:

How set null value in Cgridview.



Hi

you can use


'value'=>'$data->action==null ? '' : $data->action->name',

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users