Yii Framework Forum: Is it possible to have nested CGridViews? - Yii Framework Forum

Jump to content

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

Is it possible to have nested CGridViews? Rate Topic: -----

#1 User is offline   dubby 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 125
  • Joined: 10-February 09
  • Location:Melbourne, Australia

Posted 27 June 2012 - 12:16 AM

If I had two tables (Post and Comment) where a post had many comments, I could easily have a page showing each post in CGridView and make clicking on one of the posts take me to page showing related comments also in grid view.

However I was wondering if there were any solutions to show the related comments shown underneath its post in some sort of nested view.

Is something like this possible? I've seen this post (http://stackoverflow...ested-cgridview) but I am looking to actually include CGridView nested.
0

#2 User is offline   rootbear 

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

Posted 27 June 2012 - 06:48 AM

try groupgridview extension
when i ask, if my question does not make sense, don't blame on me, coz i am new here to learn yii;
when i answer, if my answer does not make sense, don't blame on me, coz i am trying to answer to be answered;

ok, i'm covered to mess up and fooling around, yeehaw ...
0

#3 User is offline   Jimlam 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 195
  • Joined: 17-March 12

Posted 27 June 2012 - 01:07 PM

View Postdubby, on 27 June 2012 - 12:16 AM, said:

If I had two tables (Post and Comment) where a post had many comments, I could easily have a page showing each post in CGridView and make clicking on one of the posts take me to page showing related comments also in grid view.

However I was wondering if there were any solutions to show the related comments shown underneath its post in some sort of nested view.

Is something like this possible? I've seen this post (http://stackoverflow...ested-cgridview) but I am looking to actually include CGridView nested.

Hi Dubby,

I am new to Yii and am trying to do what you said was easy in the first part of your post. Can you please guide me how to do this? I have a department with many employees and I want to have a cgrid view of the employees when I click on a row in cgrid view of department. Thanks
0

#4 User is offline   jkofsky 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 134
  • Joined: 17-May 10
  • Location:Pensacola, Florida

Posted 27 June 2012 - 01:27 PM

View PostBianca, on 27 June 2012 - 01:07 PM, said:

Hi Dubby,

I am new to Yii and am trying to do what you said was easy in the first part of your post. Can you please guide me how to do this? I have a department with many employees and I want to have a cgrid view of the employees when I click on a row in cgrid view of department. Thanks

Are talking about a CGridView of the departments, or of all employees? If just a department list, you can set the value of the column to a link to Employees with the selected department to view just those employees.

If a list of all Employees, then you could set up a dropdown list filter for the department column.
Do, or do not. There is no 'try.' Jedi Master Yoda
0

#5 User is offline   dubby 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 125
  • Joined: 10-February 09
  • Location:Melbourne, Australia

Posted 27 June 2012 - 07:07 PM

Thanks Rootbear,

That extensions looks good but it's not nested as such and it only includes the one model. I think I'll write the HTML out in a for loop to achieve what I need.

Bianca, you want something like this:

<?php $this->widget('zii.widgets.grid.CGridView',
        array('dataProvider' => $dataProvider,
        'columns'=>array(
            'id',
            'departmentName',
            array('name'=>'Employees', 'type'=>'raw', 'value'=>'CHtml::link("Employees", array("employees/admin", "id"=>$data["id"]))'),
        ),  
   ));     
 
?> 


and then in your Employee::actionAdmin method you could have something similar to this:

public function actionAdmin($id=null)
{
    $model=new Employee('search');
    $model->unsetAttributes();  // clear any default values

    if($id != null)
        $model->departmentId=$id;
    
    $this->render('admin',array(
       'model'=>$model,
    ));
}


Also, rather than specifying $id=null in the function declaration you could use a filter
0

#6 User is offline   dubby 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 125
  • Joined: 10-February 09
  • Location:Melbourne, Australia

Posted 27 June 2012 - 08:16 PM

I just thought that a simple foreach loop will not give me the pagination that I require. I'll have to look further into this.
0

#7 User is offline   Jimlam 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 195
  • Joined: 17-March 12

Posted 28 June 2012 - 12:41 AM

View Postdubby, on 27 June 2012 - 07:07 PM, said:

Thanks Rootbear,

That extensions looks good but it's not nested as such and it only includes the one model. I think I'll write the HTML out in a for loop to achieve what I need.

Bianca, you want something like this:

<?php $this->widget('zii.widgets.grid.CGridView',
        array('dataProvider' => $dataProvider,
        'columns'=>array(
            'id',
            'departmentName',
            array('name'=>'Employees', 'type'=>'raw', 'value'=>'CHtml::link("Employees", array("employees/admin", "id"=>$data["id"]))'),
        ),  
   ));     
 
?> 


and then in your Employee::actionAdmin method you could have something similar to this:

public function actionAdmin($id=null)
{
    $model=new Employee('search');
    $model->unsetAttributes();  // clear any default values

    if($id != null)
        $model->departmentId=$id;
    
    $this->render('admin',array(
       'model'=>$model,
    ));
}


Also, rather than specifying $id=null in the function declaration you could use a filter

Thank you very much for your prompt reply. I tried it and had to do some modifications because of the double quotes (btw is it normal? I had to replace the double quotes by single ones) Where must you define variable $dataProvider? Sorry I am new at Yii and am finding it hard to understand. Glad if you could help me. Thanks
0

#8 User is offline   Jimlam 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 195
  • Joined: 17-March 12

Posted 28 June 2012 - 12:56 AM

I figured out where to put the variable $dataprovider but where do you define $data?
Thanks
0

#9 User is offline   dubby 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 125
  • Joined: 10-February 09
  • Location:Melbourne, Australia

Posted 28 June 2012 - 01:08 AM

It's a variable provided by $dataProvider. I didn't understand it earlier but it seems obvious now as I type it.
0

#10 User is offline   Jimlam 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 195
  • Joined: 17-March 12

Posted 28 June 2012 - 02:54 AM

View Postdubby, on 28 June 2012 - 01:08 AM, said:

It's a variable provided by $dataProvider. I didn't understand it earlier but it seems obvious now as I type it.


Thanks Dubby. I looked for the explanation and found it but am unable to make it work: I have a problem with the syntax:

<?php $dataProvider=new CActiveDataProvider('Department'); ?>

<?php $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'department-grid',

	'dataProvider'=>$dataProvider,

	'columns'=>array(

		'id',
		'name',
		array('name'=>'Employees', 'type'=>'raw', 'value'=>'CHtml::link('Eyees', $this->createUrl('employee/admin'),array('id'=>'$data->id'))'),

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>


I am having a parse error:

Parse error: parse error, expecting `')'' in C:\wamp\www\testdrive\protected\views\department\admin.php on line 46

I have tried to edit several times but am stuck. Line 46 refers to CHtml::link
0

#11 User is offline   dubby 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 125
  • Joined: 10-February 09
  • Location:Melbourne, Australia

Posted 28 June 2012 - 07:37 AM

Hi Bianca,

Your issue is that you're encapsulating the string in single quotes but then having the variables also in single quotes. PHP is unable to determine where your string starts and stops.

Your example:

'CHtml::link('Eyees', $this->createUrl('employee/admin'),array('id'=>'$data->id'))'


try leaving the full string in single quotes but putting the variables in double quotes like this:

'CHtml::link("Eyees", $this->createUrl("employee/admin"),array("id"=>"$data->id"))'


Notice the single quotes appear as the very first and very last characters ONLY.

Further note: If you need to use single quotes (or only double quotes) throughout you need to escape them first like this:

'CHtml::link(\'Eyees\', $this->createUrl(\'employee/admin\'),array(\'id\'=>\'$data->id\'))'

0

#12 User is offline   dubby 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 125
  • Joined: 10-February 09
  • Location:Melbourne, Australia

Posted 28 June 2012 - 07:38 AM

So, this post has gone off topic. Can anyone help me with nested CGridView?
0

#13 User is offline   Gerhard Liebenberg 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 200
  • Joined: 07-January 12
  • Location:Stillbay - Western Cape - South Africa

Posted 10 July 2012 - 11:24 AM

Sorry for being a bit late dubby.

I think this wiki is what you want:

http://www.yiiframew...ew-was-clicked/

Regards
Gerhard
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