Khan
Khan

Reputation: 97

How can I show two attributes values in one column through relation in Yii 2 GridView

i have Gridview in index i want to show width and height both 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 relation fldSize in model here it is just only displaying fld_width i want to show it in the format fld_width."x".fld_height how can i do it in Yii2

Upvotes: 6

Views: 9753

Answers (2)

Alexandr Kosarevskiy
Alexandr Kosarevskiy

Reputation: 51

Sorry that after more than one year, but it works (not a dropdown 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 is the Model for Mashines:
public function getFullName() { return $this->customer.','.$this->Manufacturer.','.$this->type.','.$this->serial;}

Upvotes: 1

soju
soju

Reputation: 25322

You should simply use value callback, e.g. :

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

Upvotes: 14

Related Questions