Created
April 19, 2023 20:17
-
-
Save sjardim/59a919c7d128d16e80edea0dcf766f02 to your computer and use it in GitHub Desktop.
Adds a sum, avg, count column to a Laravel Filament table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Adapted from https://filamentphp.com/tricks/aggregate-data-in-table-footer | |
// On your Resource list page | |
protected function getTableContentFooter(): View | |
{ | |
return view('tables.footer', [ | |
'calc_columns' => [ | |
[ | |
'column' => 'monthly_fee_in_cents', | |
'operation' => 'sum', | |
'format'=> 'money', | |
'currency' => 'USD', | |
], | |
], | |
'hasCheckboxColumn' => false | |
]); | |
} | |
// On the view tables.footer | |
@php use Filament\Tables\Table; @endphp | |
<x-tables::row> | |
@if($hasCheckboxColumn) | |
<x-tables::cell> | |
{{-- for the checkbox column --}} | |
</x-tables::cell> | |
@endif | |
@foreach ($columns as $column) | |
<x-tables::cell | |
wire:loading.remove.delay | |
wire:target="{{ implode(',', Table::LOADING_TARGETS) }}" | |
> | |
@for ($i = 0; $i < count($calc_columns); $i++ ) | |
@if ($column->getName() == $column = $calc_columns[$i]['column']) | |
@php | |
$operation = $calc_columns[$i]['operation']; | |
$money = $calc_columns[$i]['format'] ?? false; | |
$currency = $calc_columns[$i]['currency'] ?? 'USD'; | |
@endphp | |
<div class="filament-tables-column-wrapper"> | |
<div class="filament-tables-text-column px-4 py-2 flex w-full {{$money ? 'justify-end text-end' : 'justify-start text-start'}}"> | |
<div class="inline-flex items-center space-x-1 rtl:space-x-reverse"> | |
<span class="font-bold"> | |
@if($money) | |
{{ money($records->$operation($column), $currency)->format() }} | |
@else | |
{{ $records->$operation($column) }} | |
@endif | |
</span> | |
</div> | |
</div> | |
</div> | |
@endif | |
@endfor | |
</x-tables::cell> | |
@endforeach | |
</x-tables::row> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Preview:
