Yii2 Gridview - 如何在页脚属性上使用总计

My VALUE column is:

    [
     'attribute' => 'value',
     'format' => 'raw',
     'contentOptions'=>['style'=>'width: 10%;text-align:left'],
     'footer' => ???
    ],

How use totals of rows on FOOTER property ?

it's works 1. create class then 2.create column array, 3.configure column, 4.configure grid

namespace app\components;
class PTotal {
public static function pageTotal($provider, $fieldName)
{
    $total=0;
    foreach($provider as $item){
        $total+=$item[$fieldName];
    }
    return $total;
}
$provider = new ActiveDataProvider([
'query' => $query,
'sort' => $sort,
]);


$grid_columns=[         
[
    'attribute' => 'saldo_in',
    'footer'=>PTotal::pageTotal($provider->models,'saldo_in'),
]
]

echo GridView::widget([
'dataProvider' => $provider,
'showFooter'=>TRUE,
'footerRowOptions'=>['style'=>'font-weight:bold;text-decoration: underline;'],
'columns' =>$grid_columns,
]); 

I have not tried this but try defining the column like this.

On the top of the file define

$total = 0;

Afterwards define the column like this:

[
     'attribute' => 'value',
     'format' => 'raw',
     'contentOptions'=>['style'=>'width: 10%;text-align:left'],
     'value'=>function ($model, $key, $index, $widget) use ($total) {
        $total += $model->value;
        return $model->value;
     },
     'footer' => function () use ($total) 
     {
        //format the total here
        return $total;
     },
],

Now there are several problems with this as they are with the http://demos.krajee.com/grid meaning it will only add what it is shown, if you have pagination it will just show the total on that page.

If you want a total for all the records you should add it by hand using the dataprovider without pagination.

Again I have not really tried this, just give it a shot.

i try as you say but show me the error: trim() expects parameter 1 to be string, object given

protected function renderFooterCellContent()
{
    return trim($this->footer) !== '' ? $this->footer : $this->grid->emptyCell;
}

In Yii 1 works fine, i just create this function in Model

public function getTotals($ids)
        {
            if($ids){
                $ids = implode(",",$ids);
                $connection=Yii::app()->db;
                $command=$connection->createCommand("SELECT SUM(value) FROM `tb_cashbook` where id in ($ids)");
                $amount = $command->queryScalar();
                return "R$ ".Yii::app()->format->formatNumber($amount);
                }
                else 
                return null;
        }

And in View, this way in footer property:

'footer'=>"<strong>".$model->getTotals($model->search()->getKeys())." </strong>",

in the view file above calculate the footer sum by following code

    $amount = 0;
    if (!empty($dataProvider->getModels())) {
        foreach ($dataProvider->getModels() as $key => $val) {
            $amount += $val->amount;
        }
    }

now in your gridview use just pass the amount variable as follow

[
     'attribute' => 'amount', 'label' => 'Charged Amount',
     'value' => function ($model, $key, $index, $widget) {
              return Yii::$app->formatter->asCurrency($model->amount, 'INR');
              },
     'footer' => $amount,
   ],

i have tried it will work...

yii2 will not support 'footer' => function () use ($total)

  • total for a column works like below for me in yii2 :

In your grid view, eg: for column total_hours you need to show footer with sum of this column total_hours then you do following by extend the column in gridview, declare variable total in above the page and start griview override column assign total pass by reference to add total_hours from each row or record and finally assign the value to gridview footer attribute.

                       [
                            'header' => 'total_hours',
                            'attribute' => 'total_hours',
                            'value'=>function ($model, $key, $index, $widget) use (&$total) {
                                $total += $model->total_hours;
                                $widget->footer = $total;
                                return $model->total_hours ;
                            },                          
                         ],

and add 'showFooter' => true,