How can I sort with a customized gridview
header?
Please give the difference between label
and header
in the Yii2
gridview
widget dataprovider
.
Here is my code:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
[
'class' => 'yii\grid\DataColumn',
'value' => function ($data) {
return $data->myTitle;
},
'headerOptions' => ['style'=>'text-align:center'],
'header' => 'Page Title',
'label' => 'Title'
],
]); ?>
Do header
and label
perform the same function?
How can I perform sorting in $data->myTitle
?
Here my Output Screen:
I want Page Title, Status, Date Modified should be active.
Thanks in advance.
I am not very sure I understand your question, but sorting option can be included in your
modelsearch.php
. So in your case you have to do like this.if
myTitle
is a field in the database, why you are using such a long syntax. Justshould work fine and should be active for sorting as you want
if you want a different header/label for the column, use label instead of header as header is only a cell content and cannot be used for sorting, while label can. details
Found the answer.
Please add attributes to ActiveDataProvider in your Search Model.
Add the attribute option in widget:
Since
myTitle
is a field from the database and not a custom value, you can just use attribute. The rest may be unnecessary e.g the defaultclass
isDataColumn