Monday, March 7, 2022

[FIXED] Merge two columns while display in yii2 table widget

Issue

My code using yii to display table is

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

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

It prints First name and last name as different col. Like this enter image description here

I want it like this

enter image description here

I searched in documentation but not able to find.

How to do this?


Solution

You must build a calculated column

here you can find a very good tutorial Essentially you must add to your model a function for calculate the column like this

In Model

/* 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')
   ];
}

and the in view

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

If you don't need filer and sortig this is all otherwise you can find wath you need in the tutorial..



Answered By - ScaisEdge

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.