props = $props; $this->mounted = true; $this->columns = $props['columns'] ?? []; $this->fetchData(); } public function updatedSearchFilter() { $this->resetPage(); $this->fetchData(); } public function setSort($field) { if ($this->sortBy === $field) { $this->sortOrder = $this->sortOrder === 'asc' ? 'desc' : 'asc'; } else { $this->sortBy = $field; $this->sortOrder = 'asc'; } $this->fetchData(); } public function clearAll() { $this->reset(['searchFilter', 'filters', 'sortBy', 'sortOrder', 'selectedRowKeys']); $this->fetchData(); } public function updatePage($page) { $this->currentPage = $page; $this->fetchData(); } public function delete($uuid) { try { $api = env('REACT_APP_STATION_API'); $path = substr($this->props['location']['pathname'], 1); $response = Http::delete("{$api}{$path}/{$uuid}"); if ($response->successful()) { session()->flash('success', 'Record deleted.'); $this->fetchData(); } else { session()->flash('error', 'Deletion failed.'); } } catch (\Exception $e) { session()->flash('error', $e->getMessage()); } } public function handleBatchDelete() { if (empty($this->selectedRowKeys)) return; try { $response = Http::delete("your-external-api-endpoint/batch-delete", [ 'data' => [$this->props['keyValue'] => $this->selectedRowKeys] ]); if ($response->successful()) { session()->flash('success', 'Records deleted.'); $this->selectedRowKeys = []; $this->fetchData(); } else { session()->flash('error', 'Deletion failed.'); } } catch (\Exception $e) { session()->flash('error', $e->getMessage()); } } public function fetchData() { $this->loading = true; $params = array_filter([ 'page' => $this->currentPage, 'page_size' => $this->perPage, 'search' => $this->searchFilter, 'sort_by' => $this->sortBy, 'sort_order' => $this->sortOrder, ...$this->filters, ]); try { $defaultUrl = $this->props['url']['default']; $response = Http::get("your-external-api-endpoint/{$defaultUrl}", $params); $data = $response->json(); $this->data = $data['data'] ?? []; $this->total = $data['total'] ?? 0; if (empty($this->data) && $this->props['isEmptyMessagePopUp']) { session()->flash('info', 'No records found.'); } } catch (\Exception $e) { session()->flash('error', $e->getMessage()); $this->data = []; $this->total = 0; } finally { $this->loading = false; } } public function render() { return view('livewire.station-table', [ 'columns' => $this->columns, 'keyValue' => $this->props['keyValue'] ?? 'id', 'apiDelete' => $this->props['url']['apiDelete'] ?? false, 'url' => $this->props['url'] ?? [], ]); } }