ArrayDataProvider在GridView中编辑值

时间:2015-02-28 18:37:19

标签: php gridview yii2 cactivedataprovider

在我的代码中,我想编辑值。我尝试$ model但它只在ActiveDataProvider中工作。当我使用$ data时问题是一样的 - 结果是0。

<?= GridView::widget(
    [
        'dataProvider' => $dataProvider,
        'tableOptions' => ['class' => 'table table-hover show-products'],
        'columns'      => [
            [
                'attribute' => 'price',
                'label'     => 'Price',
                'format'    => 'html',
                'value'     => function ($data) {
                        return '<div class="product-list-row">' . str_replace(
                            '.', ",", $data->price / 100
                        ) . ' USD</div>';
                    },
            ],
            [
                'attribute' => 'total',
                'format'    => 'html',
                'value'     => function ($model) {
                        return '<div class="product-list-row">' . str_replace(
                            '.', ",", $model->price / 100 * $model->amount
                        ) . ' PLN</div>';
                    },
            ],
        ],
    ]
); ?>

我在price和total中的结果是0.在ActiveDataProvider中使用$ model是可行的但在我的代码中不起作用。请帮我。如何编辑我的价格或总价值?

控制器

public function actionShowCart()
{

    $dataProvider = new ArrayDataProvider([
                        'allModels' =>Yii::$app->session->get('cart'),
                        'sort'=>false,
                        'pagination' => [
                            'pageSize' => 10,
                        ],
                    ]);



    return $this->render('showCart', [
        'dataProvider' => $dataProvider,
    ]);
}

我的会话var_dump()

array(2) { [64]=> array(4) { ["id"]=> int(64) ["amount"]=> int(1) ["name"]=> string(12) "PRODUCT A" ["price"]=> string(4) "0.06" } [2159]=> array(4) { ["id"]=> int(2159) ["amount"]=> int(1) ["name"]=> string(15) "PRODUCS D" ["price"]=> string(5) "15.20" } }

1 个答案:

答案 0 :(得分:1)

我找到了答案!

我必须使用$ data作为数组!

[
                                'attribute' => 'total',
                                'format' => 'html',
                                'value' => function ($data) {
                                            return str_replace('.', ",", $data['price']*$data['amount']).' USD';
                                            },
                    ],