Skip to content

Commit a2954e9

Browse files
author
Herbert Maschke
committed
introduce ability to append a summary row to a datatable
1 parent e070004 commit a2954e9

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,10 @@ class ComplexDemoTable extends LivewireDatatable
158158

159159
(new LabelColumn())
160160
->label('My custom heading')
161-
->content('This fixed string appears in every row')
161+
->content('This fixed string appears in every row'),
162+
163+
NumberColumn::name('dollars_spent')
164+
->enableSummary(),
162165
];
163166
}
164167
}
@@ -238,6 +241,23 @@ public function columns()
238241
->label('Planet'),
239242
```
240243

244+
### Summary row
245+
If you need to summarize all cells of a specific column, you can use `enableSummary()`:
246+
247+
```php
248+
public function columns()
249+
{
250+
return [
251+
Column::name('dollars_spent')
252+
->label('Expenses in Dollar')
253+
->enableSummary(),
254+
255+
Column::name('euro_spent')
256+
->label('Expenses in Euro')
257+
->enableSummary(),
258+
```
259+
260+
241261
### Custom column names
242262
It is still possible to take full control over your table, you can define a ```builder``` method using whatever query you like, using your own joins, groups whatever, and then name your columns using your normal SQL syntax:
243263

resources/views/livewire/datatables/datatable.blade.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,20 @@ class="px-3 py-2 border border-green-400 rounded-md bg-white text-green-500 text
154154
{{ __("There's Nothing to show at the moment") }}
155155
</p>
156156
@endforelse
157+
158+
@if ($this->hasSummaryRow())
159+
<div class="table-row p-1">
160+
@foreach($this->columns as $column)
161+
@if ($column['summary'])
162+
<div class="table-cell px-6 py-2 whitespace-no-wrap @if($column['align'] === 'right') text-right @elseif($column['align'] === 'center') text-center @else text-left @endif {{ $this->cellClasses($row, $column) }}">
163+
{{ $this->summarize($column['name']) }}
164+
</div>
165+
@else
166+
<div class="table-cell"></div>
167+
@endif
168+
@endforeach
169+
</div>
170+
@endif
157171
</div>
158172
</div>
159173
</div>

src/Column.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ class Column
3333
public $width;
3434
public $exportCallback;
3535

36+
/**
37+
* @var bool should the sum of all summarizable cells in this column be
38+
* displayed as a summary at the bottom of the table?
39+
*/
40+
public $summary = false;
41+
3642
/**
3743
* @var string (optional) you can group your columns to let the user toggle the visibility of a group at once.
3844
*/
@@ -113,6 +119,20 @@ public function label($label)
113119
return $this;
114120
}
115121

122+
public function enableSummary()
123+
{
124+
$this->summary = true;
125+
126+
return $this;
127+
}
128+
129+
public function disableSummary()
130+
{
131+
$this->summary = false;
132+
133+
return $this;
134+
}
135+
116136
public function sortBy($column)
117137
{
118138
$this->sort = $column;

src/Http/Livewire/LivewireDatatable.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ public function getViewColumns()
202202
'hidden',
203203
'label',
204204
'group',
205+
'summary',
205206
'content',
206207
'align',
207208
'type',
@@ -617,6 +618,34 @@ public function getSortString()
617618
}
618619
}
619620

621+
/**
622+
* @return bool has the user defined at least one column to display a summary row?
623+
*/
624+
public function hasSummaryRow()
625+
{
626+
foreach ($this->columns as $column) {
627+
if ($column['summary']) {
628+
return true;
629+
}
630+
}
631+
632+
return false;
633+
}
634+
635+
/**
636+
* Attempt so summarize each data cell of the given column.
637+
* In case we have a string or any other value that is not summarizable,
638+
* we return a empty string.
639+
*/
640+
public function summarize($column)
641+
{
642+
try {
643+
return $this->results->sum($column);
644+
} catch (\TypeError $e) {
645+
return '';
646+
}
647+
}
648+
620649
public function updatingPerPage()
621650
{
622651
$this->refreshLivewireDatatable();

0 commit comments

Comments
 (0)