Yii2 GridView自定义标题行 [英] Yii2 GridView Customize Header Row

查看:132
本文介绍了Yii2 GridView自定义标题行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 <?= GridView :: widget([
' dataProvider'=> $ dataProvider,
'columns'=> [
['class'=>'yii\grid\SerialColumn'],
['label'= >'训练分数',
'属性'=>'scoreTraining',
'format'=> ['decimal',2],
],
[ 'label'=>'考试分数',
'属性'=>'scoreExam',
'format'=> ['decimal',2],
],
],
]);

正常情况下,标题名称将为培训分数和考试分数



yii2 gridview可以自定义标题行吗?所以我的标题行看起来像2行。



< table border = 1> < tr>< th>培训< br>分数第三次考试分数< / th>< / tr>< / table>

为了达到这个目的,使用标题属性而不是标签解决方案 / p>

 <?= GridView :: widget([
'dataProvider'=> $ dataProvider,
'columns'=> [
['class'=>'yii\grid\SerialColumn'],
[
'header'=>'训练< br> Score',
'attribute'=>'scoreTraining',
'format'=> ['decimal',2],
],
[
'header'=>'Exam'Score',
'attribute'=>'scoreExam',
'format'=> ['decimal',2],
],
],
]);

这种HTML内容不会被编码。



官方文件:


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:

这篇关于Yii2 GridView自定义标题行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆