Skip to content

Commit d34d398

Browse files
romaninshmvorisek
authored andcommitted
Added lots of documentation for table and particularly totals. added afterRow hook
1 parent 129827e commit d34d398

File tree

2 files changed

+113
-4
lines changed

2 files changed

+113
-4
lines changed

docs/table.rst

Lines changed: 111 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,7 @@ Table Totals
451451

452452
.. php:attr:: totals_plan
453453
.. php:attr:: totals
454-
.. php:method:: addTotals()
455-
454+
.. php:method:: addTotals($plan, $plan_id = null)
456455
457456
Table implements a built-in handling for the "Totals" row. Simple usage would be::
458457

@@ -467,6 +466,16 @@ but here is what actually happens:
467466
.. important:: addTotals() will only calculate based on rendered rows. If your table has limit
468467
or uses :php:class:`Paginator` you should calculate totals differently. See :php:meth:`Grid::addGrandTotals`
469468

469+
Method addTotals() can be executed multiple times and every time it defines a new plan and
470+
will be reflected as an extra row for totals.
471+
472+
To illustrate the need for multiple totals plans, here are some the scenarios which Table allows
473+
you to cover:
474+
475+
- Add "subtotal" then "tax" and then "total" row to your table.
476+
- Create "group totals" which will appear in the middle of the table.
477+
- Display per-page total and grand totals (for entire table).
478+
470479
Definition of a "totals plan"
471480
-----------------------------
472481

@@ -495,20 +504,118 @@ is set. The above example can therefore be shortened::
495504

496505
$table->addTotals(['client' => 'Totals for {$clients} client(s)']);
497506

507+
In addition to the plans you define, there is also going to be `{$_row_count}` which automatically counts
508+
number of rows in your table.
509+
498510
Possible values for total plan stages
499511
-------------------------------------
500512

501513
`init` value is typically a number, but can be any value, especially if you are going to control it's
502514
increment / formatting.
503515

504-
`update` can be string - either "sum" or "increment". Other values are not supported. You can also pass
505-
a callable which gives you an option to perform update yourself.
516+
`update` can be string. Supported built-ins are:
517+
518+
- min
519+
- inc
520+
- max
521+
- sum
522+
- avg
523+
524+
You can use a callable which gives you an option to perform update yourself. Also, make note that `inc`
525+
will ONLY increment when value of the column it's associated with is not empty. If you need to get
526+
total number of rows, use a special value `{$_row_count}`
527+
528+
Update can also be set to `false` if you don't want update to take place. If not set, or set to `null`
529+
then default action (based on column type) will be invoked.
506530

507531
`format` uses a template in a format suitable for :php:class:`Template`. Within the template you can
508532
reference other fields too. A benefit for using template is that type formatting is done automatically
509533
for you.
510534

535+
If `format` set to `null` or omitted then default action will be used which is to display totals only
536+
`money` / `numeric` and title column ("Totals for 123 record(s)")
537+
538+
539+
Using Callbacks
540+
---------------
541+
542+
Value of any stage may also be a callback. Callbacks will be executed during the appropriate stage execution
543+
and the value will be used:
544+
545+
`init` defines callback like this::
546+
547+
function($model) {
548+
549+
// $model is not loaded yet!!
550+
551+
return 0; // initial value
552+
}
553+
554+
`update` defines callback like this::
555+
556+
function($total, $value, $model) {
557+
558+
// $total is previous value
559+
// $value is value of the column
560+
// $model will be loaded to current column
561+
}
562+
563+
Here is example how you can implement "longest value" function::
564+
565+
function ($total, $value) {
566+
return strlen($value) > strlen($total) ? $value : $total;
567+
}
511568

569+
`format` defines callback like this::
570+
571+
function ($total, $model) {
572+
return 'Total is: '.$total;
573+
}
574+
575+
.. important:: when defining format as a string, template engine performs value
576+
formatting. If you define it through the callback, it's up to you.
577+
578+
Some examples
579+
-------------
580+
581+
Calculate total salary yourself::
582+
583+
584+
$salary_value = my_calc_salary();
585+
586+
$table->addTotals(['salary'=> money_format($salary_value]));
587+
588+
.. important:: The value for the format above is passed through Template, so if user has control over
589+
the value, he may reference model field you don't want him to see. Keep your security tight!!
590+
591+
Safer version of the above code would be::
592+
593+
$salary_value = my_calc_salary();
594+
595+
$table->addTotals(['salary'=> function() use($salary_value) { money_format($salary_value]); });
596+
597+
Here is another alternative::
598+
599+
$salary_value = my_calc_salary();
600+
601+
$table->addTotals(['salary'=> [
602+
'init'=> $salary_value,
603+
'update'=>false,
604+
]);
605+
606+
Combine output into single column::
607+
608+
$table->addTotals([
609+
'name'=>['update'=>'increment', 'format'=>'Total salary for {$name} employees is {$salary}'],
610+
'salary'=>['update'=>'sum', 'format'=>false]
611+
]);
612+
613+
With introduciton of callbacks you can show average value too::
614+
615+
$table->addTotals([
616+
'name'=>['update'=>'increment', 'format'=>'Total salary for {$name} employees is {$salary}'],
617+
'salary'=>['update'=>'sum', 'format'=>false]
618+
]);
512619

513620

514621
Advanced Usage

src/Table.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,8 @@ public function renderView()
440440
$this->renderRow();
441441

442442
$rows++;
443+
444+
$this->hook('afterRow');
443445
}
444446

445447
// Add totals rows or empty message

0 commit comments

Comments
 (0)