unioil-cms-fe/app/Livewire/Components/Table.php

73 lines
1.8 KiB
PHP

<?php
namespace App\Livewire\Components;
use Livewire\Component;
class Table extends Component
{
public $columns = [];
public $rows = [];
public $selected = [];
public $selectAll = false;
public $hasActions = false;
public $isViewPage = false;
public $search = '';
public $sortField = null; // Column to sort
public $sortDirection = 'asc'; // Default sort direction
public function mount($columns, $rows)
{
$this->columns = $columns;
$this->rows = $rows;
}
// Update rows based on search
public function updatedSearch()
{
$this->selected = [];
}
// Select all rows
public function selectAllRows()
{
if ($this->selectAll) {
$this->selected = collect($this->rows)->pluck('id')->toArray();
} else {
$this->selected = [];
}
}
// Select single row
public function selectRow($rowId)
{
if (in_array($rowId, $this->selected)) {
$this->selected = array_diff($this->selected, [$rowId]);
} else {
$this->selected[] = $rowId;
}
}
// Simplified version
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
}
public function render()
{
return view('livewire.components.table', [
'rows' => $this->rows,
]);
}
}