2

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>';
}

1 Answer 1

1

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]]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.