Yii Framework Forum: CGridView default sort - Yii Framework Forum

Jump to content

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

CGridView default sort Rate Topic: ***** 2 Votes

#1 User is offline   jjmf 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 66
  • Joined: 10-November 08

  Posted 05 April 2010 - 08:32 AM

How can I specify the CGridView default sort?
I want that as soon as the page finishes loading, the grid should be sorted alphabetically by a specific column.
Of course the user can later change that order by clicking in the column headers.
4

#2 User is offline   Matt Kelliher 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 28
  • Joined: 12-February 10
  • Location:Tampa, FL

Posted 05 April 2010 - 08:29 PM

In your data provider, you can use the "defaultOrder" property of CSort. For example:

$dataProvider=new CActiveDataProvider('Example', array(
  'sort'=>array(
    'defaultOrder'=>'title ASC',
  )
));

4

#3 User is offline   DarkNSF 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 287
  • Joined: 12-November 08
  • Location:Palm Bay, Florida

Posted 24 June 2010 - 02:18 PM

Thanks for the answer :lol:
0

#4 User is offline   Ho Khac Huy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 06-September 10

Posted 11 October 2010 - 06:32 AM

Hello,

I can add default sort column to my CGridView, but the default sort icon does not display on column header.

Can anyone help me?

Thanks
0

#5 User is offline   mcnecessary 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 14-July 11

Posted 15 July 2011 - 03:54 PM

*
POPULAR

View PostHo Khac Huy, on 11 October 2010 - 06:32 AM, said:

Hello,

I can add default sort column to my CGridView, but the default sort icon does not display on column header.

Can anyone help me?

Thanks


I just ran into this same issue, and will document the solution here in case anyone else is searching the forums for a solution. As of version 1.1.3, you can make defaultOrder an array, which will in turn populate the directions property, which will make the arrow direction indicator icons show up like you want:

$dataProvider=new CActiveDataProvider('Example', array(
  'sort'=>array(
    'defaultOrder'=>array(
      'title'=>false
    )
  )
));


The value of the column name key indicates direction - false for ASC, true for DESC.
8

#6 User is offline   Steven Ly 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 03-July 12

Posted 05 July 2012 - 11:57 AM

View PostMatt Kelliher, on 05 April 2010 - 08:29 PM, said:

In your data provider, you can use the "defaultOrder" property of CSort. For example:

$dataProvider=new CActiveDataProvider('Example', array(
  'sort'=>array(
    'defaultOrder'=>'title ASC',
  )
));



This solved my problem. Thanks! B)
0

#7 User is offline   SpikyJT 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 16-November 10

Posted 13 July 2012 - 01:06 PM

View Postmcnecessary, on 15 July 2011 - 03:54 PM, said:

I just ran into this same issue, and will document the solution here in case anyone else is searching the forums for a solution. As of version 1.1.3, you can make defaultOrder an array, which will in turn populate the directions property, which will make the arrow direction indicator icons show up like you want:

$dataProvider=new CActiveDataProvider('Example', array(
  'sort'=>array(
    'defaultOrder'=>array(
      'title'=>false
    )
  )
));


The value of the column name key indicates direction - false for ASC, true for DESC.


Thanks - I was scratching my head over getting the order arrows to display on page load. Note that from version 1.1.10, the constants CSort::SORT_ASC and CSort::SORT_DESC are available for convenience/readability.
0

#8 User is offline   webmastak 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 10-December 12

Posted 25 March 2013 - 08:09 AM

there is no need to complicate, just:
$model=new YourModel();
$model->dbCriteria->order='title ASC';

shakedbrains this!
2

#9 User is offline   auren27 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 44
  • Joined: 06-February 13

Posted 04 April 2013 - 10:46 AM

View Postwebmastak, on 25 March 2013 - 08:09 AM, said:

there is no need to complicate, just:
$model=new YourModel();
$model->dbCriteria->order='title ASC';


This works! Great suggestion.

Many thanks
0

#10 User is offline   Scott Gardner 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 03-May 13

Posted 15 July 2013 - 05:16 AM

View Postwebmastak, on 25 March 2013 - 08:09 AM, said:

$model->dbCriteria->order='title ASC';


Thanks!
0

#11 User is offline   winyata senjaya 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 29-April 11
  • Location:Indonesia

Posted 27 March 2014 - 07:11 AM

$dataProvider=new CActiveDataProvider('Example', array(
  'sort'=>array(
    'defaultOrder'=>array(
      'title'=>false
    )
  )
));

That's very useful for me..Thank You
0

#12 User is offline   Mahdian 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 21-April 14

Posted 24 June 2014 - 04:41 AM

$sort=new CSort;
        $sort->attributes=array(
            'name' => array( "asc"=>'id', "desc" => 'id desc'),
        );
        $sort->defaultOrder = 'id desc';

$dataProvider=new CActiveDataProvider('Example', array(
  'sort'=>$sort
));

0

#13 User is offline   yordankis 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 14-January 14

Posted 01 October 2015 - 08:36 AM

View Postwebmastak, on 25 March 2013 - 08:09 AM, said:

there is no need to complicate, just:
$model=new YourModel();
$model->dbCriteria->order='title ASC';

shakedbrains this!


Please don't use this because if you click in the name of the grid column use to order the generated sql put twice order clause and fail.

use CActiveDataProvider->sort->defaultOrder='title ASC'
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