如何在yii2中向gridview添加新列?

时间:2017-04-11 12:24:30

标签: yii2

我的GridView中有4列,其中包含我需要的数据,但现在我还需要一列。如何在我的GridView中创建它?

    <?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],

        'id',
        'name',
        'surname',
        'employment_date',

        ['class' => 'yii\grid\ActionColumn'],
      ],
    ]); ?>

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

您可以根据yii api添加更多列:

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
    [
        ['class' => 'yii\grid\SerialColumn'],
        'id',
        'name',
        'surname',
        'employment_date',

        [
            'class' => DataColumn::className(), // this line is optional
            'attribute' => 'name',
            'format' => 'text',
            'label' => 'Name',
        ],
        ['class' => CheckboxColumn::className()],
        ['class' => 'yii\grid\ActionColumn'],
    ]
]); ?>

您可以阅读this link

答案 1 :(得分:0)

<?= GridView::widget([
    ....
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],

        'id',
        'name',
        'surname',
        'employment_date',
        'columns'=>[
             'attribute'=>'New Column',
             'value'=>'tbl.new_column'    
        ],
        //OR This
        new_column,
        ['class' => 'yii\grid\ActionColumn'],
      ],
    ]); ?>