shahid shahid - 3 months ago 25
PHP Question

how to add both class in one column in GridView widget YII2

I need to add both serial class and checkbox class in same column. please help me asap.thanks in advance...

<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\CheckboxColumn',
'header' => '',
'checkboxOptions' => function($model) {
if($model->featured=="Y"){
$bool =true;
}else{
$bool =false;
}

return ['checked' => $bool,'value'=>$model->user_id];},'contentOptions' => ['style'=>'text-align: center;font-size: 13px;',],
],

['class' => 'yii\grid\SerialColumn',
'header' => 'S.No',
'headerOptions' => ['style'=>'text-align: center;background-color: #868686;color: white;font-size: 13px;font-weight: bold;',],
'contentOptions' => ['style'=>'text-align: center;font-size: 13px;',],
],
], ]); ?>

Answer

Coud be you need format => raw and use anonymous function for value like this suggestion

<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
    ['class' => 'yii\grid\CheckboxColumn',
    'header' => '',
    'format' => 'raw',
    'value' => function($model,  $key, $index) {
        return "<input type='checkbox' name='select[]' ><span> <?= $index ?></span>"
    },
     ....
    ],

    .....
], ]); ?>
Comments