Debashis
Debashis

Reputation: 596

sortableAttributes option of Yii

I am using the below code to display data.

$widgetOptions = array(
    'dataProvider' => $this->dataProvider,
    'itemView' => $this->viewName('list', $_REQUEST['listType']),
    'id' => 'list_view',
    'viewData' => array('passing_percent' => $passing_percent),
    'sortableAttributes'=>array(
        'stud_i',
    ),
);

$this->widget('booster.widgets.TbListView', $widgetOptions);

To sort the data based on student id, i used sortableAttributes option. But instead of sorting the data in ASC order for the first time, i want to sort it using DESC order (i.e, when clicked first time, it will sort the data in DESC order) as my query is already returning the result in ASC order.

I tried below codes but no luck.

$this->dataProvider->sort->defaultOrder='stud_i DESC';

Also tried:

defaultOrder'=>array(
    'stud_i'=>CSort::SORT_DESC,
)

Please suggest how can i fix it.

Upvotes: 0

Views: 71

Answers (1)

Alexander R.
Alexander R.

Reputation: 1756

I'm doing this when I create provider. Look in some of my code...

    $sortAttrs = array(
        'date' => array(
            'asc' => 'a.id ASC',
            'desc' => 'a.id DESC',
            'default' => 'desc', // <-- default sorting per field
        ) /* Here I add more fields that I want be able to sort later */
    );

    return new CommentsSqlDataProvider($selectCmd, array(
        'keyField' => 'id',
        'totalItemCount' => $count,
        'params' => $params,
        'sort' => array(
            'attributes' => $sortAttrs,
            'defaultOrder' => array(
                'date' => CSort::SORT_DESC // <--- DEFAULT SORTING WHEN NO SORT KEY WAS SPECIFIED
            ),
        ),
        'pagination' => array(
            'pageSize' => $search->getLimit(),
        ),
    ));

Upvotes: 1

Related Questions