69 lines
1.8 KiB
PHP
69 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Components;
|
|
|
|
use Livewire\Component;
|
|
|
|
class Table extends Component
|
|
{
|
|
public $columns = [];
|
|
public $rows = [];
|
|
public $search = '';
|
|
public $sortField = '';
|
|
public $sortDirection = 'asc';
|
|
public $currentPage = 1;
|
|
public $perPage = 10;
|
|
public $checkboxes = false;
|
|
public $showActions = false;
|
|
public $actions = [];
|
|
public $addButtonLabel = '';
|
|
public $searchable = false; // Add this property
|
|
|
|
// Sort the table by the given field
|
|
public function sortBy($field)
|
|
{
|
|
if ($this->sortField === $field) {
|
|
$this->sortDirection = $this->sortDirection === 'asc' ? 'desc' : 'asc';
|
|
} else {
|
|
$this->sortField = $field;
|
|
$this->sortDirection = 'asc';
|
|
}
|
|
}
|
|
|
|
// Calculate total pages for pagination
|
|
public function calculateTotalPages()
|
|
{
|
|
return ceil(count($this->rows) / $this->perPage);
|
|
}
|
|
|
|
// Get the paginated rows for the current page
|
|
public function getPaginatedRowsProperty()
|
|
{
|
|
$sorted = collect($this->rows);
|
|
|
|
if ($this->sortField) {
|
|
$sorted = $this->sortDirection === 'asc'
|
|
? $sorted->sortBy($this->sortField)
|
|
: $sorted->sortByDesc($this->sortField);
|
|
}
|
|
|
|
return $sorted
|
|
->slice(($this->currentPage - 1) * $this->perPage, $this->perPage)
|
|
->values();
|
|
}
|
|
|
|
|
|
// Set the current page for pagination
|
|
public function setPage($page)
|
|
{
|
|
$this->currentPage = max(1, min($this->calculateTotalPages(), $page));
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.components.table', [
|
|
'paginatedRows' => $this->paginatedRows, // 👈 explicitly pass computed property
|
|
]);
|
|
}
|
|
|
|
} |