default sorting
Closed this issue · 1 comments
sajtosnorbert commented
Hi!
I am trying to set a default sort for the $dataProvider in the index list.
I added the 'title' (which is a multilingual field) to the grid columns.
I am trying to sort the list by adding the defaultOrder attribute.
$dataProvider->setSort( [
'defaultOrder' => [ 'title' => SORT_ASC ],
] );
Got this:
'undifined index: title'
Where and what should I modify to easyliy use your behavior for sorting too.
Thanks for your answer!
Norbert
OmgDef commented
@sajtosnorbert Hi!
I think you should do something like this
$query = Post::find()->joinWith(['translation' => function ($query) {
$language = Yii::$app->language;
// $language = substr($language, 0, 2);
$query->where(['language' => $language]);
}])->orderBy('{{%postLang}}.title DESC');
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
Also you should read this article
http://www.yiiframework.com/wiki/653/displaying-sorting-and-filtering-model-relations-on-a-gridview/