Krajee

Group Grid Demo #7

Thankful to Krajee! BUY A COFFEEor to get more out of us.

An example of a multi level grouping with group footer summary. This example uses as a variation an ArrayDataProvider instead of ActiveDataProvider. The extension always assumes the first column as a non-grouped column. This example shows how you can still display the first column as grouped. To do that just setup a hidden dummy column like the SerialColumn with hidden property set to true.



Tip

Not seeing the updated content on this page! Hard refresh your browser to clean cache for this page (e.g. SHIFT-F5 on Windows Chrome)

Showing 1-12 of 12 items.
Grid Grouping Example
#YearMonthCatRegionIDAmount ($)
12017JANCAT-1IT-511,400.00
22017JANCAT-1IT-521,300.00
32017JANCAT-1IT-632,400.00
42017JANCAT-1IT-644,900.00
52017JANCAT-2IT-757,340.00
62017JANCAT-2IT-764,560.00
72017JANCAT-2IT-873,550.00
82017JANCAT-2IT-889,450.00
92017FEBCAT-2IT-693,900.00
102017FEBCAT-2IT-61052,200.00
112018JANCAT-3IT-5114,700.00
122018JANCAT-3IT-51211,900.00
 Page Summary    107,600.00
use yii\data\ArrayDataProvider;
use kartik\grid\GridView;

// An Array Data Provider
$dataProvider = new ArrayDataProvider(['allModels' => [    
    ['id' => 1, 'year' => 2017, 'month' => 'JAN', 'cat' => 'CAT-1', 'region' => 'IT-5', 'amount' => 1400],
    ['id' => 2, 'year' => 2017, 'month' => 'JAN', 'cat' => 'CAT-1', 'region' => 'IT-5', 'amount' => 1300],
    ['id' => 3, 'year' => 2017, 'month' => 'JAN', 'cat' => 'CAT-1', 'region' => 'IT-6', 'amount' => 2400],
    ['id' => 4, 'year' => 2017, 'month' => 'JAN', 'cat' => 'CAT-1', 'region' => 'IT-6', 'amount' => 4900],
    ['id' => 5, 'year' => 2017, 'month' => 'JAN', 'cat' => 'CAT-2', 'region' => 'IT-7', 'amount' => 7340],
    ['id' => 6, 'year' => 2017, 'month' => 'JAN', 'cat' => 'CAT-2', 'region' => 'IT-7', 'amount' => 4560],
    ['id' => 7, 'year' => 2017, 'month' => 'JAN', 'cat' => 'CAT-2', 'region' => 'IT-8', 'amount' => 3550],
    ['id' => 8, 'year' => 2017, 'month' => 'JAN', 'cat' => 'CAT-2', 'region' => 'IT-8', 'amount' => 9450],
    ['id' => 9, 'year' => 2017, 'month' => 'FEB', 'cat' => 'CAT-2', 'region' => 'IT-6', 'amount' => 3900],
    ['id' => 10, 'year' => 2017, 'month' => 'FEB', 'cat' => 'CAT-2', 'region' => 'IT-6', 'amount' => 52200],
    ['id' => 11, 'year' => 2018, 'month' => 'JAN', 'cat' => 'CAT-3', 'region' => 'IT-5', 'amount' => 4700],
    ['id' => 12, 'year' => 2018, 'month' => 'JAN', 'cat' => 'CAT-3', 'region' => 'IT-5', 'amount' => 11900],
]]);
// Group Footer Settings
$gfYear = function ($model, $key, $index, $widget) {
    return [
        'mergeColumns' => [[1, 5]], 
        'content' => [              // content to show in each summary cell
            1 => 'Year Total (' . $model['year'] . ')',
            6 => GridView::F_SUM,
        ],
        'contentFormats' => [      // content reformatting for each summary cell
            6 => ['format' => 'number', 'decimals' => 2],
        ],
        'contentOptions' => [      // content html attributes for each summary cell
            6 => ['class' => 'text-right text-end'],
        ],
        'options' => ['class' => 'active table-active h6']
    ];
};
$gfMonth = function ($model, $key, $index, $widget) {
    return [
        'mergeColumns' => [[2, 5]], 
        'content' => [              // content to show in each summary cell
            2 => "Month Total ({$model['month']}→{$model['year']})",
            6 => GridView::F_SUM,
        ],
        'contentFormats' => [      // content reformatting for each summary cell
            6 => ['format' => 'number', 'decimals' => 2],
        ],
        'contentOptions' => [      // content html attributes for each summary cell
            6 => ['class' => 'text-right text-end'],
        ],
        'options' => ['class' => 'success table-success h6']
    ];
};
$gfCategory = function ($model, $key, $index, $widget) {
    return [
        'mergeColumns' => [[3, 5]], 
        'content' => [              // content to show in each summary cell
            3 => "Category Total ({$model['cat']}→{$model['month']}→{$model['year']})",
            6 => GridView::F_SUM,
        ],
        'contentFormats' => [      // content reformatting for each summary cell
            6 => ['format' => 'number', 'decimals' => 2],
        ],
        'contentOptions' => [      // content html attributes for each summary cell
            6 => ['class' => 'text-right text-end'],
        ],
        'options' => ['class' => 'danger table-danger h6'],
    ];
};
$gfRegion = function ($model, $key, $index, $widget) {
    return [
        'mergeColumns' => [[4, 5]], 
        'content' => [              // content to show in each summary cell
            4 => "Region Total ({$model['region']}→{$model['cat']}→{$model['month']}→{$model['year']})", 
            6 => GridView::F_SUM,
        ],
        'contentFormats' => [      // content reformatting for each summary cell
            6 => ['format' => 'number', 'decimals' => 2],
        ],
        'contentOptions' => [      // content html attributes for each summary cell
            6 => ['class' => 'text-right text-end'],
        ],
        'options' => ['class' => 'info table-info h6']
    ];
};
// The GridView widget (multi level grouped)
echo GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => null,
    'showPageSummary' => true,
    'pjax' => true,
    'hover' => true,
    'panel' => ['type' => 'primary', 'heading' => 'Grid Grouping Example'],
    'toggleDataContainer' => ['class' => 'btn-group mr-2 me-2'],
    'columns' => [
        // note that you MUST NOT have the first column as a grid group
        // to achieve that add a dummy hidden column like shown below
        ['class' => 'kartik\grid\SerialColumn', 'hidden' => true], 
        ['attribute' => 'year',  'group' => true, 'groupFooter' => $gfYear, 'pageSummary' => 'Page Summary'],
        ['attribute' => 'month',  'group' => true, 'subGroupOf' => 1, 'groupFooter' => $gfMonth],
        ['attribute' => 'cat',  'group' => true, 'subGroupOf' => 2, 'groupFooter' => $gfCategory],
        ['attribute' => 'region',  'group' => true, 'subGroupOf' => 3, 'groupFooter' => $gfRegion],
        ['attribute' => 'id', 'label' => 'ID', 'hAlign' => 'center', 'width' => '150px'],
        ['attribute' => 'amount', 'label' => 'Amount ($)', 'format' => ['decimal',  2], 'hAlign' => 'right', 'width' => '150px', 'pageSummary' => true],
    ],
]);

Note

You can now visit the Krajee Webtips Q & A forum for searching OR asking questions OR helping programmers with answers on these extensions and plugins. For asking a question click here. Select the appropriate question category (i.e. Krajee Plugins) and choose this current page plugin in the question related to field.

The comments and discussion section below are intended for generic discussions or feedback for this plugin. Developers may not be able to search or lookup here specific questions or tips on usage for this plugin.

 
visitors to Krajee Yii2 Demos since 22-May-2017