2

如何添加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>';
}
4

1 回答 1

1

您需要将formatfor the column更改为rawfor 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>';
            }
        ],
    ],
]);

编辑

除了退货之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]]);
于 2018-09-11T07:28:56.667 回答