From 3049bd01e27f1080b562a15ec1b216031eb18c95 Mon Sep 17 00:00:00 2001 From: erishBRBS Date: Sun, 20 Apr 2025 15:33:19 +0800 Subject: [PATCH] sorting by column added --- app/Livewire/Components/Table.php | 57 ++++++++++-------- app/Livewire/UserManagement.php | 20 ++----- .../views/livewire/components/table.blade.php | 58 ++++++++++++------- .../livewire/navigation/drawer.blade.php | 2 +- 4 files changed, 74 insertions(+), 63 deletions(-) diff --git a/app/Livewire/Components/Table.php b/app/Livewire/Components/Table.php index ededad4..ed675af 100644 --- a/app/Livewire/Components/Table.php +++ b/app/Livewire/Components/Table.php @@ -8,35 +8,29 @@ class Table extends Component { public $columns = []; public $rows = []; - public $selected = []; // Holds the selected row IDs - public $selectAll = false; // To track if 'Select All' is checked - public $hasActions = false; - public $isViewPage = false; + public $selected = []; + public $selectAll = false; + public $hasActions = false; + public $isViewPage = false; + public $search = ''; + public $sortField = null; // Column to sort + public $sortDirection = 'asc'; // Default sort direction - // Add new methods for handling actions - public function editRow($rowId) + public function mount($columns, $rows) { - // Logic for editing a row (e.g., redirect to the edit page or open modal) - session()->flash('message', 'Edit action for user ID: ' . $rowId); + $this->columns = $columns; + $this->rows = $rows; } - public function deleteRow($rowId) + // Update rows based on search + public function updatedSearch() { - // Logic for deleting a row (e.g., delete from database) - session()->flash('message', 'Delete action for user ID: ' . $rowId); + $this->selected = []; } - 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); - } - - - + // Select all rows 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 { @@ -44,6 +38,7 @@ class Table extends Component } } + // Select single row public function selectRow($rowId) { if (in_array($rowId, $this->selected)) { @@ -53,11 +48,25 @@ class Table extends Component } } + // Simplified version + public function sortBy($field) + { + if ($this->sortField === $field) { + $this->sortDirection = $this->sortDirection === 'asc' ? 'desc' : 'asc'; + } else { + $this->sortField = $field; + $this->sortDirection = 'asc'; + } + + $this->rows = collect($this->rows)->sortBy(function ($row) { + return $row[$this->sortField] ?? null; + }, SORT_REGULAR, $this->sortDirection === 'desc')->values()->all(); // Use all() to return plain array again + } + public function render() { - return view('livewire.components.table'); + return view('livewire.components.table', [ + 'rows' => $this->rows, + ]); } } - - - diff --git a/app/Livewire/UserManagement.php b/app/Livewire/UserManagement.php index e07229a..da3760b 100644 --- a/app/Livewire/UserManagement.php +++ b/app/Livewire/UserManagement.php @@ -1,40 +1,28 @@ search = $this->search; - // } - public function mount() { - $this->loadUsers(); + $this->loadUsers(); // Load users initially } - - - // Get filtered users based on the search input public function loadUsers() { $this->users = collect(json_decode(file_get_contents(storage_path('app/users.json')), true)); } - public function render() { return view('livewire.user-management.user-management', [ - 'users' => $this->users, // Pass filtered users here + 'users' => $this->users, // Pass all users to the table ]); } -} \ No newline at end of file +} diff --git a/resources/views/livewire/components/table.blade.php b/resources/views/livewire/components/table.blade.php index 896531a..3b7a59f 100644 --- a/resources/views/livewire/components/table.blade.php +++ b/resources/views/livewire/components/table.blade.php @@ -1,5 +1,13 @@ -
-
+
+
+ +
+ +
+ @@ -9,12 +17,25 @@ @foreach ($columns as $column) - @endforeach - @if ($hasActions) + @if ($hasActions) @endif @@ -34,28 +55,21 @@ @endforeach - @if ($hasActions) - diff --git a/resources/views/livewire/navigation/drawer.blade.php b/resources/views/livewire/navigation/drawer.blade.php index 5ceed88..c4ca3dc 100644 --- a/resources/views/livewire/navigation/drawer.blade.php +++ b/resources/views/livewire/navigation/drawer.blade.php @@ -1,4 +1,4 @@ -
+
Unioil Logo
+ {{ $column['label'] }} + + @if ($sortField === $column['field']) + @if ($sortDirection === 'asc') + + + @else + + + @endif + @else + + + @endif Actions
- @if($isViewPage) - - + @if($isViewPage) + @else - - - - - - - - - @endif