Nodemon
Nodemon

Reputation: 1046

How to add Footer in Yii2 Gridview

Am using Yii Kartik-v Grid. I want to set footer for gridview . That has to show sum of total amount in footer.

Below is my gridview. Footer

In footer how can i show total of amount.

Help me am stuck with this...Help will be really appreciated

Gridview Code:

 <?php $gridColumns = [
            ['class' => 'yii\grid\SerialColumn'],
                'membercode',
                'member_name',
                [
                   'attribute' => 'payment_category',
                   'format' => 'raw',
                   'label' => 'Payment Category',
                   'value' => function($model, $key, $index, $grid) {
                        $temp = $model->payment_category;
                        $si = Category::find()->where(['category_id' => $temp])->one();
                        return $si['category_name'];
                    },  
                ],
                [
                   'attribute' => 'membercode',
                   'format' => 'raw',
                   'label' => 'Address',
                   'value' => function($model, $key, $index, $grid) {
                        $temp = $model->membercode;
                        $si = Memberlist::find()->where(['member_code' => $temp])->one();
                        return $si['address_line_1'] . "<br>" . $si['address_line_2'] . " " . $si['address_line_3'] . "<br>" . $si['city'] . " " . $si['pincode'];
                    },  
                ],
                'member_gender',
                [
                    'attribute' => 'payment_date',
                    'format' => 'raw',
                    'value' => function($model, $key, $index, $grid) {
                        $exp = date("d-m-Y", strtotime($model->payment_date)); 
                        return $exp;
                    }
                ],
                'amount',
            ['class' => 'yii\grid\ActionColumn'],
        ]; ?>

    <?= ExportMenu::widget([
            'dataProvider' => $dataProvider,
            'columns' => $gridColumns,
            'columnSelectorOptions'=>[
                'label' => 'Columns',
                'class' => 'btn btn-danger'
            ],
            'fontAwesome' => true,
            'dropdownOptions' => [
                'label' => 'Export All',
                'class' => 'btn btn-primary'
            ]
        ]); ?>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => $gridColumns,
        'showFooter'=>TRUE,
        'layout' => '{items}{pager}',
        'pager' => [
            'firstPageLabel' => 'First',
            'lastPageLabel' => 'Last',
        ]
    ]); ?>

Upvotes: 0

Views: 4690

Answers (2)

Kalaiselvan Mahendiran
Kalaiselvan Mahendiran

Reputation: 996

Add Kartik\grid\SerialColumn and Kartik\grid\ActionColumn

<?php $gridColumns = [
            ['class' => 'kartik\grid\SerialColumn'],
                'membercode',
                'member_name',
                [
                  'attribute'=>'amount', // use your column
                  'pageSummary' => true
                ],
                'amount',
            ['class' => 'kartik\grid\ActionColumn'],
        ]; ?>

    <?= ExportMenu::widget([
            'dataProvider' => $dataProvider,
            'columns' => $gridColumns,
            'columnSelectorOptions'=>[
                'label' => 'Columns',
                'class' => 'btn btn-danger'
            ],
            'fontAwesome' => true,
            'dropdownOptions' => [
                'label' => 'Export All',
                'class' => 'btn btn-primary'
            ]
        ]); ?>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'showPageSummary' => true ,
        'columns' => $gridColumns,
        'showFooter'=>TRUE,
        'layout' => '{items}{pager}',
        'pager' => [
            'firstPageLabel' => 'First',
            'lastPageLabel' => 'Last',
        ]
    ]); ?>

Upvotes: 2

Double H
Double H

Reputation: 4160

Each Ceil is Derived from Column Class in GridView.. Column Class has footer property

Update Your Code as:-

<?php
$gridColumns = [
   ['class' => 'yii\grid\SerialColumn'],
   'membercode',
   'member_name',
   [
    'attribute' => 'payment_category',
    'format' => 'raw',
    'label' => 'Payment Category',
    'value' => function($model, $key, $index, $grid) {
        $temp = $model->payment_category;
        $si = Category::find()->where(['category_id' => $temp])->one();
        return $si['category_name'];
    },
],
[
    'attribute' => 'membercode',
    'format' => 'raw',
    'label' => 'Address',
    'value' => function($model, $key, $index, $grid) {
        $temp = $model->membercode;
        $si = Memberlist::find()->where(['member_code' => $temp])->one();
        return $si['address_line_1'] . "<br>" . $si['address_line_2'] . " " . $si['address_line_3'] . "<br>" . $si['city'] . " " . $si['pincode'];
    },
],
'member_gender',
[
    'attribute' => 'payment_date',
    'format' => 'raw',
    'value' => function($model, $key, $index, $grid) {
        $exp = date("d-m-Y", strtotime($model->payment_date));
        return $exp;
    }
],

//amount is added as
[
    'attribute' => 'amount',
    'value' => function($model, $key, $index, $obj){
        $obj->footer +=$model->amount;
        return $model->amount;
    }
],
['class' => 'yii\grid\ActionColumn'],
]; ?>

Upvotes: 2

Related Questions