Yii2 GridView Customize Header Row

邮差的信 提交于 2019-12-09 02:15:21

问题


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>

回答1:


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



回答2:


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,
 ],


来源:https://stackoverflow.com/questions/29791440/yii2-gridview-customize-header-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!