Naeem Ali
Naeem Ali

Reputation: 332

Yii2 : Get Sum In footer of gridview

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

Upvotes: 4

Views: 15399

Answers (3)

vavilinjohn
vavilinjohn

Reputation: 21

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

Upvotes: 2

Insane Skull
Insane Skull

Reputation: 9368

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;
}

Upvotes: 13

ScaisEdge
ScaisEdge

Reputation: 133400

The footer normally is intended for a total result for a column (not for a separated column )

Assuming $sum is the value you want see below the column price you sould

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

But looking to your code you have several error ..

In Search this is wrong

 return $dataProvider;$sum;

You can return a value only so you should

  return $dataProvider;

In your controller this is wrong

 $searchModel = new ReceiptsSearch();
 $sum = new ReceiptsSearch();

The result for $sum is the same that for $searcModel no new value is returned for $sum

You should perform the query for $sum this way

$sum = Receipts::find()->sum('price');



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

Upvotes: 0

Related Questions