ui icon indicating copy to clipboard operation
ui copied to clipboard

Feature/refactor totals

Open DarkSide666 opened this issue 8 years ago • 7 comments

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
]);

DarkSide666 avatar Dec 09 '17 17:12 DarkSide666

Need to add tests for all different kinds of totals.

DarkSide666 avatar Dec 09 '17 17:12 DarkSide666

wow relally good PR but do I have to write all the docs myself again ? 👍

romaninsh avatar Dec 10 '17 12:12 romaninsh

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.

DarkSide666 avatar Dec 11 '17 21:12 DarkSide666

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.

romaninsh avatar Dec 12 '17 09:12 romaninsh

Also it would be better to move all that totals functionality outside Table in separate class Totals. That's better because:

  1. make Table class simpler
  2. easier to extend just Totals class and make some changes there
  3. 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(...);

DarkSide666 avatar Dec 12 '17 19:12 DarkSide666

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

mvorisek avatar Apr 29 '23 18:04 mvorisek