In my view code I have this:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
['label' => 'Training Score',
'attribute' => 'scoreTraining',
'format' => ['decimal',2],
],
['label' => 'Exam Score',
'attribute' => 'scoreExam',
'format' => ['decimal',2],
],
],
]);
Normally the header name will be "Training Score" and "Exam Score"
Is that possible in yii2 gridview to customize the header row? so that my header row looks like in 2 line..
<table border=1>
<tr><th>Training <br> Score</th><th>Exam <br> Score</th></tr>
</table>
To achieve that, use header
property instead of label
:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'header' => 'Training <br> Score',
'attribute' => 'scoreTraining',
'format' => ['decimal', 2],
],
[
'header' => 'Exam <br> Score',
'attribute' => 'scoreExam',
'format' => ['decimal', 2],
],
],
]);
That way HTML content won't be encoded.
Official docs:
Use the 'label' attribute to set the header:
http://www.yiiframework.com/doc-2.0/yii-grid-datacolumn.html#$label-detail
This way the sorting functionality will still work.
Use 'encodeLabel' => false to allow HTML-entities like
to work:
http://www.yiiframework.com/doc-2.0/yii-grid-datacolumn.html#$encodeLabel-detail
Example:
[
'attribute' => 'firstname',
'label' => 'First <br /> Name',
'encodeLabel' => false,
],
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With