DISPLAY RELATED ATTRIBUTE IN GRID VIEW AND DETAIL VIEW IN YII 2

 


At view\index.php of Proctor

<?= GridView::widget([
 'dataProvider' => $dataProvider,
 'filterModel' => $searchModel,
 'columns' => [
 ['class' => 'yii\grid\SerialColumn'],
 'proctorEmail:email',
 'proctorFullName',
 'company',
 'mobileNo',
[
       'attribute' => 'location',
       'value' => function($model) {
        return $model->location == 1 ? 'Local' : 'Abroad';
         },
       'filter' => [
             1 => 'Local',
             2 => 'Abroad'
        ]
 ],
 [
       'attribute' => 'listofvenues.displayname',
       'format' => 'text',
       'label' => 'Primary Venue',
],

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

“listofvenues.displayname” came from a function in the model of this Proctor class.

public function getListofvenues()
 {
 return $this->hasOne(Venues::className(), ['id' => 'listofvenues_id']);
 }

At the model of Venue Class

public function getDisplayName()
{ 
         return $this->name;
}

At proctor/view.php

<?= DetailView::widget([
 'model' => $model,
 'attributes' => [
 'proctorID',
 'proctorEmail:email',
 'proctorFullName',
 'mailingAddress',
 'citizenship',
 'educAttainment',
 'position',
 'company',
 'companyWebsite',
 'zipCode',
 'telNo',
 'mobileNo',
 'faxNo',
 'birTIN',
 [
 'attribute' => 'location',
 'value' => $model->location == 1 ? 'Local' : 'Abroad',
 ],
 [ 
 'attribute' => 'listofvenues.name',
 'label' => 'Primary Venue',
 ],
 ],
 ]) ?>

Post a Comment

0 Comments