0

我想在页脚中显示总行值。

我尝试了几种方法,但没有显示正确的结果。

任何帮助都将受到高度评价。

我正在使用kartik网格。

我试过这个Yii2: Kartik Gridview sum of a column in footer

但在我的情况下不起作用。

$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
]);

到目前为止,我设法在页脚中打印总计

我是我的控制器,我写了这个

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

并分别在所有页脚中'footer'=>$total_ltrs 'footer'=>$total_rate通过。'footer'=>$total_amt

现在,当我通过 From Date 和 TO Date 进行搜索时,结果会根据过滤器显示在网格中。

但是页脚中的总计结果保持不变。我只想做一些网格中的行。

有人可以帮我吗?

4

1 回答 1

1
  1. 在您的 GridView 中,启用showFooter
  2. 在您的专栏中,设置页脚文本

现在您将需要一个函数来返回您的总和并设置为页脚的文本。


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

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

    ],
]);

如果你想要一个函数,请使用静态助手:

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

        'footer' => MyHelper::footer()
于 2016-08-04T14:30:13.937 回答