Export
This example demonstrates how to Export Data.
XLSX
Csv
|
ID
|
Name
|
Chef
|
Price
|
In Stock
|
Created At
|
---|---|---|---|---|---|---|
|
1
|
Click to Search
"Arkansas Possum Pie"
|
|
243,39 €
|
Yes
|
06/07/2024
|
|
2
|
Click to Search
"Albacore Tuna Melt"
|
Vitor
|
63,11 €
|
Yes
|
03/07/2024
|
|
3
|
Click to Search
"борщ"
|
Dan
|
100,19 €
|
Yes
|
26/06/2024
|
|
4
|
Click to Search
"Bacalhau com natas"
|
Dan
|
238,11 €
|
Yes
|
02/06/2024
|
|
5
|
Click to Search
"Baba Ghanoush"
|
Vitor
|
143,09 €
|
No
|
30/05/2024
|
|
6
|
Click to Search
"Bacon Cheeseburger"
|
|
257,39 €
|
Yes
|
13/06/2024
|
|
7
|
Click to Search
"Baked potato"
|
|
171,89 €
|
No
|
05/07/2024
|
|
8
|
Click to Search
"Baklava"
|
|
143,46 €
|
No
|
14/07/2024
|
|
9
|
Click to Search
"Bangers and mash"
|
Luan
|
253,72 €
|
No
|
26/05/2024
|
|
10
|
Click to Search
"Black Pudding"
|
Dan
|
248,73 €
|
No
|
10/07/2024
|
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\ExportTable; use App\Models\Dish;use Illuminate\Database\Eloquent\Builder;use Illuminate\Support\Carbon;use Illuminate\Support\Number;use PowerComponents\LivewirePowerGrid\Column;use PowerComponents\LivewirePowerGrid\Exportable;use PowerComponents\LivewirePowerGrid\Footer;use PowerComponents\LivewirePowerGrid\Header;use PowerComponents\LivewirePowerGrid\PowerGrid;use PowerComponents\LivewirePowerGrid\PowerGridComponent;use PowerComponents\LivewirePowerGrid\PowerGridFields;use PowerComponents\LivewirePowerGrid\Traits\WithExport; final class ExportTable extends PowerGridComponent{ use WithExport; public function setUp(): array { $this->showCheckBox(); return [ Exportable::make('export') ->striped() ->columnWidth([ 2 => 30, ]) ->type(Exportable::TYPE_XLS, Exportable::TYPE_CSV), Header::make() ->showSearchInput(), Footer::make() ->showPerPage() ->showRecordCount(), ]; } public function datasource(): ?Builder { return Dish::with('category'); } public function fields(): PowerGridFields { return PowerGrid::fields() ->add('id') ->add('name') ->add('link', function (Dish $dish) { return sprintf( 'Click to Search "<a target="_blank" class="underline text-blue-600 hover:text-blue-800 visited:text-purple-600" href="https://www.google.com/search?q=%s">%s</a>"', urlencode(e($dish->name)), e($dish->name) ); }) ->add('serving_at') ->add('price') ->add('price_in_eur', fn ($dish) => Number::currency($dish->price, in: 'EUR', locale: 'pt_PT')) ->add('chef_name', fn ($dish) => e($dish->chef?->name)) ->add('in_stock') ->add('in_stock_label', fn ($dish) => $dish->in_stock ? 'Yes' : 'No') ->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(), //Displayed on the grid, but no in the exported file Column::make('Name', 'link', 'name') ->visibleInExport(visible: true) ->sortable(), //Hidden on the grid, but available in the exported file Column::make('Name', 'name') ->searchable() ->hidden() ->visibleInExport(visible: true), Column::make('Chef', 'chef_name', 'dishes.chef_name') ->searchable() ->placeholder('Chef placeholder') ->sortable(), Column::make('Price', 'price_in_eur', 'price') ->searchable() ->sortable(), Column::make('In Stock', 'in_stock_label', 'in_stock'), Column::make('Created At', 'created_at_formatted'), ]; }}
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.
|