R
R
Ravenenok2015-11-11 17:10:08
Yii
Ravenenok, 2015-11-11 17:10:08

How to sort column with date in Yii2 GridView?

The data is selected from the database, we get an array that we want to display using the GridView and in which we want to be able to sort by clicking on the create_date column (the date format in it is dd.mm.yyyy). Now sorting by the create_date column works, I had to introduce an additional create_date_unix column (it contains the Unix date format), by which we sort the date. In my opinion, this is a crutch, and there must be a way for the date column to be sorted without using additional values, but not as text (in this case, sorting is by number, and only then by month), but as a date.
How to achieve this? The code below works and implements sorting on the create_date column, but using a helper column.

$memos = yii::$app->db->createCommand('SELECT * FROM dbo.v_memos_list
                                                    WHERE agency_number = :agency_number order by create_date_unix desc')
                                            ->bindValue(':agency_number',$agency_number)
                                            ->queryAll();

Our data source will be ArrayDataProvider, which is defined as follows
$dataProvider = new ArrayDataProvider([
            'allModels' => $memos,
            'pagination' => [
                'pageSize' => 17,
            ],
            'sort' => [
                'attributes' => ['id', 'subtype', 'status', 'reg_id', 'create_date'=> [
                    'asc' => [
                        'create_date_unix' => SORT_ASC,
                    ],
                    'desc' => [
                        'create_date_unix' => SORT_DESC,
                    ],
                    'label' => 'Создано',
                    'default' => SORT_ASC
                ]],
            ]
        ]);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Borisovich, 2015-11-13
@Alexufo

the date format in it is dd.mm.yyyy

Here she is, friend and problem. You need to store it backwards yyyy.mm.dd and there will be no problems with sorting

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question