Skip to content

Commit

Permalink
Merge pull request #178 from atk4/feature/extend-table-totals
Browse files Browse the repository at this point in the history
Adds more customization options for Table totals
  • Loading branch information
romaninsh authored Jun 19, 2017
2 parents 2a7f114 + 27123ec commit 3a0bb28
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,13 +396,43 @@ public function jsRow()
public function updateTotals()
{
foreach ($this->totals_plan as $key=>$val) {

// if value is array, then we treat it as built-in or callable aggregate method
if (is_array($val)) {
switch ($val[0]) {
case 'sum':
if (!isset($this->totals[$key])) {
$this->totals[$key] = 0;
$f = $val[0]; // shortcut

// initial value is always 0
if (!isset($this->totals[$key])) {
$this->totals[$key] = 0;
}

// closure support
// arguments - current value, key, \atk4\ui\Table object
if ($f instanceof \Closure) {
$this->totals[$key] += ($f($this->model[$key], $key, $this) ?: 0);
}
// built-in methods
elseif (is_string($f)) {
switch ($f) {
case 'sum':
$this->totals[$key] += $this->model[$key];
break;
case 'count':
$this->totals[$key] += 1;
break;
case 'min':
if ($this->model[$key] < $this->totals[$key]) {
$this->totals[$key] = $this->model[$key];
}
break;
case 'max':
if ($this->model[$key] > $this->totals[$key]) {
$this->totals[$key] = $this->model[$key];
}
break;
default:
throw new Exception(['Aggregation method does not exist', 'method'=>$f]);
}
$this->totals[$key] += $this->model[$key];
}
}
}
Expand Down

0 comments on commit 3a0bb28

Please sign in to comment.