如何在以下函数中添加data-id
:
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>';
}
],
],
]);
以下是data-id
属性,该属性未在浏览器中显示:
"value"=>function($model){
return '<div class="myClass" data-id="'.$model->id.'">'.$model->scale.'</div>';
}
答案 0 :(得分:1)
您需要将列的format
更改为列的raw
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>';
}
],
],
]);
除了返回html
之类的
return '<div class="myClass" data-id="'.$model->id.'">'.$model->scale.'</div>';
您可以使用yii\helpers\Html::tag($name,$content,$options[])
创建div标签,如下所示。
return Html::tag('div',$model->scale,['class'=>'myClass','data'=>['id'=>$model->id]]);