GridView set default sort column

If you want to sort GridView by a specific column by default, the following code will do so.

  1. $dataProvider = new ActiveDataProvider([
  2.     'query' => $query,
  3.     'sort'=>[
  4.         'defaultOrder'=>['attribute'=> SORT_DESC],
  5.     ],
  6. ]);

Change ‘attribute’ to the name of the attribute to be sorted by.

Sort options are: SORT_DESC and SORT_ASC

Published by

Joel Bowers

Web developer since 1999. PHP, YII2, Laravel, Javascript, HTML, CSS, jQuery, Perl, Wordpress, MySQL.

Leave a Reply

Your email address will not be published. Required fields are marked *