Question

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?

Was it helpful?

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(),

OTHER TIPS

get CActiveDataProvider

$dataProvider = $model->search();

and edit property "sort"

$dataProvider->sort = array(
  'defaultOrder'=>'order ASC'
);

In the 'search($order)' method you can put:

$criteria=new CDbCriteria;
$criteria->order = $order; // $order = 'order ASC' in your example.

return new CActiveDataProvider(YourModel::model(), array(
            'criteria'=>$criteria,
        ));
public function search()
{
    $criteria = new CDbCriteria;

    // build your criteria here

    // $criteria->order = 'something'; would be needed if you did `find?()` to return ordered array

    // Because CGridView|CListView uses data provider ...
    return new CActiveDataProvider( $this, array(
        'criteria' => $criteria,
        'sort' => array(
            'attributes' => array(
                '*',
            ),
            // ... so you need to set the default order for order to work
            'defaultOrder' => array(
                'order' => CSort::SORT_ASC,
            ),
        ),
    ));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top