Search code examples
phphtmlgridviewyii2formatting

How to put custom HTML into Yii2 GridView header?


There's this <abbr></abbr> tag in bootstrap that will automatically shows popup of the abbreviated word. I want to insert this tag to a certain header in the gridview with attribute name act. Here is my code so far.

        [
            'attribute'=>'act',
            'format'=>'raw',
            'label'=>'<abbr title="Area Coordinating Team">ACT</abbr>',
            'value'=>function($model){
              return '<span class="fa fa-thumbs-up text-green"></span>';
            }
        ],

but the output literally shows the whole <abbr title="Area Coordinating Team">ACT</abbr>

enter image description here


Solution

  • I already answered that here.

    To achieve that, use header property instead of label:

    [
        'attribute' => 'act',
        'format' => 'raw',
        'header' => '<abbr title="Area Coordinating Team">ACT</abbr>',
        'value' => function ($model) {
            return '<span class="fa fa-thumbs-up text-green"></span>';
        },
    ],
    

    That way HTML content won't be encoded.

    Official docs: