未使用GridView列呈现的Yii2 data- *属性

时间:2018-09-11 07:07:02

标签: yii yii2 yii2-grid

如何在以下函数中添加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>';
}

1 个答案:

答案 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]]);