Feature/refactor totals
Total support in Grid is currently quite basic, implementing ability to add up numeric values, define per-column strategies and format the footer.
This PR offers many enhancements:
Ability to add multiple Total lines
Sometimes you would want to define multiple total lines, for instance - SubTotals and Totals. They will be increased simultaneously, but will output in 2 separate lines. They will also have separate strategy.
However SubTotals need ability to be "inserted" multiple times too, which we can implement using hook beforeRow (#277).
$table->addTotals([
'name'=>'Totals for {$category}',
'category'=>false, // will not output and will use colspan
'amount'=>'sum'
], 'sub');
$last_category = null;
$table->beforeRow(function($row) use ($table) {
if ($last_category == null) {
$last_category = $row['category'];
} elseif ($last_category != $row['category']) {
$table->renderTotals('sub', ['category'=>$last_category]);
}
});
Advanced calculation strategies
Here for the data that contains various dates, we find and display date range in the bottom: "1 Jul - 20 Aug"
$table->addTotals([
'date'=>[
'default'=> [null, null],
'row'=>function($v, $prev) {
return [min($prev[0], $v), max($prev[1], $v]
},
'total'=>function($v) {
return $v[0]->format('M d').' - '.$v[1]->format('M d');
},
'format' => false, // to not use date column formatter
]);
Need to add tests for all different kinds of totals.
wow relally good PR but do I have to write all the docs myself again ? 👍
Tomorrow I will add one more feature there. I have it now (idea) written down on paper but to sleepy to implement it now :) Also I will write docs with examples tomorrow.
something that came to my mind was ability to specify 'name' for the total rows, e.g. addTotals('row2'), then it is safer for you to modify it. Obviously should be optional.
Also it would be better to move all that totals functionality outside Table in separate class Totals. That's better because:
- make Table class simpler
- easier to extend just Totals class and make some changes there
- Totals class should be added to Table invisibly for user, so user still will call Table->addTotals(...) and that will do $table->totals_plan[] = new Totals(...);
I have rebased https://github.com/atk4/ui/commit/56da26eb40026aa8aaaed1a0ea6f1367f5925a4d 1:1 and simplified the history as much as possible (by squashing CS fixer for example) into https://github.com/atk4/ui/commit/d34d398dd6a3a3ad2c6f93da737961697c85fccf. The diff shows zero changes.
Then I have reverted the changed file paths (files) on the latest develop and rebased the changes again (now with paths matching the current develop): all original changed: https://github.com/atk4/ui/compare/c75b7ae082a8b1755e2b573818af3926d633bed6...d34d398dd6a3a3ad2c6f93da737961697c85fccf