如何在Yii2 Gridview中添加页脚

时间:2016-06-22 10:10:36

标签: php yii2 yii2-advanced-app

使用Yii Kartik-v Grid。我想为gridview设置页脚。这必须显示页脚总量的总和。

以下是我的gridview。 Footer

在页脚中,我如何显示总量。

帮助我坚持这个......帮助将非常感激

Gridview代码:

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

2 个答案:

答案 0 :(得分:2)

每个Ceil都是从GridView中的列类派生出来的。列类具有footer属性

将您的代码更新为: -

<?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'],
]; ?>

答案 1 :(得分:2)

添加Kartik \ grid \ SerialColumn和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',
        ]
    ]); ?>