Yii2 : Get Sum In footer of gridview

12,206

Solution 1

Controller

$searchModel = new ReceiptsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

return $this->render('index', [
  'searchModel' => $searchModel,
  'dataProvider' => $dataProvider,
]);

SearchModel

public function search($params)
{
  $query = Receipts::find()->joinWith('patient');

  $dataProvider = new ActiveDataProvider([
    'query' => $query,
  ]);

  $this->load($params);

  if (!$this->validate()) {
      return $dataProvider;
  }

  $query->andFilterWhere([
     'id' => $this->id,
     'price' => $this->price,
     'reg_date' => $this->reg_date,
  ]);

  $query->andFilterWhere(['like','patient.patient_name',$this->patient_id]);

  return $dataProvider;
}

View

<?= GridView::widget([
   'dataProvider' => $dataProvider,
   'filterModel' => $searchModel,
   'showFooter' => true,
   'columns' => [
       ['class' => 'yii\grid\SerialColumn'],

       [
         'attribute' => 'patient_id',
         'value' => 'patient.patient_name'
       ],
       [
         'attribute' => 'price',
         'footer' => Receipts::getTotal($dataProvider->models, 'price'),       
       ],

       ['class' => 'yii\grid\ActionColumn'],
    ],
]); ?>

Receipts Model

public static function getTotal($provider, $fieldName)
{
    $total = 0;

    foreach ($provider as $item) {
        $total += $item[$fieldName];
    }

    return $total;
}

Solution 2

You can reach the same effect with use kartik\grid\GridView; without using helpers function.

Just add 'showPageSummary' => true to your GridView config and pageSummary' => true to columns that you need to sum.

View

use kartik\grid\GridView;

echo GridView::widget([
       'dataProvider' => $dataProvider,
       'filterModel' => $searchModel,
       'showPageSummary' => true,
       'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            [
                'attribute' => 'patient_id',
                'value' => 'patient.patient_name'
            ],
            [
                'attribute' => 'price',
                'pageSummary' => true
            ],
            ['class' => 'yii\grid\ActionColumn'],
       ],
]); ?>
Share:
12,206
Naeem Ali
Author by

Naeem Ali

I am a geek having no idea at programming, system administration and management. I am seeking a good position as a software developer and administrator

Updated on June 07, 2022

Comments

  • Naeem Ali
    Naeem Ali almost 2 years

    I'm new here that I can't comment here

    and I have a problem when I try to Get sum in the footer .

    my code in controller :

    $searchModel = new ReceiptsSearch();
    $sum = new ReceiptsSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
     return $this->render('index', [
       'searchModel' => $searchModel,
       'dataProvider' => $dataProvider,
       'sum'=>$sum,
       ]);
    

    my SearchModel Code :

    public function search($params)
    {
        $query = Receipts::find();
        $sum = $query->sum('price');
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
    
        $this->load($params);
    
        if (!$this->validate()) {
            return $dataProvider;
        }
        $query->joinWith('patient'); 
        $query->andFilterWhere([
           'id' => $this->id,
           'price' => $this->price,
           'reg_date' => $this->reg_date,
        ]);
        $query->andFilterWhere(['like','patient.patient_name',$this->patient_id]);
    
        return $dataProvider;$sum;
    }
    

    my view page

    <?= GridView::widget([
    
        'dataProvider' => $dataProvider,$sum,
    
        'filterModel' => $searchModel,
        'showFooter' => true,
        'columns' => [
    
            ['class' => 'yii\grid\SerialColumn'],
    
            [
            'attribute'=>'patient_id',
            'value'=>'patient.patient_name'
            ],
            'price',
            ],
            [
            'attribute' => 'sum',
            'footer' => 'sum',
            ],
            ['class' => 'yii\grid\ActionColumn'],
        ],
        ]); 
    ?>
    

    the message shown is :

    Setting unknown property: yii\grid\GridView::0

  • Naeem Ali
    Naeem Ali over 7 years
    still gives me Setting unknown property: yii\grid\GridView::0
  • ScaisEdge
    ScaisEdge over 7 years
    1) Remove the $sum form the 'dataProvider' row like in my code 2) eventually Just for debugging try comment the row 'value'=>'patient.patient_name'
  • Naeem Ali
    Naeem Ali over 7 years
    @scaisEedge trim() expects parameter 1 to be string, object given
  • ScaisEdge
    ScaisEdge over 7 years
    looking to your code i have see a some conceptual error ..try to correct these errors and then post the updated code
  • Naeem Ali
    Naeem Ali over 7 years
    I used this way and its works fine , but it gives me the total of all data in Receipt table even if I used filters , Thank you for your help
  • Naeem Ali
    Naeem Ali over 7 years
    I told you before you are a genius, not Insane it works as I want :) .. thank you
  • ScaisEdge
    ScaisEdge over 7 years
    @NaeemAli . your primary problem was the concectual error in your code .. the problem of the total in query is easly solved using a function for calculate inside the gridview instead of pass. the value in render call .. but this was not clear in your code request ..
  • Insane Skull
    Insane Skull over 7 years
    @NaeemAli Told you i am insane, that's why genius. :)
  • Naeem Ali
    Naeem Ali over 7 years
    this is my fault .. Sorry !
  • Naeem Ali
    Naeem Ali about 6 years
    you answered my question after 1 year and 3 months :) , yeah you can do it and i got this answer by experience :P
  • Andrey Zausaylov
    Andrey Zausaylov about 4 years
    you dont need the static method Receipts::getTotal Its enough to do it like this $dataProvider->query->sum('price') in your view file. thats even better cause it always use your current filter