0

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" } }
Cliassi
  • 81
  • 3
  • 10
  • Can you post your controller? Also, can you clarify what you are using? You've tagged your post with yii2, and cactivedataprovider, but yii2 doesn't have Cactivedataprovider, that's yii1. Also, you say in your title that you're using ArrayDataProvider, but in your post you seem to be using ActiveDataProvider. Please clarify! – Joe Miller Mar 01 '15 at 04:13
  • Can you also post var_dump(Yii::$app->session->get('cart')) from in your controller please? I would like to see what that value contains. – Joe Miller Mar 01 '15 at 11:38

1 Answers1

1

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';
                                            },
                    ],
Cliassi
  • 81
  • 3
  • 10