108 lines
2.9 KiB
PHP
108 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use Livewire\Component;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class TableLayout extends Component
|
|
{
|
|
public $data = [];
|
|
public $total = 0;
|
|
public $loading = false;
|
|
public $columns = [];
|
|
public $selectedRowKeys = [];
|
|
public $currentPage = 1;
|
|
public $perPage = 10;
|
|
public $search = '';
|
|
public $props = [];
|
|
|
|
protected $queryString = ['search', 'currentPage', 'perPage'];
|
|
|
|
public function mount($props)
|
|
{
|
|
$this->props = $props;
|
|
$this->columns = $props['headers'] ?? [];
|
|
$this->fetchData();
|
|
}
|
|
|
|
public function updatedSearch()
|
|
{
|
|
$this->resetPage();
|
|
$this->fetchData();
|
|
}
|
|
|
|
public function updatePage($page)
|
|
{
|
|
$this->currentPage = $page;
|
|
$this->fetchData();
|
|
}
|
|
|
|
public function delete($path, $uuid)
|
|
{
|
|
try {
|
|
$response = Http::delete("your-external-api-endpoint/{$path}/{$uuid}");
|
|
if ($response->successful()) {
|
|
session()->flash('success', 'Record was successfully deleted.');
|
|
$this->fetchData();
|
|
} else {
|
|
session()->flash('error', 'Something went wrong.');
|
|
}
|
|
} 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->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,
|
|
'_limit' => $this->perPage,
|
|
'search' => $this->search,
|
|
]);
|
|
|
|
try {
|
|
$response = Http::get("your-external-api-endpoint/{$this->props['api']['default']}", $params);
|
|
$data = $response->json();
|
|
|
|
$this->data = $data['data'] ?? [];
|
|
$this->total = $data['total'] ?? 0;
|
|
|
|
} catch (\Exception $e) {
|
|
session()->flash('error', $e->getMessage());
|
|
$this->data = [];
|
|
$this->total = 0;
|
|
} finally {
|
|
$this->loading = false;
|
|
}
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.table-layout', [
|
|
'columns' => $this->columns,
|
|
'keyValue' => $this->props['keyValue'] ?? 'id',
|
|
'scrollable' => $this->props['scrollable'] ?? false,
|
|
]);
|
|
}
|
|
} |