How do I pass a array as a param for a Yii2 gridview column

11,505

Solution 1

From the source code for \Yii\grid\Column

@var callable This is a callable that will be used to generated the content of each cell. The signature of the function should be the following: function ($model, $key, $index, $column)

As Mihai has correctly pointed out you can use use() to include variables into the function's scope as follows:

"content" => function($model, $key, $index, $column) use ($arr_judete_v2) {
    return $arr_judete_v2[$model['county_id']]['nume'];
}

Please note that the variables are copied into the function and as such any changes will not affect the variable outside of the function. A better explanation of this is given in this answer.

Solution 2

Use use (), see how the function is defined for the value of the column.

        $invoice_status_data = array('' => 'Select a status') + ArrayHelper::map(InvoiceStatus::find()->asArray()->all(), 'id', 'name');
    ........................
            'columns' => [
    ........................
                        [
                            'attribute'=>'Contact.name',
                            'format'=>'raw',
                            'value'=>function ($model, $key, $index, $widget) use ($invoice_status_data) {
.............................
                                    $content .= 
                                        Html::dropDownList('dropdown'. $model->id, '', $invoice_status_data, [
                                            'class' => 'form-control', 
                                            'onchange' => 'javascript: myInvoice.change($(this));', 
                                            'data-href' => Url::toRoute(['invoice/change-status', "Invoice_id"=>$model->id])]);


                                return $content;
                            },
                        ],
Share:
11,505
Ionut Flavius Pogacian
Author by

Ionut Flavius Pogacian

WILL.I.AM says it all

Updated on June 05, 2022

Comments

  • Ionut Flavius Pogacian
    Ionut Flavius Pogacian almost 2 years

    I am trying to pass $arr_judete_v2 as a param to a callback function in a gridview and it does not work;

    $model['county_id'] returns a number

    $arr_judete_v2[1]['nume'] returns a name

    my issue:

    [
                        'attribute' => 'county_id',
                        'label' => Yii::t('diverse', 'Judet'),
                        'content' => function($model, $arr_judete_v2) {
                            return $arr_judete_v2[$model['county_id']]['nume'];
                        },
                    ],
    

    the entire gridview

    <?php
        echo GridView::widget([
            'layout' => "{items}",
            'dataProvider' => $dataProvider,
            'columns' => [
                'id',
                [
                    'attribute' => 'nume',
                    'label' => Yii::t('companie', 'nume'),
                ],
                'cui',
                'email',
                [
                    'attribute' => 'county_id',
                    'label' => Yii::t('diverse', 'Judet'),
                    'content' => function($model, $arr_judete_v2) {
                        return $arr_judete_v2[$model['county_id']]['nume'];
                    },
                ],
                [
                    'class' => 'yii\grid\ActionColumn',
                    'template' => '{update} {delete}',
                    'buttons' => [
                        'update' => function ($url, $model) {
                            return Html::a('<span class="glyphicon glyphicon-pencil"></span>', ['update', 'id' => $model['id']], [
                                        'title' => Yii::t('yii', 'Update'),
                                        'data-pjax' => '0',
                            ]);
                        }
                    ]
                ],
            ],
        ]);
    
  • Mihai P.
    Mihai P. over 9 years
    you can actually use 'value'=>function ($model, $key, $index, $widget) use (list of variables)
  • topher
    topher over 9 years
    I didn't know you could do that. Thanks!