doufeng9567
doufeng9567
2015-01-17 19:30
浏览 332
已采纳

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 ?

图片转代码服务由CSDN问答提供 功能建议

我的VALUE栏是:

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

如何在FOOTER属性上使用行总数?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • dongyuan3094
    dongyuan3094 2015-01-28 07:40
    已采纳

    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,
    ]); 
    
    点赞 评论
  • donglu3243
    donglu3243 2015-01-18 03:54

    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.

    点赞 评论
  • duanlan8763
    duanlan8763 2015-01-18 20:25

    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>",
    
    点赞 评论
  • douqiang5933
    douqiang5933 2016-04-17 15:43

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

    点赞 评论
  • douya1248
    douya1248 2019-05-09 06:20

    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,

    点赞 评论

相关推荐