Cliassi
Cliassi

Reputation: 81

ArrayDataProvider edit value in GridView

In my code i want to edit values. I try $model but it's work only in ActiveDataProvider. When i use $data problem is the same - result is 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>';
                    },
            ],
        ],
    ]
); ?>

and my result in price and total is 0. In ActiveDataProvider using $model is work but in my code doesn't work. Please help me. How can I edit my price or total value?

controller

public function actionShowCart()
{

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



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

my session 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" } }

Upvotes: 0

Views: 1205

Answers (1)

Cliassi
Cliassi

Reputation: 81

I find the answer!

I have to use $data as array!

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

Upvotes: 1

Related Questions