Summarize
This example uses the Column Summary feature.
ID
|
Name
|
Calories
|
Price
|
Created At
|
---|---|---|---|---|
|
|
Average: 555,47 kcal
|
Sum Price: 31 723,58 €
Count Price: 192 price(s) Avg Price: 165,23 € |
|
1
|
Arkansas Possum Pie
|
419 kcal
|
243,39 €
|
06/07/2024
|
2
|
Albacore Tuna Melt
|
530 kcal
|
63,11 €
|
03/07/2024
|
3
|
борщ
|
835 kcal
|
100,19 €
|
26/06/2024
|
4
|
Bacalhau com natas
|
560 kcal
|
238,11 €
|
02/06/2024
|
5
|
Baba Ghanoush
|
750 kcal
|
143,09 €
|
30/05/2024
|
6
|
Bacon Cheeseburger
|
579 kcal
|
257,39 €
|
13/06/2024
|
7
|
Baked potato
|
747 kcal
|
171,89 €
|
05/07/2024
|
8
|
Baklava
|
120 kcal
|
143,46 €
|
14/07/2024
|
9
|
Bangers and mash
|
138 kcal
|
253,72 €
|
26/05/2024
|
10
|
Black Pudding
|
660 kcal
|
248,73 €
|
10/07/2024
|
|
|
|
Min Price: 50,69 €
Max Price: 278,65 € |
|
Disclaimer: Table data is randomly generated for
illustrative purposes only. The information here is not a reflection of the actual market and does
not constitute business, financial, or medical advice.
<?php namespace App\Livewire\Examples\SummarizeTable; use App\Models\Dish;use Illuminate\Database\Eloquent\Builder;use Illuminate\Support\Carbon;use Illuminate\Support\Number;use PowerComponents\LivewirePowerGrid\Column;use PowerComponents\LivewirePowerGrid\Footer;use PowerComponents\LivewirePowerGrid\Header;use PowerComponents\LivewirePowerGrid\PowerGrid;use PowerComponents\LivewirePowerGrid\PowerGridComponent;use PowerComponents\LivewirePowerGrid\PowerGridFields; class SummarizeTable extends PowerGridComponent{ public function setUp(): array { return [ Header::make() ->showSearchInput(), Footer::make() ->showPerPage() ->showRecordCount(), ]; } public function datasource(): ?Builder { return Dish::query(); } public function fields(): PowerGridFields { return PowerGrid::fields() ->add('id') ->add('name') ->add('calories', fn ($dish) => $dish->calories . ' kcal') ->add('price_in_eur', fn ($dish) => Number::currency($dish->price, in: 'EUR', locale: 'pt_PT')) ->add('created_at_formatted', fn ($dish) => Carbon::parse($dish->created_at)->format('d/m/Y')); } public function columns(): array { return [ Column::make('ID', 'id') ->searchable() ->sortable(), Column::make('Name', 'name') ->searchable() ->sortable(), Column::make('Calories', 'calories', 'calories') ->withAvg('Average', header: true, footer: false) ->sortable(), Column::make('Price', 'price_in_eur', 'price') ->withSum('Sum Price', header: true, footer: false) ->withAvg('Avg Price', header: true, footer: false) ->withCount('Count Price', header: true, footer: false) ->withMin('Min Price', header: false, footer: true) ->withMax('Max Price', header: false, footer: true) ->sortable(), Column::make('Created At', 'created_at_formatted'), ]; } public function summarizeFormat(): array { return [ 'price.{sum,avg,min,max}' => fn ($value) => Number::currency($value, in: 'EUR', locale: 'pt_PT'), 'price.{count}' => fn ($value) => Number::format($value, locale: 'br') . ' price(s)', 'calories.{avg}' => fn ($value) => Number::format($value, locale: 'br', precision: 2) . ' kcal', ]; }}
Code highlighting provided by Torchlight.dev
Here you can find all relevant packages installed on this demo.
Name
|
Version
|
Description
|
---|---|---|
laravel/framework
|
v11
|
The Laravel Framework.
|
livewire/livewire
|
v3.5.4
|
A front-end framework for Laravel.
|
openspout/openspout
|
v4.24.3
|
PHP Library to read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way
|
power-components/livewire-powergrid
|
5.x-dev
|
PowerGrid generates Advanced Datatables using Laravel Livewire.
|