Krajee

Group Grid Demo #4

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

Enhancement of example # 2 with group summaries in the form of an additional group-wise footer. Note that group footer is set as a Closure in this example to show how one can derive values in the summary dynamically. This also includes automatic summarizing of number fields across both the groups. The Unit Price is averaged using GridView::F_AVG setting and Quantity and Amount (a formula column) are summed using GridView::F_SUM setting. This example also demonstrates an ability to to format summary number data at runtime and also merge columns in the group footer.


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 41-60 of 77 items.
Grid Grouping Example 4
#SupplierCategoryProduct NameUnit PriceUnits In StockAmount In Stock
 
 
41Formaggi Fortini s.r.l.Dairy ProductsGorgonzola Telino12.5000.00
42Formaggi Fortini s.r.l.Dairy ProductsMozzarella di Giovanni34.8014487.20
43Norske MeierierDairy ProductsGeitost2.50112280.00
44Norske MeierierDairy ProductsFlotemysost21.5026559.00
45Norske MeierierDairy ProductsGudbrandsdalsost36.0026936.00
46Bigfoot BreweriesBeveragesLaughing Lumberjack Lager14.0052728.00
47Bigfoot BreweriesBeveragesSteeleye Stout18.0020360.00
48Bigfoot BreweriesBeveragesSasquatch Ale14.001111,554.00
49Svensk Sjfda ABSeafoodInlagd Sill19.001122,128.00
50Svensk Sjfda ABSeafoodGravad lax26.0011286.00
51Svensk Sjfda ABSeafoodRd Kaviar15.001011,515.00
52Aux joyeux ecclsiastiquesBeveragesCte de Blaye263.50174,479.50
53Aux joyeux ecclsiastiquesBeveragesChartreuse verte18.00691,242.00
54New England Seafood CannerySeafoodBoston Crab Meat18.401232,263.20
55New England Seafood CannerySeafoodJack's New England Clam Chowder9.6585820.25
56Leka TradingBeveragesIpoh Coffee46.0017782.00
57Leka TradingCondimentsGula Malacca19.4527525.15
58Leka TradingGrains/CerealsSingaporean Hokkien Fried Mee14.0026364.00
59LyngbysildSeafoodRogede sild9.50547.50
60LyngbysildSeafoodSpegesild12.00951,140.00
   Page Summary31.191,04920,496.80
echo GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'showPageSummary' => true,
    'pjax' => true,
    'striped' => false,
    'hover' => true,
    'panel' => ['type' => 'primary', 'heading' => 'Grid Grouping Example'],
    'toggleDataContainer' => ['class' => 'btn-group mr-2 me-2'],
    'columns' => [
        ['class' => 'kartik\grid\SerialColumn'],
        [
            'attribute' => 'supplier_id', 
            'width' => '310px',
            'value' => function ($model, $key, $index, $widget) { 
                return $model->supplier->company_name;
            },
            'filterType' => GridView::FILTER_SELECT2,
            'filter' => ArrayHelper::map(Suppliers::find()->orderBy('company_name')->asArray()->all(), 'id', 'company_name'), 
            'filterWidgetOptions' => [
                'pluginOptions' => ['allowClear' => true],
            ],
            'filterInputOptions' => ['placeholder' => 'Any supplier'],
            'group' => true,  // enable grouping
            'groupFooter' => function ($model, $key, $index, $widget) { // Closure method
                return [
                    'mergeColumns' => [[1,3]], // columns to merge in summary
                    'content' => [             // content to show in each summary cell
                        1 => 'Summary (' . $model->supplier->company_name . ')',
                        4 => GridView::F_AVG,
                        5 => GridView::F_SUM,
                        6 => GridView::F_SUM,
                    ],
                    'contentFormats' => [      // content reformatting for each summary cell
                        4 => ['format' => 'number', 'decimals' => 2],
                        5 => ['format' => 'number', 'decimals' => 0],
                        6 => ['format' => 'number', 'decimals' => 2],
                    ],
                    'contentOptions' => [      // content html attributes for each summary cell
                        1 => ['style' => 'font-variant:small-caps'],
                        4 => ['style' => 'text-align:right'],
                        5 => ['style' => 'text-align:right'],
                        6 => ['style' => 'text-align:right'],
                    ],
                    // html attributes for group summary row
                    'options' => ['class' => 'info table-info','style' => 'font-weight:bold;']
                ];
            }
        ],
        [
            'attribute' => 'category_id', 
            'width' => '250px',
            'value' => function ($model, $key, $index, $widget) { 
                return $model->category->category_name;
            },
            'filterType' => GridView::FILTER_SELECT2,
            'filter' => ArrayHelper::map(Categories::find()->orderBy('category_name')->asArray()->all(), 'id', 'category_name'), 
            'filterWidgetOptions' => [
                'pluginOptions' => ['allowClear' => true],
            ],
            'filterInputOptions' => ['placeholder' => 'Any category'],
            'group' => true,  // enable grouping
            'subGroupOf' => 1 // supplier column index is the parent group,
            'groupFooter' => function ($model, $key, $index, $widget) { // Closure method
                return [
                    'mergeColumns' => [[2, 3]], // columns to merge in summary
                    'content' => [              // content to show in each summary cell
                        2 => 'Summary (' . $model->category->category_name . ')',
                        4 => GridView::F_AVG,
                        5 => GridView::F_SUM,
                        6 => GridView::F_SUM,
                    ],
                    'contentFormats' => [      // content reformatting for each summary cell
                        4 => ['format' => 'number', 'decimals' => 2],
                        5 => ['format' => 'number', 'decimals' => 0],
                        6 => ['format' => 'number', 'decimals' => 2],
                    ],
                    'contentOptions' => [      // content html attributes for each summary cell
                        4 => ['style' => 'text-align:right'],
                        5 => ['style' => 'text-align:right'],
                        6 => ['style' => 'text-align:right'],
                    ],
                    // html attributes for group summary row
                    'options' => ['class' => 'success table-success','style' => 'font-weight:bold;']
                ];
            },
        ],
        [
            'attribute' => 'product_name',
            'pageSummary' => 'Page Summary',
            'pageSummaryOptions' => ['class' => 'text-right text-end'],
        ],
        [
            'attribute' => 'unit_price',
            'width' => '150px',
            'hAlign' => 'right',
            'format' => ['decimal', 2],
            'pageSummary' => true,
            'pageSummaryFunc' => GridView::F_AVG
        ],
        [
            'attribute' => 'units_in_stock',
            'width' => '150px',
            'hAlign' => 'right',
            'format' => ['decimal', 0],
            'pageSummary' => true
        ],
        [
            'class' => 'kartik\grid\FormulaColumn',
            'header' => 'Amount In Stock',
            'value' => function ($model, $key, $index, $widget) { 
                $p = compact('model', 'key', 'index');
                return $widget->col(4, $p) * $widget->col(5, $p);
            },
            'mergeHeader' => true,
            'width' => '150px',
            'hAlign' => 'right',
            'format' => ['decimal', 2],
            '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