Skip to content

Commit 730f198

Browse files
authored
Merge pull request #322 from thyseus/introduce-summary-row
introduce ability to append a summary row to a datatable
2 parents 0c44d0a + 4a83f08 commit 730f198

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
@@ -160,7 +160,10 @@ class ComplexDemoTable extends LivewireDatatable
160160

161161
(new LabelColumn())
162162
->label('My custom heading')
163-
->content('This fixed string appears in every row')
163+
->content('This fixed string appears in every row'),
164+
165+
NumberColumn::name('dollars_spent')
166+
->enableSummary(),
164167
];
165168
}
166169
}
@@ -240,6 +243,23 @@ public function columns()
240243
->label('Planet'),
241244
```
242245

246+
### Summary row
247+
If you need to summarize all cells of a specific column, you can use `enableSummary()`:
248+
249+
```php
250+
public function columns()
251+
{
252+
return [
253+
Column::name('dollars_spent')
254+
->label('Expenses in Dollar')
255+
->enableSummary(),
256+
257+
Column::name('euro_spent')
258+
->label('Expenses in Euro')
259+
->enableSummary(),
260+
```
261+
262+
243263
### Custom column names
244264
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:
245265

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
@@ -37,6 +37,12 @@ class Column
3737
public $maxWidth;
3838
public $exportCallback;
3939

40+
/**
41+
* @var bool should the sum of all summarizable cells in this column be
42+
* displayed as a summary at the bottom of the table?
43+
*/
44+
public $summary = false;
45+
4046
/**
4147
* @var string (optional) you can group your columns to let the user toggle the visibility of a group at once.
4248
*/
@@ -129,6 +135,20 @@ public function label($label)
129135
return $this;
130136
}
131137

138+
public function enableSummary()
139+
{
140+
$this->summary = true;
141+
142+
return $this;
143+
}
144+
145+
public function disableSummary()
146+
{
147+
$this->summary = false;
148+
149+
return $this;
150+
}
151+
132152
public function setIndex($index)
133153
{
134154
$this->index = $index;

src/Http/Livewire/LivewireDatatable.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ public function getViewColumns()
268268
'hidden',
269269
'label',
270270
'group',
271+
'summary',
271272
'content',
272273
'align',
273274
'type',
@@ -713,6 +714,34 @@ public function getSortString()
713714
}
714715
}
715716

717+
/**
718+
* @return bool has the user defined at least one column to display a summary row?
719+
*/
720+
public function hasSummaryRow()
721+
{
722+
foreach ($this->columns as $column) {
723+
if ($column['summary']) {
724+
return true;
725+
}
726+
}
727+
728+
return false;
729+
}
730+
731+
/**
732+
* Attempt so summarize each data cell of the given column.
733+
* In case we have a string or any other value that is not summarizable,
734+
* we return a empty string.
735+
*/
736+
public function summarize($column)
737+
{
738+
try {
739+
return $this->results->sum($column);
740+
} catch (\TypeError $e) {
741+
return '';
742+
}
743+
}
744+
716745
public function updatingPerPage()
717746
{
718747
$this->refreshLivewireDatatable();

0 commit comments

Comments
 (0)