1

我已经为模型Faq生成了 giix crud

控制器:

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

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

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

查看 - 管理员:

<?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',
        ),
    ),
)); ?>

我想按字段“订单”订购商品,所以我添加 array('order'=>'order ASC') 了,$model->search();但这并没有改变任何东西。错在哪里?

4

4 回答 4

8

更新模型中的 search() 函数并添加以下代码

'criteria'=>$criteria,
//add here

'sort'=>array(
    'defaultOrder'=>'order ASC',
),

并在管理视图中将 dataprovider 更改为此

'dataProvider' => $model->search(),
于 2013-09-16T07:24:09.367 回答
5

获取 CActiveDataProvider

$dataProvider = $model->search();

并编辑属性“排序”

$dataProvider->sort = array(
  'defaultOrder'=>'order ASC'
);
于 2013-09-16T11:51:14.523 回答
1

在“ search($order) ”方法中,您可以输入:

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

return new CActiveDataProvider(YourModel::model(), array(
            'criteria'=>$criteria,
        ));
于 2013-09-16T07:39:13.497 回答
1
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,
            ),
        ),
    ));
}
于 2013-09-16T08:08:54.103 回答