Answer the question
In order to leave comments, you need to log in
How to sort by calculated fields in GridView?
By default, only those fields that are in the model table are available for sorting in the GridView.
The task is to sort by calculated fields. There is a tutorial, tyk , but it just gets the value through the link and pushes it into setSort in the search method:
$dataProvider->setSort([
'attributes' => [
'id',
'countryName' => [
'asc' => ['tbl_country.country_name' => SORT_ASC],
'desc' => ['tbl_country.country_name' => SORT_DESC],
'label' => 'Country Name'
]
]
]);
$query = Model::find();
$select = ['*', '(SELECT %MY QUERY WITH COUNT JOINS ETC%) AS calculatedField'];
$query->select($select);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->setSort([
'attributes'=>[
'calculatedField' => [
'asc'=>['calculatedField'=>SORT_ASC,],
'desc'=>['calculatedField'=>SORT_DESC],
],
]
]);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question