Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 GridView Customize Header Row

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>
like image 793
Wonka Avatar asked Apr 22 '15 08:04

Wonka


2 Answers

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:

  • $header
like image 183
arogachev Avatar answered Nov 17 '22 09:11

arogachev


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,
 ],
like image 32
Herbert Maschke Avatar answered Nov 17 '22 07:11

Herbert Maschke