Yii2 Gridview - How use totals on footer property

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-20 10:58:16

问题


My VALUE column is:

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

How use totals of rows on FOOTER property ?


回答1:


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,
]); 



回答2:


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...




回答3:


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.




回答4:


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,




回答5:


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>",


来源:https://stackoverflow.com/questions/28003381/yii2-gridview-how-use-totals-on-footer-property

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!