Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

gridview

yii2

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

like image 586
Khan Avatar asked Jan 13 '15 05:01

Khan


2 Answers

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;
    },
],
like image 83
soju Avatar answered Sep 22 '22 19:09

soju


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;}

like image 34