105

在 Yii 1.1 中,此代码适用于默认排序:

$dataProvider = new CActiveDataProvider('article',array(
    'sort'=>array(
        'defaultOrder'=>'id DESC',
    ),
));

Yii2如何设置默认排序?

尝试以下代码,但没有结果:

$dataProvider = new ActiveDataProvider([
    'query' => $query,
    'sort' => ['defaultOrder'=>'topic_order asc']
]);
4

8 回答 8

189

我认为有适当的解决方案。配置yii\data\Sort对象:

 $dataProvider = new ActiveDataProvider([
     'query' => $query,
     'sort'=> ['defaultOrder' => ['topic_order' => SORT_ASC]],
 ]);

官方文档链接

于 2014-04-10T16:43:14.427 回答
42

或者

       $dataProvider->setSort([
        'defaultOrder' => ['topic_order'=>SORT_DESC],
        'attributes' => [...
于 2015-02-27T13:47:12.507 回答
17

defaultOrder包含一个数组,其中键是列名,值是 a ,SORT_DESC或者 SORT_ASC这就是下面代码不起作用的原因。

$dataProvider = new ActiveDataProvider([
        'query' => $query,
        'sort' => ['defaultOrder'=>'topic_order asc']
    ]);

正确的方式

$dataProvider = new ActiveDataProvider([
    'query' => $query,
    'sort' => [
        'defaultOrder' => [
            'topic_order' => SORT_ASC,
        ]
    ],
]);

注意:如果查询已经指定了 orderBy 子句,则最终用户(通过排序配置)给出的新排序指令将附加到现有的 orderBy 子句中。任何现有的限制和偏移子句都将被最终用户的分页请求覆盖(通过分页配置)。

您可以详细了解 数据提供者的 Yii2 指南

通过在查询中传递 Sort 对象进行排序

 $sort = new Sort([
        'attributes' => [
            'age',
            'name' => [
                'asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC],
                'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC],
                'default' => SORT_DESC,
                'label' => 'Name',
            ],
        ],
    ]);

    $models = Article::find()
        ->where(['status' => 1])
        ->orderBy($sort->orders)
        ->all();
于 2016-05-10T13:09:07.393 回答
11

如果您有 CRUD(索引)并且您需要为 GridView 或 ListView 或更多设置默认排序控制器...示例

public function actionIndex()
{
    $searchModel = new NewsSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    // set default sorting
    $dataProvider->sort->defaultOrder = ['id' => SORT_DESC];

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}

你需要添加

$dataProvider->sort->defaultOrder = ['id' => SORT_DESC];
于 2018-11-08T15:46:25.550 回答
4

试试这个

$dataProvider = new ActiveDataProvider([
    'query' => $query,
]);

$sort = $dataProvider->getSort();

$sort->defaultOrder = ['id' => SORT_ASC];

$dataProvider->setSort($sort);
于 2018-06-07T11:04:09.453 回答
2
$dataProvider = new ActiveDataProvider([ 
    'query' => $query, 
    'sort'=> ['defaultOrder' => ['iUserId'=>SORT_ASC]] 
]);
于 2020-02-29T12:02:51.557 回答
-1

你可以像这样修改搜索模型

$dataProvider = new ActiveDataProvider([
        'query' => $query,
        'sort' => [
            'defaultOrder' => ['user_id ASC, document_id ASC']
        ]
    ]);
于 2017-11-25T10:50:15.473 回答
-2
    $modelProduct       =   new Product();
    $shop_id            =   (int)Yii::$app->user->identity->shop_id;

    $queryProduct       =   $modelProduct->find()
                            ->where(['product.shop_id'  => $shop_id]);


    $dataProviderProduct    =   new ActiveDataProvider([
                                    'query'         =>  $queryProduct,
                                     'pagination' => [ 'pageSize' => 10 ],
                                    'sort'=> ['defaultOrder' => ['id'=>SORT_DESC]]
                                ]); 
于 2018-12-17T10:54:12.610 回答