在 yii2 table 小部件中显示时合并两列

Merge two columns while display in yii2 table widget

我使用 yii 显示 table 的代码是

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

                    'HRMS_candidateFirstName',
                    'HRMS_candidateLastName',
                     'HRMS_candidaterRefType',
                     'HRMS_candidateStatus',
                   ['class' => 'yii\grid\ActionColumn'],
                ],
            ]);

它将名字和姓氏打印为不同的列。像这样

我想要这样

我在文档中搜索但找不到。

怎么做?

您必须构建一个计算列

在这里你可以找到一个很好的tutorial 本质上,您必须向模型中添加一个 calculate 列的函数,例如

模型中

/* Getter for person full name */
public function getFullName() {
   return $this->first_name . ' ' . $this->last_name;
}

/* Your model attribute labels */
public function attributeLabels() {
   return [
       /* Your other attribute labels */
       'fullName' => Yii::t('app', 'Full Name')
   ];
}

视图

echo GridView::widget([
  'dataProvider' => $dataProvider,
  'filterModel' => $searchModel,
  'columns' => [
      ['class' => 'yii\grid\SerialColumn'],
      'id',
      'fullName',
      ['class' => 'yii\grid\ActionColumn'],
  ]
]);

如果您不需要 filer 和 sortig,这就是全部,否则您可以在教程中找到您需要的内容..

可以直接在视图文件中这样完成-

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        'id',
        [
            'attribute' => 'an_attributeid',
            'label' => 'yourLabel',
            'value' => function($model) { return $model->first_name  . " " . $model->last_name ;},
        ],
        ['class' => 'yii\grid\ActionColumn',],
    ],
]);?>