Yii2 data-* attributes not rendered using GridView Column

Shringiraj Dewangan

How do add data-id in below function:

GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        [
            'contentOptions' => ['class' => "text-center"],
            'attribute' => 'scale',
            "format"=>"Html",
            "value"=>function($model){
                return '<div class="myClass" data-id="'.$model->id.'">'.$model->scale.'</div>';
            }
        ],
    ],
]);

Here is data-id attribute which is not shown in the browser:

"value"=>function($model){
    return '<div class="myClass" data-id="'.$model->id.'">'.$model->scale.'</div>';
}
Muhammad Omer Aslam

You need to change the format for the column to raw for the column

GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        [
            'contentOptions' => ['class' => "text-center"],
            'attribute' => 'scale',
            "format"=>"raw",
            "value"=>function($model){
                return '<div class="myClass" data-id="'.$model->id.'">'.$model->scale.'</div>';
            }
        ],
    ],
]);

EDIT

Apart from returning the html like,

return '<div class="myClass" data-id="'.$model->id.'">'.$model->scale.'</div>';

You can use the yii\helpers\Html::tag($name,$content,$options[]) to create a div tag see below.

return Html::tag('div',$model->scale,['class'=>'myClass','data'=>['id'=>$model->id]]);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related