3

How do I sum up the values of a column in my GridView and display the total?

My GridView:

$dataProvider = new ActiveDataProvider([
'query' => Accounts::find()->where('account_type=1'),
'sort'=> ['defaultOrder' => ['account_code'=>SORT_ASC]],
]);
echo GridView::widget([
'dataProvider' => $dataProvider,
        'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        'account_code',
        'account_name',
        'amount',
        ['class' => 'yii\grid\ActionColumn'],
        ],

My table:

drich
  • 69
  • 1
  • 8

3 Answers3

4

A bit better and dynamic way :

1) create function in model

public static function getTotal($provider, $columnName)
{
    $total = 0;
    foreach ($provider as $item) {
      $total += $item[$columnName];
  }
  return $total;  
}

2) View

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'showFooter' => true,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        'account_code',
        'account_name',
        [
         'attribute' =>'amount',
         'footer' => ModelName::getTotal($dataProvider->models, 'amount'),
        ],

        ['class' => 'yii\grid\ActionColumn'],
    ] 
]) ?>
Insane Skull
  • 9,220
  • 9
  • 44
  • 63
3

To calculate the amount for all pages, make some changes to your code:

$dataProvider = new ActiveDataProvider([
    'query' => Accounts::find()->where('account_type=1'),
    'sort'=> ['defaultOrder' => ['account_code'=>SORT_ASC]],
]);
echo GridView::widget([
    'dataProvider' => $dataProvider,
    'showFooter' => true,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],

        'account_code',
        'account_name',
        [
            'attribute' =>'amount',
            'footer' => $dataProvider->query->sum('amount'),
        ],

        ['class' => 'yii\grid\ActionColumn'],
    ] 
]);
  • 1
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Derek Brown Aug 06 '18 at 15:53
0

You could add some code for calculate the total

This kind of operation should be done in you controller (or you could add a proper function in model)

In you case you are working directly in view (is not a good practice, but anyway) yo

  $dataProvider = new ActiveDataProvider([
  'query' => Accounts::find()->where('account_type=1'),
  'sort'=> ['defaultOrder' => ['account_code'=>SORT_ASC]],
  ]);

   // calculate the result in $mySum
   foreach ($provider as $item) {
          $mySum += $item['amount'];

      }

  // add showfooter => true,
  echo GridView::widget([
    'dataProvider' => $dataProvider,
    'showFooter' => true,
    'columns' => [
          ['class' => 'yii\grid\SerialColumn'],
          'account_code',
          'account_name',
          [
           'attribute' =>'amount',
           'footer' => $mySum, // add the footer values
          ],
          ['class' => 'yii\grid\ActionColumn'],
  ],
  ......
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107