Search code examples
yiiframeworkscgridview

Yii: Change default order in CGridView


I have generated giix crud for model Faq

Controller:

public function actionAdmin() {
    $model = new Faq('search');
    $model->unsetAttributes();

    if (isset($_GET['Faq']))
        $model->setAttributes($_GET['Faq']);

    $this->render('admin', array(
        'model' => $model,
    ));
}

View - admin:

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'faq-grid',
    'dataProvider' => $model->search(array('order'=>'order ASC')),
    'filter' => $model,
    'columns' => array(
        'order',
        'question',
        'answer',
        array(
            'class' => 'CButtonColumn',
        ),
    ),
)); ?>

I want order items by field 'order', so I add array('order'=>'order ASC') to $model->search(); but this didn't change anything. Where is mistake?


Solution

  • update your search() function in the model and add the following code

    'criteria'=>$criteria,
    //add here
    
    'sort'=>array(
        'defaultOrder'=>'order ASC',
    ),
    

    and inside admin view change the dataprovider to this

    'dataProvider' => $model->search(),