【问题标题】:How do I pass a array as a param for a Yii2 gridview column如何将数组作为 Yii2 gridview 列的参数传递
【发布时间】:2014-08-16 02:22:37
【问题描述】:

我试图将$arr_judete_v2 作为参数传递给gridview 中的回调函数,但它不起作用;

$model['county_id'] 返回一个数字

$arr_judete_v2[1]['nume'] 返回一个名字

我的问题:

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

整个网格视图

<?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',
                        ]);
                    }
                ]
            ],
        ],
    ]);

【问题讨论】:

    标签: php yii yii2


    【解决方案1】:

    来自the source code for \Yii\grid\Column

    @var callable 这是一个可调用对象,用于生成每个单元格的内容。 该函数的签名应为:function ($model, $key, $index, $column)

    作为Mihai has correctly pointed out,您可以使用use() 将变量包含到函数的作用域中,如下所示:

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

    请注意,变量被复制到函数中,因此任何更改都不会影响函数外部的变量。 in this answer 对此给出了更好的解释。

    【讨论】:

    • 你实际上可以使用'value'=>函数($model, $key, $index, $widget)使用(变量列表)
    • 我不知道你能做到这一点。谢谢!
    【解决方案2】:

    使用use(),看看函数是如何为列的值定义的。

            $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;
                                },
                            ],
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-21
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 2017-03-22
      • 2011-08-12
      • 2016-09-26
      相关资源
      最近更新 更多