abnab
abnab

Reputation: 846

order data by desc order in default in YII

I am tyring to display somedata in desending order in YII view.HOw Do I order the data by studentID desc by default ?

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

'order'=>'StudentID DESC',

     ),

    'dataProvider' => Agent::getStudents($model->agent_id),
    'columns' => array(
        'StudentID',
        'first_name',
        'last_name',

        'dob',
        'gender',
        array(
            'header' => 'Options',
            'class' => 'CButtonColumn',
            'template'=>'{View}',
            'buttons'=>array(
                'View' => array(
                    'url'=> 'Yii::app()->createUrl("/students/view/" . $data->StudentID)',
                ),
            ),
        ),
    ),
)); ?>

Thanks Ab

Upvotes: 0

Views: 8505

Answers (1)

pere_
pere_

Reputation: 235

In your dataProvider (i assume that's what getStudents() function returns) add another array to your config array :) Like:

return new CActiveDataProvider(get_class($this), array(
    'criteria'=>$some_criteria,
    /* Your array */
    'sort'=>array(
        'defaultOrder'=>array(
            'StudentID'=>true,
        ),
    ),
    /***/
));

Value 'false' in 'defaultOrder' array refers to ascending order, 'true' refers to descending order.

I hope that's what you're looking for :).

Also check out this forum thread: http://www.yiiframework.com/forum/index.php/topic/8428-cgridview-default-sort/ and this doc: http://www.yiiframework.com/doc/api/1.1/CSort#defaultOrder-detail

Regards.

Upvotes: 5

Related Questions