Reputation: 149
I learn yii2, tried to make GridView::widget with sortable date column.
I found an easy way, use: 'columns' => ['created_at:datetime']
but I don't understand how to make the same with custom way.
Here is my code:
<?=GridView::widget([
'dataProvider' => $dataProvider,
'columns' =>
[
['class' => 'yii\grid\SerialColumn'],
'created_at:datetime', //an easy way, makes default asc/desc sort on link click
[
'header' => "Date", //the same, but how to make sort, (need to add link someway)?
'format' => 'datetime',
'value' => 'created_at',
'contentOptions'=>['style'=>'width: 130px;',
'enableSorting' => true,
]
],
],
'options' => ['class'=>'doc-table'],
]);
?>
Here is $dataProvider
:
$dataProvider->sort = ['defaultOrder' =>
//['created_at' => 'desc'],
['attributes' =>
['created_at' =>
[
'asc' => ['created_at' => SORT_ASC],
'desc' => ['created_at' => SORT_DESC],
]
]
],
];
return $this->render('index', [
'dataProvider' => $dataProvider,
]);
Upvotes: 1
Views: 1482
Reputation: 3818
You use attribute name On field of 'created_at'.
Like..
[
'attribute' => 'created_at',
'header' => 'date',
'Value' => .....,
]
Upvotes: 1