5

I have normal ModelSearch with ActiveDataProvider, and I would like to add a virtual/related attribute to sorting in gridview. If I'm doing with setSort, and I'm adding this only attribute, then all other attributes are not sortable any more. Is there a built-in way to add an attribute to Sort? Thanks a lot!

public function search($params) {
    $query = Za::find();

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
        'sort' => ['defaultOrder' => ['aonr' => SORT_ASC]],
        'pagination' => [
            'pageSize' => 15,
        ],
    ]);

$dataProvider->setSort([
    'attributes' => [
        'lwnr' => [
            'asc' => ['lwnr' => SORT_ASC],
            'desc' => ['lwnr' => SORT_DESC],
            'label' => 'lwnr',
            'default' => SORT_DESC,
        ],
    ]
]);

$this->load($params);
...
}
user2511599
  • 796
  • 1
  • 13
  • 38
  • update your question and add your related ModelSearch code .. please – ScaisEdge Mar 16 '17 at 08:44
  • [This will help](http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/) – Insane Skull Mar 16 '17 at 09:01
  • Of course but as I said, if I'm using setSort, then all other attributes stop sorting and I would have to define all other attributes also ince again. The question is, if there is a built-in solution to **add** a new attribute to Sort next to the existing ones. – user2511599 Mar 16 '17 at 09:26
  • In your code i don't see any "virtual/related" attribute and any setSort .. .. please show the minimal (consistent) code relate to you question .. – ScaisEdge Mar 16 '17 at 09:48

1 Answers1

6

You can set the sortable attributes with the setSort method, but in this case you need to set all the columns you want to sort, not just the column from the relation.

If you want to add one column you can try this (merging the currently existing sort attributes with the new one):

    $dataProvider->setSort([
        'attributes' => array_merge(
            $dataProvider->getSort()->attributes,
            [
                'lwnr' => [
                    'asc'     => ['lwnr' => SORT_ASC],
                    'desc'    => ['lwnr' => SORT_DESC],
                    'label'   => 'lwnr',
                    'default' => SORT_DESC,
                ],
            ]
        ),
    ]);

or you can add the missing attributes/columns by hand (which is a far better idea)

    $dataProvider->setSort([
        'attributes' =>
            [
                'lwnr' => [
                    'asc'     => ['lwnr' => SORT_ASC],
                    'desc'    => ['lwnr' => SORT_DESC],
                    'label'   => 'lwnr',
                    'default' => SORT_DESC,
                ],
                // Other attribute
                'id'   => [
                    'asc'  => ['id' => SORT_ASC],
                    'desc' => ['id' => SORT_DESC],
                ],
                ...
            ],
    ]);

Another way:

$dataProvider->getSort()->attributes['lwnr'] = [
                    'asc'     => ['lwnr' => SORT_ASC],
                    'desc'    => ['lwnr' => SORT_DESC],
                    'label'   => 'lwnr',
                    'default' => SORT_DESC,
                ];
Szántó Zoltán
  • 981
  • 1
  • 12
  • 26
  • thank you very much, that's exactly what I needed (the upper one). – user2511599 Mar 16 '17 at 13:15
  • why is the lower one a far better idea? – user2511599 Mar 16 '17 at 13:45
  • As you noticed the attributes field has default data (the columns in your db table). So with the first example: every time a page is rendered, the default sort attributes are generated, accessed, merged with your new value and the result is reassigned. This is unnecessary because you don't have dynamic columns in your db table and you can define these values manually. Ps.: Try out the third example from the answer which is just updated – Szántó Zoltán Mar 16 '17 at 13:53