Yii Framework Forum: Using Switch In Cgridview Cssclassexpression - Yii Framework Forum

Jump to content

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

Using Switch In Cgridview Cssclassexpression Rate Topic: -----

#1 User is offline   jreznik 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 05-September 13

Posted 16 January 2014 - 08:56 PM

Is there any problem with the following array as a column in CGridView?

		array(
			'header' => 'Status',
			'value' => '$data->found_work',
			'cssClassExpression' =>
				'switch($data->found_work)
				{
					case \'Found\':
						return \'green\';
					case \'Not found\':
						return \'red\';
					case \'Wrong user\':
						return \'yellow\';
				}',
		),


I'm getting the error
Parse error: syntax error, unexpected 'switch' (T_SWITCH) in C:\xampp\yii\framework\base\CComponent.php(612) : eval()'d code on line 1
but don't know why. It's valid PHP code, does eval() not like switch statements?
0

#2 User is offline   jcagentzero 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 44
  • Joined: 14-October 11
  • Location:Philippines

Posted 16 January 2014 - 09:51 PM

View Postjreznik, on 16 January 2014 - 08:56 PM, said:

Is there any problem with the following array as a column in CGridView?

		array(
			'header' => 'Status',
			'value' => '$data->found_work',
			'cssClassExpression' =>
				'switch($data->found_work)
				{
					case \'Found\':
						return \'green\';
					case \'Not found\':
						return \'red\';
					case \'Wrong user\':
						return \'yellow\';
				}',
		),


I'm getting the error
Parse error: syntax error, unexpected 'switch' (T_SWITCH) in C:\xampp\yii\framework\base\CComponent.php(612) : eval()'d code on line 1
but don't know why. It's valid PHP code, does eval() not like switch statements?


Why do you need the switch inside the array? Can you try to remove the switch from the array and get the result of the switch statement before creating your array so that will will only look like this.

		
switch($data->found_work)
				{
					case 'Found':
						$classvalue = 'green';
					case 'Not found':
						$classvalue = 'red';
					case 'Wrong user':
						$classvalue = 'yellow';
				}
$array = array(
			'header' => 'Status',
			'value' => '$data->found_work',
			'cssClassExpression' => $classvalue
				
		),

Thank you and regards,

jcagentzero

http://www.jundycosmod.com

If you think this post is helpful, please don't hesitate to click + button.
0

#3 User is offline   jreznik 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 05-September 13

Posted 17 January 2014 - 02:17 PM

No, because the data model object for that row is not available outside that array.
http://www.yiiframew...pression-detail
0

#4 User is offline   jcagentzero 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 44
  • Joined: 14-October 11
  • Location:Philippines

Posted 17 January 2014 - 07:18 PM

Do something like this:
$arrayClass = array("Found" => "green", "Not Found" => "red", "Wrong User" => "yellow");

array(
                        'header' => 'Status',
                        'value' => '$data->found_work',
                        'cssClassExpression' => $arrayClass[$data->found_work]
                                
                ),

Thank you and regards,

jcagentzero

http://www.jundycosmod.com

If you think this post is helpful, please don't hesitate to click + button.
0

#5 User is offline   jreznik 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 05-September 13

Posted 26 January 2014 - 06:44 PM

Appreciate the support, however that's incorrect as well. $data is only valid when the expression is encased in quotes, it's all around when that code gets evaluated. In your suggestion, $data is undefined at that point in time because it is still at the point where we are configuring CGridView. It needs to be encased in quotes so that as CGridView is running its internal methods, it evaluates $data at that point in time. This would work if I could somehow inject $arrayClass into the column (via a expressionData property, which does not exist).

I think we will just drop CGridView and use CListView as that allows for much more flexible rendering possibilities.
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