diff --git a/app/Livewire/Components/Table.php b/app/Livewire/Components/Table.php index 8bcaefa..5085031 100644 --- a/app/Livewire/Components/Table.php +++ b/app/Livewire/Components/Table.php @@ -6,45 +6,28 @@ use Livewire\Component; class Table extends Component { - public $columns = []; - public $rows = []; - public $selected = []; - public $addRoute = null; - public $hasAddButton = true; - public $selectAll = false; - public $hasCheckbox = true; - public $hasActions = false; - public $isViewPage = false; - public $search = ''; - public $sortField = null; // Column to sort - public $sortDirection = 'asc'; // Default sort direction + // Table Configuration + public $columns = []; // Columns to display + public $rows = []; // Data rows for the table + public $selected = []; // Selected row IDs + public $addRoute = null; // Route for adding new rows (optional) + public $hasAddButton = true; // Whether the 'Add' button is shown + public $selectAll = false; // Whether 'Select All' is active + public $hasCheckbox = true; // Whether checkboxes are shown for row selection + public $hasActions = false; // Whether action buttons (like Edit/Delete) are shown + public $isViewPage = false; // Whether the table is for a view page + public $search = ''; // Search query for filtering rows + public $sortField = null; // Column to sort by + public $sortDirection = 'asc'; // Sorting direction (asc or desc) - // Inside App\Livewire\Components\Table - -public $showModal = false; -public $modalMode = 'view'; // or 'edit' -public $modalData = []; - -public function viewRow($id) -{ - $this->modalData = collect($this->rows)->firstWhere('id', $id) ?? []; - $this->modalMode = 'view'; - $this->showModal = true; -} - -public function editRow($id) -{ - $this->modalData = collect($this->rows)->firstWhere('id', $id) ?? []; - $this->modalMode = 'edit'; - $this->showModal = true; -} - -public function closeModal() -{ - $this->showModal = false; - $this->modalData = []; -} + // Modal Configuration + public $showModal = false; // Whether the modal is visible + public $modalMode = 'view'; // Modal mode: 'view' or 'edit' + public $modalData = []; // Data for the modal + /** + * Initialize the table with given columns and rows + */ public function mount($columns, $rows, $addRoute = null) { $this->columns = $columns; @@ -52,13 +35,46 @@ public function closeModal() $this->addRoute = $addRoute; } - // Update rows based on search + /** + * View a row's details in the modal + */ + public function viewRow($id) + { + $this->modalData = collect($this->rows)->firstWhere('id', $id) ?? []; + $this->modalMode = 'view'; + $this->showModal = true; + } + + /** + * Edit a row's details in the modal + */ + public function editRow($id) + { + $this->modalData = collect($this->rows)->firstWhere('id', $id) ?? []; + $this->modalMode = 'edit'; + $this->showModal = true; + } + + /** + * Close the modal + */ + public function closeModal() + { + $this->showModal = false; + $this->modalData = []; + } + + /** + * Update the selected rows based on search input + */ public function updatedSearch() { $this->selected = []; } - // Select all rows + /** + * Select or deselect all rows + */ public function selectAllRows() { if ($this->selectAll) { @@ -68,7 +84,9 @@ public function closeModal() } } - // Select single row + /** + * Select or deselect a single row + */ public function selectRow($rowId) { if (in_array($rowId, $this->selected)) { @@ -78,7 +96,9 @@ public function closeModal() } } - // Sorting + /** + * Sort rows by a specific field + */ public function sortBy($field) { if ($this->sortField === $field) { @@ -93,6 +113,9 @@ public function closeModal() }, SORT_REGULAR, $this->sortDirection === 'desc')->values()->all(); // Use all() to return plain array again } + /** + * Render the table view + */ public function render() { return view('livewire.components.table', [ diff --git a/resources/views/livewire/components/table.blade.php b/resources/views/livewire/components/table.blade.php index 6dbf7c9..a053395 100644 --- a/resources/views/livewire/components/table.blade.php +++ b/resources/views/livewire/components/table.blade.php @@ -1,127 +1,146 @@
+
- + + @if ($hasAddButton && $addRoute) - - + Add - + + Add + @endif
+ + @if ($hasCheckbox) - + @endif + @foreach ($columns as $column) - + @endforeach + @if ($hasActions) - + @endif + + @forelse ($rows as $row) - - @if ($hasCheckbox) - - @endif - - @foreach ($columns as $column) - - @endforeach - - @if ($hasActions) - + + @if ($hasCheckbox) + @endif - - @endif - @if ($showModal) -
-
-
-

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

- -
-
- @foreach ($columns as $column) -
- - @if ($modalMode === 'edit') - - @else -
- {{ $modalData[$column['field']] ?? '' }} -
- @endif -
- @endforeach -
- @if ($modalMode === 'edit') -
- -
- @endif -
-
- @endif - + + + @foreach ($columns as $column) + + @endforeach + + + @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']] ?? '' }} - - @if($isViewPage) - - @else - - - +
+ +
+ {{ $row[$column['field']] ?? '' }} + + @if($isViewPage) + + @else + + + + @endif +
No data available
No data available
+ + + @if ($showModal) +
+
+ +
+

+ {{ $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 +