D
D
Denis2017-10-11 08:09:26
Yii
Denis, 2017-10-11 08:09:26

How to implement column sorting in gridView added from linked table in Yii2?

How to implement column sorting in a GridView that is added from a related table. All columns are sorted except this one. Please be consistent: what needs to be added to the Controller, what to the Model ..

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim Fedorov, 2017-10-11
@qonand

in the data provider, you need to specify data on how to sort this attribute, for example:

$dataProvider->sort->attributes['author.name'] = [
    'asc' => ['author.name' => SORT_ASC],
    'desc' => ['author.name' => SORT_DESC],
];

but it is worth considering that for sorting, related data must be loaded using joinWith, and not with
PS maybe you should learn to read the documentation or just google , so as not to ask questions that are well described everywhere?

M
Maxim Fedorov, 2017-10-11
@Maksclub

in index.php in GridView
in the example below, the connection with City by the city_id field

[
                'attribute' => 'city_id',
                'filter' => City::find()->select(['name', 'id'])->indexBy('id')->column(),
                'value' => ...
            ]

A
Alexander Urich, 2017-10-13
@Urichalex

Specify a property in the search model, and dance from it in search().
Specify this parameter in DataProvider in sort

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question