There are some very advanced grids in the Yii2 community, specifically Kartik's amazing gridview extensions but they all designed for interactive screen use.
ReportGrid is designed to provide report results to users, without filtering or sorting.
But more importantantly it has sub-totalling and report totalling built into the gridview itself.
Because it enables you to use closures (anonymous functions) within the sub-total fields. For example, say we want to build a report on order items, with a break at order level. At the order level, we want to report something off the order model but the dataProvider is on the order-item level.
With ReportGrid, we can do this by using a closure on the sub-total to return the $model->order->some_relationship->some_value
Installation The preferred way to install this extension is through composer. Either run:
$ php composer.phar require chrisb34/reportgrid "@dev"
or add:
"chrisb34/yii2-report-grid": "@dev"
to the require section of your composer.json file. Then run:
php composer.phar update
to get the updated package on your application install.
As with most Yii2 widgets, you control the using options on widget creation. This widget is based on the Yii2 gridview widget, so anything mentioned here is over and above the base options on the gridview widget
Note: that this widget does not support pagination. It actively switches pagination off in the dataProvider
controlBreak : boolean, turn on all this functionality. defaults to true. If set to false will make this widget behave like a notmal gridView
totalRowOptions : array, array list of options eg: [ 'class'=>'total-row']
totalsHeader : boolean, whether to repeat the table header row just before the report totals.
exportCSV : boolean, whether to include the 'Export to CSV' button
afterRow : closure, provides the ability to output an extra row after every model row. Closure call in the format function( $model, $key, $index)
pageSummary : closure, provides the ability to output an extra row at the end of the report. Closure call in the format function( $model, $key, $index)
Usage
echo ReportGrid::widget([ 'dataProvider' => $dataProvider, 'columns' => $gridColumns, 'controlBreak' => true, 'totalRowOptions' => ['class'=>'total-row'], 'totalsHeader' => true, 'exportCSV' => true, 'afterRow' => function( $model, $key, $index) { return $someContent; }, 'pageSummary' => [ Html::tag('tr','<td colspan=10><h1>Report Summary goes here</h1></td>'), ] ]);
When you use sub-totalling in a report, your report layout must follow the same structure as your query. So the first thing is to define your query with the results in the correct order.
When defining columns, you first need to specify which columns will cause your report to break. Normally, these will also be in the same order as the columns themselves but this is not mandatory.
subTotalOn : integer, The break level runs from 1 upwards and the gridview then uses level zero as the report totals.
subTotal : array | boolean, with the following options;
Usage
'subTotal' => [ 'breakValue' => function($model, $key, $index, $widget, $break) { if ($break == 1) return $model->category_name; elseif ($break == 0) return 'REPORT TOTAL'; }, 'hideOnBreak' => 2 ]
for more info see: yii2.chris-backhouse.com
Be the first person to leave a comment
Please login to leave your comment.