Yii Framework Forum: CGridView of a model inside the view of another model - Yii Framework Forum

Jump to content

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

CGridView of a model inside the view of another model Rate Topic: -----

#1 User is offline   transistor 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 79
  • Joined: 05-June 09
  • Location:Mexico

Posted 22 February 2010 - 02:08 AM

Hello everyone.
I want to display and manage the items that belong to a different model.
In my controller I have this code to create a dataProvider in the parent Controller:
$vehiculo_modelos_dp = new CActiveDataProvider('vehiculo_modelo', array(
	'criteria'=>array(
		'condition'=>'vehiculo_marca_id=:vehiculo_marca_id',
		'params'=>array(':vehiculo_marca_id'=>$model->id),
		'order'=>'t.orden, vehiculo_modelo asc',
		'with'=>'vehiculo_tipo',
		'together'=>true,
	),
));

This goes in the _form partial of the parent Controller:
<?php $this->widget('zii.widgets.grid.CGridView', array(
	'dataProvider'=>$vehiculo_modelos_dp,
	'columns'=>array(
		'id',
		'vehiculo_modelo',
		array(
			'name' => 'Tipo',
			'value' => '$data->vehiculo_tipo->vehiculo_tipo',
			'sortable'=>true,
		),
		'orden',
		'activo',
		array(
			'class'=>'CButtonColumn',
		),
	),
)); ?>

The grid shows up alright, but I can't sort it (nothing happens when I click the header) and the links of the action buttons (delete, edit, view) act on the parent model, not the child.
Also, my column named "Tipo" is not sortable (there is no link to click).
Can you help me work this out please?
0

#2 User is offline   bettor 

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

Posted 22 February 2010 - 10:54 AM

try using 'name' => 'vehiculo_tipo', and it should be sortable now
0

#3 User is offline   transistor 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 79
  • Joined: 05-June 09
  • Location:Mexico

Posted 22 February 2010 - 12:32 PM

@ bettor:
Using 'name'=>'vehiculo_tipo_id' (which is the name of the field) makes it sortable, thank you!

However, it doesn't sort. I click any header and the order stays the same.

The link on the header is:
http://transistor.local/vmc_yii/admin.php?r=vehiculo_marca/update&id=1&vehiculo_modelo_sort=vehiculo_tipo_id&ajax=yw0


The problem is that the link points to vehiculo_marca (because it is in the view of that controller, which is the parent), but it should point to the vehiculo_modelo (which is the child).
Maybe I need a way to pass the vehiculo_modelo_sort to the CGridView thru the view... I'll try this.

If anyone else knows what to do to make my CGridView sort and delete, edit, view, please let me know.
Thanks
0

#4 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,339
  • Joined: 12-October 09
  • Location:Croatia

Posted 25 February 2010 - 06:16 AM

If you use 'order' in the dataprovider then the grid is not sortable by other columns because the SQL command has the "order by ..."

So instead of
'order'=>'t.orden, vehiculo_modelo asc',


use

'sort'=>array('defaultOrder'=>'t.orden, vehiculo_modelo asc',),




Find more about me.... btw. Do you know your WAN IP?
1

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