1

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,
    ]);
1

1 Answer 1

1

You use attribute name On field of 'created_at'.

Like..

[
'attribute' => 'created_at',
'header' => 'date',
'Value' => .....,
]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.