From bc8f68a61001022c2fddcdf23ea516827c180815 Mon Sep 17 00:00:00 2001 From: erishBRBS Date: Sun, 27 Apr 2025 08:32:31 +0800 Subject: [PATCH] export csv added --- app/Livewire/Components/Table.php | 37 +++ app/Livewire/MobileUsageReport.php | 16 +- app/Livewire/RegistrationReport.php | 18 +- app/Livewire/StationRatingReport.php | 16 +- app/Livewire/TopUpUsageReport.php | 21 +- .../views/livewire/components/table.blade.php | 240 ++++++++++-------- .../report/mobile-usage-report.blade.php | 16 +- .../report/registration-report.blade.php | 15 +- .../report/station-rating-report.blade.php | 17 +- .../report/top-up-usage-report.blade.php | 22 +- .../top-nav/user-management.blade.php | 2 + 11 files changed, 301 insertions(+), 119 deletions(-) diff --git a/app/Livewire/Components/Table.php b/app/Livewire/Components/Table.php index 0c6f9e2..d33f853 100644 --- a/app/Livewire/Components/Table.php +++ b/app/Livewire/Components/Table.php @@ -2,6 +2,7 @@ namespace App\Livewire\Components; +use Symfony\Component\HttpFoundation\StreamedResponse; use Livewire\Component; class Table extends Component @@ -20,6 +21,12 @@ class Table extends Component public $search = ''; // Search query for filtering rows public $sortField = null; // Column to sort by public $sortDirection = 'asc'; // Sorting direction (asc or desc) + public bool $hasSearch = true; + + + public $startDate; + public $endDate; + // Modal Configuration public $showModal = false; // Whether the modal is visible @@ -114,6 +121,36 @@ class Table extends Component }, SORT_REGULAR, $this->sortDirection === 'desc')->values()->all(); // Use all() to return plain array again } + /** + * Download the data from the tables + */ + + public function export() + { + $filename = 'export.csv'; + + return response()->streamDownload(function () { + // Open output stream + $handle = fopen('php://output', 'w'); + + // Write the CSV header + fputcsv($handle, collect($this->columns)->pluck('label')->toArray()); + + // Write the rows + foreach ($this->rows as $row) { + fputcsv($handle, collect($this->columns)->map(function ($column) use ($row) { + return $row[$column['field']] ?? ''; + })->toArray()); + } + + // Close the output stream + fclose($handle); + }, $filename, [ + 'Content-Type' => 'text/csv', + 'Content-Disposition' => "attachment; filename={$filename}", + ]); + } + /** * Render the table view */ diff --git a/app/Livewire/MobileUsageReport.php b/app/Livewire/MobileUsageReport.php index 2484854..c613852 100644 --- a/app/Livewire/MobileUsageReport.php +++ b/app/Livewire/MobileUsageReport.php @@ -8,8 +8,22 @@ use Livewire\Attributes\Layout; // Required for layout declaration #[Layout('layouts.dashboard')] // Attribute syntax for Laravel 11 class MobileUsageReport extends Component { + public $mobileUsageReport = []; + + public function mount() + { + $this->loadMobileUsageReport(); // Load mobileUsageReport initially + } + + public function loadMobileUsageReport() + { + $this->mobileUsageReport = collect(json_decode(file_get_contents(storage_path('app/mobile-usage.json')), true)); + } + public function render() { - return view('livewire.report.mobile-usage-report'); + return view('livewire.report.mobile-usage-report', [ + 'mobileUsageReport' => $this->mobileUsageReport, + ]); } } diff --git a/app/Livewire/RegistrationReport.php b/app/Livewire/RegistrationReport.php index bd0f44a..1a38364 100644 --- a/app/Livewire/RegistrationReport.php +++ b/app/Livewire/RegistrationReport.php @@ -8,8 +8,22 @@ use Livewire\Attributes\Layout; // Required for layout declaration #[Layout('layouts.dashboard')] // Attribute syntax for Laravel 11 class RegistrationReport extends Component { + public $registrationReport = []; + + public function mount() + { + $this->loadRegistrationReport(); // Load registrationReport initially + } + + public function loadRegistrationReport() + { + $this->registrationReport = collect(json_decode(file_get_contents(storage_path('app/registration-report.json')), true)); + } + public function render() { - return view('livewire.report.registration-report'); + return view('livewire.report.registration-report', [ + 'registrationReport' => $this->registrationReport, + ]); } -} +} \ No newline at end of file diff --git a/app/Livewire/StationRatingReport.php b/app/Livewire/StationRatingReport.php index 0fc9708..af5b919 100644 --- a/app/Livewire/StationRatingReport.php +++ b/app/Livewire/StationRatingReport.php @@ -8,8 +8,22 @@ use Livewire\Attributes\Layout; // Required for layout declaration #[Layout('layouts.dashboard')] // Attribute syntax for Laravel 11 class StationRatingReport extends Component { + public $stationRating = []; + + public function mount() + { + $this->loadStationRating(); // Load stationRating initially + } + + public function loadStationRating() + { + $this->stationRating = collect(json_decode(file_get_contents(storage_path('app/station-rating.json')), true)); + } + public function render() { - return view('livewire.report.station-rating-report'); + return view('livewire.report.station-rating-report', [ + 'stationRating' => $this->stationRating, + ]); } } diff --git a/app/Livewire/TopUpUsageReport.php b/app/Livewire/TopUpUsageReport.php index 2f7d39b..14913a5 100644 --- a/app/Livewire/TopUpUsageReport.php +++ b/app/Livewire/TopUpUsageReport.php @@ -8,8 +8,27 @@ use Livewire\Attributes\Layout; // Required for layout declaration #[Layout('layouts.dashboard')] // Attribute syntax for Laravel 11 class TopUpUsageReport extends Component { + public $topUpUsage = []; + + public function mount() + { + $this->loadTopUpUsage(); // Load topUpUsage initially + } + + public function loadTopUpUsage() + { + $this->topUpUsage = collect(json_decode(file_get_contents(storage_path('app/topup-usage-report.json')), true)); + } + public function render() { - return view('livewire.report.top-up-usage-report'); + return view('livewire.report.top-up-usage-report', [ + 'topUpUsage' => $this->topUpUsage, + ]); } } + + + + + diff --git a/resources/views/livewire/components/table.blade.php b/resources/views/livewire/components/table.blade.php index 86701e8..ba69c44 100644 --- a/resources/views/livewire/components/table.blade.php +++ b/resources/views/livewire/components/table.blade.php @@ -1,22 +1,48 @@
- - -
- - - - @if ($hasAddButton && $addRoute) - - + Add - - @endif +
+ @if ($hasSearch ?? true) + + + @else + +
+ + +
+ @endif + + @if ($hasAddButton ?? true) + + + + Add + + @else + + + @endif +
+ @@ -24,30 +50,30 @@ @if ($hasCheckbox) - + @endif @foreach ($columns as $column) - + @endforeach @if ($hasActions) - + @endif @@ -55,53 +81,53 @@ @forelse ($rows as $row) - - - @if ($hasCheckbox) - - @endif + + + @if ($hasCheckbox) + + @endif - - @foreach ($columns as $column) - - @endforeach + + @foreach ($columns as $column) + + @endforeach - - @if ($hasActions) - + + @if ($hasActions) + + + @endif + @empty - - - + + + @endforelse
- - + + - {{ $column['label'] }} - @if ($sortField === $column['field']) - @if ($sortDirection === 'asc') - - @else - - @endif - @else - - @endif - + {{ $column['label'] }} + @if ($sortField === $column['field']) + @if ($sortDirection === 'asc') + + @else + + @endif + @else + + @endif + ActionsActions
- -
+ + - {{ $row[$column['field']] ?? '' }} - + {{ $row[$column['field']] ?? '' }} + - @if($isViewPage) - - @else - - - - @endif - + @if($isViewPage) + + @else + + + @endif -
No data available
No data available
@if($hasDelete) - -
- - -
- @foreach ($columns as $column) -
- - @if ($modalMode === 'edit') - - @else -
- {{ $modalData[$column['field']] ?? '' }} -
- @endif -
- @endforeach -
- - - @if ($modalMode === 'edit') -
- -
- @endif +
+
+ +
+

+ {{ $modalMode === 'view' ? 'View Details' : 'Edit Details' }} +

+
+ + +
+ @foreach ($columns as $column) +
+ + @if ($modalMode === 'edit') + + @else +
+ {{ $modalData[$column['field']] ?? '' }} +
+ @endif +
+ @endforeach +
+ + + @if ($modalMode === 'edit') +
+ +
+ @endif
+
@endif
-
+ \ No newline at end of file diff --git a/resources/views/livewire/report/mobile-usage-report.blade.php b/resources/views/livewire/report/mobile-usage-report.blade.php index 39218d8..c7c1225 100644 --- a/resources/views/livewire/report/mobile-usage-report.blade.php +++ b/resources/views/livewire/report/mobile-usage-report.blade.php @@ -1,5 +1,19 @@
{{-- Top Nav --}} @include('livewire.report.top-nav.mobile-usage-report') -

This is card type page

+
\ No newline at end of file diff --git a/resources/views/livewire/report/registration-report.blade.php b/resources/views/livewire/report/registration-report.blade.php index 07c2dad..631e4ad 100644 --- a/resources/views/livewire/report/registration-report.blade.php +++ b/resources/views/livewire/report/registration-report.blade.php @@ -1,5 +1,18 @@
{{-- Top Nav --}} @include('livewire.report.top-nav.registration-report') -

This is card type page

+
\ No newline at end of file diff --git a/resources/views/livewire/report/station-rating-report.blade.php b/resources/views/livewire/report/station-rating-report.blade.php index 1b55835..37ebed3 100644 --- a/resources/views/livewire/report/station-rating-report.blade.php +++ b/resources/views/livewire/report/station-rating-report.blade.php @@ -1,5 +1,20 @@
{{-- Top Nav --}} @include('livewire.report.top-nav.station-rating-report') -

This is card type page

+
\ No newline at end of file diff --git a/resources/views/livewire/report/top-up-usage-report.blade.php b/resources/views/livewire/report/top-up-usage-report.blade.php index 5f8055d..dca8f8a 100644 --- a/resources/views/livewire/report/top-up-usage-report.blade.php +++ b/resources/views/livewire/report/top-up-usage-report.blade.php @@ -1,5 +1,19 @@
-{{-- Top Nav --}} -@include('livewire.report.top-nav.top-up-usage-report') -

This is card type page

-
\ No newline at end of file + {{-- Top Nav --}} + @include('livewire.report.top-nav.top-up-usage-report') + + + diff --git a/resources/views/livewire/user-management/top-nav/user-management.blade.php b/resources/views/livewire/user-management/top-nav/user-management.blade.php index 8a957d7..f845a56 100644 --- a/resources/views/livewire/user-management/top-nav/user-management.blade.php +++ b/resources/views/livewire/user-management/top-nav/user-management.blade.php @@ -17,4 +17,6 @@
+ +