Yii Framework Forum: renderPartial in CGridView cell - Yii Framework Forum

Jump to content

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

renderPartial in CGridView cell Rate Topic: ***** 1 Votes

#1 User is offline   i.amniels 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 54
  • Joined: 05-August 10

Posted 05 September 2012 - 05:10 PM

I want to render a php view file in the cell of a CGridView.

Therefore I wrote this code:
$this->widget('zii.widgets.grid.CGridView', array(
  'dataProvider'=>$dataProvider,
  'columns'=>array(
      array(
          'value'=>array($this, 'renderPartial("_lineProblems")')
      ))));

But I get this error:
BookController and its behaviors do not have a method or closure named "renderPartial('_lineProblems' )".

The stack trace shows this as the problem:
call_user_func_array(array(BookController, "renderPartial('_lineProblems' )"), array("data" => line, "row" => 0, 0 => CDataColumn))


I don't understand the error. $this is an instance of BookController and BookController is a child of CController. CController has the methode renderPartial, so $this also has this method. How do I fix this?
0

#2 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,178
  • Joined: 05-January 12
  • Location:Morocco

Posted 06 September 2012 - 12:53 AM

I'm even more surprised that you could declare an array as the 'value' property… Doc says 'string'
http://www.yiiframew...mn#value-detail

Normally, $this used in the context of a component refers to that component. You may want to read this: http://stackoverflow...is-this-defined
0

#3 User is offline   i.amniels 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 54
  • Joined: 05-August 10

Posted 06 September 2012 - 02:42 AM

Passing an array is a feature:
http://www.yiiframew...pression-detail

And $this refers to the controller, because $this is called in the view, not in the component.
0

#4 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,178
  • Joined: 05-January 12
  • Location:Morocco

Posted 06 September 2012 - 03:11 AM

Good to know, thanks!

For the $this part, I guess I meant when someone would want to use it in a regular string value: 'value' => '$this->…' then $this wouldn't be the controller, but CDataColumn in this case.

Anyway, it's not what you need, but I've learnt something new this morning, thanks.

I tried on some existing code, and doing this works:
     'value' => function(){
                    $this->renderPartial('_lineProblems');
                },


But layout disappears even with just render
0

#5 User is offline   i.amniels 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 54
  • Joined: 05-August 10

Posted 06 September 2012 - 03:18 AM

The code you suggested doesn't work here. I get this error:

Fatal error: Using $this when not in object context in view.php on line 112
0

#6 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,178
  • Joined: 05-January 12
  • Location:Morocco

Posted 06 September 2012 - 03:30 AM

Weird, it worked for me. Which PHP and Yii versions do you have?
0

#7 User is offline   i.amniels 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 54
  • Joined: 05-August 10

Posted 06 September 2012 - 03:35 AM

This works for me:

$controller = $this;
...

    'value'=>function($data, $row) use ($controller){
                return $controller->renderPartial('_lineProblems', NULL, true);
          }



Thanks for your help.

I use Yii 1.1.9 with PHP 5.3.6.
0

#8 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,178
  • Joined: 05-January 12
  • Location:Morocco

Posted 06 September 2012 - 03:42 AM

Here 5.3.5 with 1.1.10 -- maybe there's a difference, too lazy to check :)

And your layout is ok with your syntax?
0

#9 User is offline   i.amniels 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 54
  • Joined: 05-August 10

Posted 06 September 2012 - 03:57 AM

Yes, it works fine.
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