vijay nathji
vijay nathji

Reputation: 1638

Yii2 Customized Row's total in footer Kartik v ( display total of row values in footer)

I want to display total of row vaules in footer.

I have tried several ways but doesn't show proper result.

Any help will be highly appriciated.

I am using kartik grid.

I have tried this Yii2: Kartik Gridview sum of a column in footer

but doesn't work in my case.

$gridColumns = [
    [
        'class' => 'kartik\grid\SerialColumn',
        'contentOptions' => ['class' => 'kartik-sheet-style'],
        'width' => '36px',
        'header' => '',
        'headerOptions' => ['class' => 'kartik-sheet-style']
    ],
    [ 
        'attribute' => 'vno',
        'footer'=>'Total'
    ],

    [
        'attribute' => 'fliters',
        'label' => 'Total Fuel Consumption Liters',
        'footer'=> true   // Here i want to display the sum of column filer ( Note : the value in grid of filter is a sum(fuel_liters)  )
    ],
    [
        'attribute' => 'fuel_rate',
        'label' => 'Fuel Rate (Per Ltr.)',
        'pageSummary'=>true,
        'footer'=>true      // Here i want to display the sum of column fuel_rate ( Note : the value in grid of filter is a sum(fuel_rate)  )
    ],
    [
        'attribute' => 'famt',
        'label' => 'Total Fuel Consumption Amount',
         'pageSummary'=>true,
        'footer'=>true  // Here i want to display the sum of column fuel_amount ( Note : the value in grid of filter is a sum(fuel_amount)  )
    ],
];

echo GridView::widget([
    'dataProvider'=> $dataProvider,
    'filterModel' => $searchModel,
    'columns' => $gridColumns,
    'showPageSummary' => true
]);

So far I manage to print total in footer

Im my Controller i have wriiten this

 $total_ltrs = FuelConsumption::find()->sum('fuel_liters');
            $total_amt  = FuelConsumption::find()->sum('fuel_amount');
            $total_rate = FuelConsumption::find()->sum('fuel_rate');


            return $this->render('petrol-report', [
                    'total_ltrs' => $total_ltrs,
                    'total_amt' => $total_amt,
                    'total_rate' => $total_rate,
                    'model' => $model,
                    'searchModel' => $searchModel,
                    'dataProvider' => $dataProvider,
            ]);

And passed 'footer'=>$total_ltrs 'footer'=>$total_rate and 'footer'=>$total_amt Respectively in all footers.

Now when i do search throgh From Date and TO Date the result is dislaying in grid as per filter.

But the Result of Total in footer keeps unchanged. I want to do some of only those rows which are in grid.

Can anybody help me on this ??

Upvotes: 0

Views: 2388

Answers (1)

e-frank
e-frank

Reputation: 749

  1. in your GridView, enable showFooter
  2. in your column, set the footer text

now you will need a function to return your sum and set is a the footer's text.


GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel'  => $searchModel,
    'showFooter' => true,
    'columns'      => [
        [
            'class' => '\common\grid\CheckboxColumn'
        ],

        [
            'attribute' => 'myfield1',
            'footer' => 'my footer'
        ],

    ],
]);

if you want a function, use a static helper:

class MyHelper {
    public static function footer() { time(); }
}

        'footer' => MyHelper::footer()

Upvotes: 1

Related Questions