hello eveybody, can you help me.
I have some problem with local varible in gridview yii2.
first I define local varible $totalAmount = 0, and in gridview I calculator totalAmount and output to use in the footer but alway errror if use
// ‘footer’ => function () use ($totalAmount){
// return $totalAmount;// why have error here
// },
and value= 0 when I use ‘footer’=> $totalAmount,
Thanks every body
<?php $totalAmount = 0;// define varible to count totalAmount?>
<?php Pjax::begin(['id' => 'pjax-container', 'timeout' => false, 'enablePushState' => false, 'clientOptions' => ['method' => 'POST']]);
?>
<?= GridView::widget([
'dataProvider' => $orderDetail,
'tableOptions' => ['class' => 'table table-striped table-bordered table-hover','id'=>'table-basic'],
'layout'=>"{pager}\n{summary}\n{items}\n{pager}",
'summary' => "",
'showOnEmpty'=>true,
'showHeader'=>true,// show column name
'showFooter'=>true,// shot footer
'columns' => [
[
'class' => 'yii\grid\SerialColumn',
'contentOptions'=>['class'=>'w30 table-cell'],
],
'product_id:url',
[
'attribute' =>'quantity',
'headerOptions'=>['class'=>'text-center col-md-1'],
'contentOptions'=>['class'=>'text-center table-cell'],
],
[
'attribute' =>'price',
'headerOptions'=>['class'=>'text-center col-md-1'],
'format'=>'raw',
'value'=>function($model){
if($model->price>0){
return Yii::$app->formatter->asDecimal($model->price,0).' <sup>đ</sup>';
}
},
],
[
'attribute' => 'price',
'format' => 'raw',
'contentOptions'=>['style'=>'width: 10%;text-align:left'],
'value' => function ($model, $key, $index, $widget) use ($totalAmount) {
$total=$model->quantity * $model->price;
$totalAmount+=$total; [b]// for example total=500$ => $totalAmount=500$ but output alwal value =0[/b]
return Yii::$app->formatter->asDecimal($total,0).' <sup>$</sup>';
},
// 'footer' => function () use ($totalAmount){
// return $totalAmount;// use this function alway error
// },
'footer'=> $totalAmount,// alway value = 0
],
],
// 'rowOptions'=>function ($model, $key, $index, $grid){
// $class=$index%2?'odd':'even';
// return array('key'=>$key,'index'=>$index,'class'=>$class); đổi class giống hover
// },
]); ?>
<?php Pjax::end(); ?>