columns = $columns; $this->rows = $rows; $this->addRoute = $addRoute; } /** * 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 or deselect all rows */ public function selectAllRows() { if ($this->selectAll) { $this->selected = collect($this->rows)->pluck('id')->toArray(); } else { $this->selected = []; } } /** * Select or deselect a single row */ public function selectRow($rowId) { if (in_array($rowId, $this->selected)) { $this->selected = array_diff($this->selected, [$rowId]); } else { $this->selected[] = $rowId; } } /** * Sort rows by a specific field */ 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 } /** * Render the table view */ public function render() { return view('livewire.components.table', [ 'rows' => $this->rows, ]); } }