Yii Framework Forum: How To Access Variable Passed From Controller Into Cgridview - Yii Framework Forum

Jump to content

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

How To Access Variable Passed From Controller Into Cgridview Rate Topic: -----

#1 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 756
  • Joined: 02-February 09

Posted 15 February 2013 - 04:41 AM

I have a Controller with action. In the action I render a view and pass an array to the view such as:

$this->render('view', array(
                        'hours'                 =>      $object->hours,
            ));


hours is an array of the type array('Name'=>200)

What I am trying to achieve is in the view I am rendering CGridView and in one of the columns I would like to display the hours to the respective name. Now in the CGridView I have a column which displays the name and another which should display the hours. How can I inject the array which was passed from the controller so I can display a value of the respective name.

Here is the columns section which I am struggling with:

array('name'=>'team', 'type'=>'raw', 'header'=>'Team'),
array('name'=>'hours', 'header'=>'Hours', 'value'=>............),


Cheers.
0

#2 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 15 February 2013 - 05:46 AM

Dear Friend

We can do it in the following way.

array('name'=>'hours',
      'header'=>'Hours', 
      'value'=>function($data,$row) use($hours)
                      {
                        return isset($hours[$data->name])?$hours[$data->name]:'';
                      }

),


Regards.
0

#3 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 756
  • Joined: 02-February 09

Posted 15 February 2013 - 07:06 AM

Thank for your reply seenivasan. I should've mentioned that the project is running on PHP 5.1 hence anonymous functions are not supported. Any other ides?
0

#4 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,707
  • Joined: 04-March 10
  • Location:UK

Posted 15 February 2013 - 07:20 AM

Can you not do the same thing but use a callback function rather than an anonymous function?
0

#5 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 756
  • Joined: 02-February 09

Posted 15 February 2013 - 08:46 AM

View PostKeith, on 15 February 2013 - 07:20 AM, said:

Can you not do the same thing but use a callback function rather than an anonymous function?


It would not be the same. The thing is that I am generating this array with a heavy query. So what I am trying to prevent is call that query for every row generated by CGridView so I am trying to pass all the data via controller and then reuse it in the form of an array but I cannot seem to find a way to inject that array into CGridView which is a pain in the but :P
0

#6 User is online   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,194
  • Joined: 16-February 11
  • Location:Japan

Posted 15 February 2013 - 09:16 AM

It's not an elegant solution, but you can declare some property (public variable) in the controller to hold your data.
Then it can be accessed from the CDataColumn's value expression.
'$this->grid->controller->hours'

Where $this is a CDataColumn. It has a property 'grid' that means the parent grid. And the CGridView has a property 'controller'. And then ... :D
0

#7 User is offline   rootbear 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 225
  • Joined: 17-June 11

Posted 15 February 2013 - 11:52 AM

inspired by @softark, do this:

1) in model, add virtual property

public $myhour;


2) in control, pass array to view

$this->render('list', array(
	'model' => $model,
	'hours' => $hours,
));


3. in view,

'dataProvider' => $model->search($hours), //<--pass $hours to model search function!
//~~
'columns' => array(
//~~
array(
	'name'=>'myhour',
	'value'=>'$data->myhour',
),
//~~


4. back to model search function, ta-ta!

public function search($hours = null) {

//~~
	
$dataProvider = new CActiveDataProvider($this, array(
	'criteria' => $criteria,
));

//~~ trick here !!!
if ($hours){
	foreach($dataProvider->data as $one)
	{
		if (isset($hours[$one->name]))
			$one->myhour = $hours[$one->name];
	}
}

return $dataProvider;

}


I enjoy the Yii.sy coding life here.
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