Reputation: 784
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>';
}
Upvotes: 2
Views: 1289
Reputation: 23738
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>';
}
],
],
]);
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]]);
Upvotes: 1