GridView column sort

Hi , I m using GridView::widget(

‘dataProvider’ => $dataProvider,

        'layout' => "{items}{summary}{pager}",


        'options' => ['class' => 'grid-view table-responsive'],


        'tableOptions' => ['class' => 'table table-bordered table-striped table-hover'],


        'columns' => [


            [


                'class' => 'yii\grid\SerialColumn',


            ],


            // Data columns


            [


                'class' => 'yii\grid\DataColumn',


                'attribute' => "person_id",


                'content' => function ($model) {


                    if (!empty($model->person->id)) {


                        return $model->person->id;


                    }


                },


            ],

]

I want to make sort by person id, can anyone tell me how to do that ?

Default sort or sortable column?

Yes I solve it with sortable column, on the main query in my model just add :

$query->andFilterWhere([‘person.id’ => $this->id]);

$query->orderBy(‘person.id DESC’);

This is not totally right, because you can’t sort using other columns (clicking on gridview header).

You should set sort property of DataProvider, es:




        $dataProvider->setSort([

            'defaultOrder' => ['person.id'=>SORT_ASC],

        ]);	



So you have set default sort and you can also sort again clicking on gridview header.