How to show two attribute values ​​in one column through relation in Yii 2 GridView

I have a gridview in the index, I want to show the width and height as in one column, how can I do it here is the view code

<?= GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ ['class' => 'yii\grid\SerialColumn'], 'fld_id', 'fld_name', [ 'label' => 'Material Name', 'attribute' => 'fld_material_id', 'value' => 'fldMaterial.fld_name', ], [ 'label' => 'Size', 'attribute' => 'fld_size_id', 'value' => 'fldSize.fld_width', ], // 'fld_size_id', ['class' => 'yii\grid\ActionColumn'], ], ]); ?> 

I have an fldSize relation in the model, only fld_width is just displayed here. I want to show it in fld_width format. "x" .fld_height, how can I do this in Yii2

+6
source share
2 answers

You should just use the value callback, for example.

 [ 'label' => 'Size', 'attribute' => 'fld_size_id', 'value' => function ($model) { return $model->fldSize->fld_width . 'x' . $model->fldSize->fld_height; }, ], 
+14
source

Sorry after more than one year, but it works (not a drop-down list, but Select2). Here is the code for the form

  <?= $form->field($model, 'ID_MACH')->widget(Select2::classname(), [ 'data'=> ArrayHelper::map(Maschines::find()->all(),'ID_MACH','fullname'), 'language'=>'ru', 'theme'=>'krajee', 'options'=>['placeholders'=>'suda...', 'prompt'=>'10-'], 'pluginOptions'=>[ 'allowclear'=>true ], 

Next model for cars:
public function getFullName() { return $this->customer.','.$this->Manufacturer.','.$this->type.','.$this->serial;}

+1
source

Source: https://habr.com/ru/post/980907/


All Articles