Answer the question
In order to leave comments, you need to log in
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();
$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
the date format in it is dd.mm.yyyy
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question