From 817dac463c8c3105b40799a3272cde8488af4d90 Mon Sep 17 00:00:00 2001 From: erishBRBS Date: Sun, 20 Apr 2025 14:16:44 +0800 Subject: [PATCH] table component added --- app/Livewire/Components/Table.php | 86 ++++++------ app/Livewire/Notification.php | 27 +++- app/Livewire/UserManagement.php | 28 ++-- resources/views/layouts/dashboard.blade.php | 2 +- .../views/livewire/components/table.blade.php | 128 ++++++------------ .../notification/notification.blade.php | 13 +- .../user-management/user-management.blade.php | 18 +-- 7 files changed, 138 insertions(+), 164 deletions(-) diff --git a/app/Livewire/Components/Table.php b/app/Livewire/Components/Table.php index 1675169..ededad4 100644 --- a/app/Livewire/Components/Table.php +++ b/app/Livewire/Components/Table.php @@ -8,62 +8,56 @@ class Table extends Component { public $columns = []; public $rows = []; - public $search = ''; - public $sortField = ''; - public $sortDirection = 'asc'; - public $currentPage = 1; - public $perPage = 10; - public $checkboxes = false; - public $showActions = false; - public $actions = []; - public $addButtonLabel = ''; - public $searchable = false; // Add this property + public $selected = []; // Holds the selected row IDs + public $selectAll = false; // To track if 'Select All' is checked + public $hasActions = false; + public $isViewPage = false; - // Sort the table by the given field - public function sortBy($field) + // Add new methods for handling actions + public function editRow($rowId) { - if ($this->sortField === $field) { - $this->sortDirection = $this->sortDirection === 'asc' ? 'desc' : 'asc'; + // Logic for editing a row (e.g., redirect to the edit page or open modal) + session()->flash('message', 'Edit action for user ID: ' . $rowId); + } + + public function deleteRow($rowId) + { + // Logic for deleting a row (e.g., delete from database) + session()->flash('message', 'Delete action for user ID: ' . $rowId); + } + + public function viewRow($rowId) + { + // Logic for viewing a row (e.g., redirect to the view page or open a modal) + session()->flash('message', 'View action for user ID: ' . $rowId); + } + + + + public function selectAllRows() + { + // Convert rows to a collection if it is an array, then pluck the 'id' + if ($this->selectAll) { + $this->selected = collect($this->rows)->pluck('id')->toArray(); } else { - $this->sortField = $field; - $this->sortDirection = 'asc'; + $this->selected = []; } } - // Calculate total pages for pagination - public function calculateTotalPages() + public function selectRow($rowId) { - return ceil(count($this->rows) / $this->perPage); - } - - // Get the paginated rows for the current page - public function getPaginatedRowsProperty() - { - $sorted = collect($this->rows); - - if ($this->sortField) { - $sorted = $this->sortDirection === 'asc' - ? $sorted->sortBy($this->sortField) - : $sorted->sortByDesc($this->sortField); + if (in_array($rowId, $this->selected)) { + $this->selected = array_diff($this->selected, [$rowId]); + } else { + $this->selected[] = $rowId; } - - return $sorted - ->slice(($this->currentPage - 1) * $this->perPage, $this->perPage) - ->values(); - } - - - // Set the current page for pagination - public function setPage($page) - { - $this->currentPage = max(1, min($this->calculateTotalPages(), $page)); } public function render() { - return view('livewire.components.table', [ - 'paginatedRows' => $this->paginatedRows, // 👈 explicitly pass computed property - ]); + return view('livewire.components.table'); } - -} \ No newline at end of file +} + + + diff --git a/app/Livewire/Notification.php b/app/Livewire/Notification.php index 0211a4f..63b0f6f 100644 --- a/app/Livewire/Notification.php +++ b/app/Livewire/Notification.php @@ -5,8 +5,33 @@ namespace App\Livewire; use Livewire\Component; class Notification extends Component { + public $search = ''; + public $notifs = []; + + // protected $queryString = ['search']; + + // // Reset the current page when search is updated + // public function updatingSearch() + // { + // $this->search = $this->search; + // } + + public function mount() + { + $this->loadNotifications(); + } + + // Get filtered users based on the search input + public function loadNotifications() + { + $this->notifs = collect(json_decode(file_get_contents(storage_path('app/notifs.json')), true)); + } + + public function render() { - return view('livewire.notification.notification'); + return view('livewire.notification.notification', [ + 'notification' => $this->notifs, // Pass filtered notifs here + ]); } } diff --git a/app/Livewire/UserManagement.php b/app/Livewire/UserManagement.php index b2e7728..e07229a 100644 --- a/app/Livewire/UserManagement.php +++ b/app/Livewire/UserManagement.php @@ -9,35 +9,27 @@ class UserManagement extends Component public $search = ''; public $users = []; - protected $queryString = ['search']; + // protected $queryString = ['search']; - // Reset the current page when search is updated - public function updatingSearch() - { - $this->search = $this->search; - } + // // Reset the current page when search is updated + // public function updatingSearch() + // { + // $this->search = $this->search; + // } public function mount() { $this->loadUsers(); } + + // Get filtered users based on the search input public function loadUsers() { - $users = collect(json_decode(file_get_contents(storage_path('app/users.json')), true)); - - if ($this->search) { - $this->users = $users->filter(function ($user) { - return str_contains(strtolower($user['username']), strtolower($this->search)) || - str_contains(strtolower($user['first_name']), strtolower($this->search)) || - str_contains(strtolower($user['last_name']), strtolower($this->search)) || - str_contains(strtolower($user['email']), strtolower($this->search)); - }); - } else { - $this->users = $users; - } + $this->users = collect(json_decode(file_get_contents(storage_path('app/users.json')), true)); } + public function render() { diff --git a/resources/views/layouts/dashboard.blade.php b/resources/views/layouts/dashboard.blade.php index fba2f71..db9c229 100644 --- a/resources/views/layouts/dashboard.blade.php +++ b/resources/views/layouts/dashboard.blade.php @@ -3,7 +3,7 @@ - + Unioil @vite(['resources/css/app.css', 'resources/js/app.js']) @livewireStyles diff --git a/resources/views/livewire/components/table.blade.php b/resources/views/livewire/components/table.blade.php index c0aee1d..896531a 100644 --- a/resources/views/livewire/components/table.blade.php +++ b/resources/views/livewire/components/table.blade.php @@ -1,112 +1,72 @@
-
-
- @if ($searchable) - - @endif - @if ($addButtonLabel) - - @endif -
- - +
+
- @if ($checkboxes) - - @endif + + - @foreach ($columns as $col) - @endforeach - {{-- Status Column --}} - - {{-- Actions Column --}} - - - @if ($showActions) - + @if ($hasActions) + @endif - @forelse ($paginatedRows as $row) + @forelse ($rows as $row) - @if ($checkboxes) - - @endif + + - @foreach ($columns as $col) - @endforeach - @if ($showActions) - @endif @empty - + @endforelse
+ + -
- {{ $col['label'] }} - - {{-- Sort Icons --}} - @if ($sortField === $col['field']) - @if ($sortDirection === 'asc') - {{-- Ascending Icon --}} - - - - @else - {{-- Descending Icon --}} - - - - @endif - @else - {{-- Neutral Sort Icon --}} - - - - @endif -
+ @foreach ($columns as $column) +
+ {{ $column['label'] }} ActionActions
+ + - {{ $row[$col['field']] ?? '-' }} + @foreach ($columns as $column) + + {{ $row[$column['field']] ?? '' }} - @foreach ($actions as $action) - + @if($isViewPage) + + - @endforeach + @else + + + + + + + + + + @endif
- No data found. - No data available
- - {{-- Pagination Controls --}} -
- @for ($page = 1; $page <= $this->calculateTotalPages(); $page++) - - @endfor -
diff --git a/resources/views/livewire/notification/notification.blade.php b/resources/views/livewire/notification/notification.blade.php index 8f06873..7205b3a 100644 --- a/resources/views/livewire/notification/notification.blade.php +++ b/resources/views/livewire/notification/notification.blade.php @@ -1,6 +1,17 @@
{{-- Top Nav --}} @include('livewire.notification.top-nav.notification') -

This is notification page

+ +
diff --git a/resources/views/livewire/user-management/user-management.blade.php b/resources/views/livewire/user-management/user-management.blade.php index eba18cb..16cdc74 100644 --- a/resources/views/livewire/user-management/user-management.blade.php +++ b/resources/views/livewire/user-management/user-management.blade.php @@ -5,20 +5,12 @@ ['label' => 'Username', 'field' => 'username'], ['label' => 'First Name', 'field' => 'first_name'], ['label' => 'Last Name', 'field' => 'last_name'], - ['label' => 'Role', 'field' => 'role'], ['label' => 'Email', 'field' => 'email'], + ['label' => 'Role', 'field' => 'role'], ['label' => 'Status', 'field' => 'status'], ]" - :rows="$users" - :searchable="true" - :search="$search" - :checkboxes="true" - :showActions="true" - :actions="[ - ['method' => 'editUser', 'icon' => '✏️'], - ['method' => 'deleteUser', 'icon' => '🗑️'], - ['method' => 'scheduleUser', 'icon' => '⏱️'], - ]" - addButtonLabel="Add User" -/> + :rows="$users" + :hasActions="true" + :isViewPage="false" + />