Yii2自定义行在页脚Kartik v中的总数(在页脚中显示行值的总和)

时间:2016-08-04 11:00:46

标签: php yii2 yii2-advanced-app yii2-model

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

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

任何帮助都会受到高度关注。

我正在使用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

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

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

任何人都可以帮我吗?

1 个答案:

答案 0 :(得分:1)

    在您的GridView中
  1. ,启用showFooter
  2. 在列中
  3. ,设置页脚文本
  4. 现在你需要一个函数来返回你的总和,而set是一个页脚的文本。

    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()