Yii Framework Forum: Sorting CGridView by relation - Yii Framework Forum

Jump to content

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

Sorting CGridView by relation Rate Topic: -----

#1 User is offline   rymonator 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 124
  • Joined: 02-December 10

Posted 04 November 2011 - 10:41 AM

I feel like this is incredibly obvious but it's just not clicking for me right now...I want to have the default order for a CGridView to be based on a relation's atrribute...

//relation
return array(
    'engine' => array(self::BELONGS_TO,'Engines','eid','order'=>'name'),
);


In my CGridView I display as one of the columns the engine's name, like so:

array(
    'name'=>'engine',
    'value'=>'$data->engine->name'
),


How do I get it so that the grid is sorted by default by that name attribute? Thanks.
0

#2 User is offline   rei 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 324
  • Joined: 10-November 10

Posted 05 November 2011 - 01:09 AM

Hello,

I have ever made something like this before and here is how I made it works.

In the relation:
return array(
   'distributor' => array(self::BELONGS_TO, 'Distributor', 'distributor_id'),
);


In search procedure:
// set sort options
$sort = new CSort;
$sort->attributes = array(
   ... (the other fields)
   'distributorName'=>array(
      'asc'=>'distributor.name',
      'desc'=>'distributor.name desc',
  ),
);
$sort->defaultOrder = 'distributor.name';

return new CActiveDataProvider(get_class($this), array(
  'criteria'=>$criteria,
  'sort'=>$sort,
));


And in CGridView:
array(
  'name'=>'distributorName',
  'value'=>'$data->distributor->name',			
)


Not sure if this is the best solution, but hopefully this can give a click to you. :)
Fipick - Find and pick recommendations
0

#3 User is offline   angela88 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 144
  • Joined: 14-October 11
  • Location:Jakarta

Posted 06 November 2011 - 09:29 PM

View Postrymonator, on 04 November 2011 - 10:41 AM, said:

I feel like this is incredibly obvious but it's just not clicking for me right now...I want to have the default order for a CGridView to be based on a relation's atrribute...

//relation
return array(
    'engine' => array(self::BELONGS_TO,'Engines','eid','order'=>'name'),
);


In my CGridView I display as one of the columns the engine's name, like so:

array(
    'name'=>'engine',
    'value'=>'$data->engine->name'
),


How do I get it so that the grid is sorted by default by that name attribute? Thanks.


maybe by adding an order criteria in fuction search in models...
like this
$criteria=new CDbCriteria;
$criteria->order = "engine.name";

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