Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2: Kartik Gridview sum of a column in footer

I have installed the Kartik gridview extension, which is working fine.

But I couldn't find or missed it in the docs, how I can show the sum of a column in the footer.

This is my complete code in index.php

<?php 
        $gridColumns = [
    ['class' => 'yii\grid\SerialColumn'],
    'id',
    [
        //'attribute'=>'service_name',
        'attribute'=>'service_name',
        'value'=>'serviceName.services',                
    ],
    [
        'attribute'=>'room_category',
        'value'=>'roomCategory.room_category'
     ],
        'charges_cash',
        'charges_cashless',
    ['class' => 'yii\grid\ActionColumn']
];

    echo ExportMenu::widget([
    'dataProvider' => $dataProvider,
    'columns' => $gridColumns,
    'fontAwesome' => true,
    'showPageSummary' => true,
    'dropdownOptions' => [
        'label' => 'Export All',
        'class' => 'btn btn-default'
    ]
]) 
        ?>
    </div></div>


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

            'id',
            //'service_name',
            [
                //'attribute'=>'service_name',
                'attribute'=>'service_name',
                'value'=>'serviceName.services',

            ],
           // 'room_category',
            [
                'attribute'=>'room_category',
                'value'=>'roomCategory.room_category'
            ],
            'charges_cash',
            'charges_cashless',

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

</div>

Looking for some help on this one. Thanks.

like image 936
Pawan Avatar asked Mar 21 '15 14:03

Pawan


2 Answers

I think you just need to add the page summary;

use kartik\grid\GridView;

// Create a panel layout for your GridView widget
echo GridView::widget([
    'dataProvider'=> $dataProvider,
    'filterModel' => $searchModel,
    'columns' => $gridColumns,
    'showPageSummary' => true
]);

Kartik describes it pretty well in the demo and plugin details.

like image 124
Joe Miller Avatar answered Nov 14 '22 16:11

Joe Miller


Complete example:

     GridView::widget([
            'dataProvider'=> $dataProvider,
            'filterModel' => $searchModel,
            'columns' => [

            [
        'class' => 'kartik\grid\ActionColumn',
        'urlCreator' => function($action, $model, $key, $index) {
            // using the column name as key, not mapping to 'id' like the standard generator
            $params = is_array($key) ? $key : [$model->primaryKey()[0] => (string) $key];
            $params[0] = \Yii::$app->controller->id ? \Yii::$app->controller->id . '/' . $action : $action;
            return Url::toRoute($params);
        },
        'contentOptions' => ['nowrap'=>'nowrap']
    ],
        'id',
        'name',

 [

'attribute'=>'total_quantity',
'pageSummary' => true

],

[

'attribute'=>'quantity_sold',
'pageSummary' => true

],

    ],
            'showPageSummary' => true
    ]);

Note: please rename column class from yii\grid to kartik\grid\ . This goes for DataColum, ActionColumn etc

like image 3
iltaf khalid Avatar answered Nov 14 '22 18:11

iltaf khalid