简体   繁体   中英

How to set default sort by value in yii2?

Is it possible to sort using value not using column name?

For example,

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

I know i can easily do this with controller or adding params but i want to do it using search() function.

By default, sorting works with column name, So, i am finding new workaround for sort by value.

You can use setSort

    $dataProvider->setSort([
         'defaultOrder' => [ 'id' => SORT_ASC],
    ]);

otherwise your request is not satifisied this format because in not based on an array of column and order_type.

You can try override this setting the ->orderBy(' FIELD(id, '100', '300', '500')') ;

$dataProvider->query->orderBy(' FIELD(id, '100', '300', '500')') 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM