Formatting fixes and cleanup.
This commit is contained in:
parent
a1e14a3e60
commit
51fb71e469
41 changed files with 394 additions and 392 deletions
|
|
@ -5,197 +5,202 @@ namespace App\Http\Livewire;
|
|||
use Livewire\Component;
|
||||
use Livewire\WithPagination;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Datatable extends Component
|
||||
{
|
||||
use WithPagination, Traits\WithSort, Traits\Alert;
|
||||
use WithPagination, Traits\WithSort, Traits\Alert;
|
||||
|
||||
/**
|
||||
* Query string settings.
|
||||
*/
|
||||
protected $queryString = [
|
||||
'sort' => ['except' => 'id'],
|
||||
'dir' => ['except' => 'asc'],
|
||||
'page' => ['except' => 1],
|
||||
'trashed' => ['except' => false]
|
||||
];
|
||||
/**
|
||||
* Query string settings.
|
||||
*/
|
||||
protected $queryString = [
|
||||
'sort' => ['except' => 'id'],
|
||||
'dir' => ['except' => 'asc'],
|
||||
'page' => ['except' => 1],
|
||||
'trashed' => ['except' => false]
|
||||
];
|
||||
|
||||
/**
|
||||
* Model to get data from.
|
||||
*/
|
||||
public $model;
|
||||
/**
|
||||
* Model to get data from.
|
||||
*/
|
||||
public $model;
|
||||
|
||||
/**
|
||||
* Array of columns to show
|
||||
*/
|
||||
public array $columns;
|
||||
/**
|
||||
* Array of columns to show
|
||||
*/
|
||||
public array $columns;
|
||||
|
||||
/**
|
||||
* What colums are sortable
|
||||
*/
|
||||
public array $sort_columns;
|
||||
/**
|
||||
* What colums are sortable
|
||||
*/
|
||||
public array $sort_columns;
|
||||
|
||||
/**
|
||||
* Default sorting column
|
||||
*/
|
||||
public string $default_sort;
|
||||
/**
|
||||
* Default sorting column
|
||||
*/
|
||||
public string $default_sort;
|
||||
|
||||
/**
|
||||
* If only trashed records should be shown.
|
||||
*/
|
||||
public bool $trashed = false;
|
||||
/**
|
||||
* If only trashed records should be shown.
|
||||
*/
|
||||
public bool $trashed = false;
|
||||
|
||||
/**
|
||||
* Route for creating a record (if null, link is omitted)
|
||||
*/
|
||||
public $route_create;
|
||||
/**
|
||||
* Route for creating a record (if null, link is omitted)
|
||||
*/
|
||||
public $route_create;
|
||||
|
||||
/**
|
||||
* Route for editing a record (if null, link is omitted)
|
||||
*/
|
||||
public $route_edit;
|
||||
/**
|
||||
* Route for editing a record (if null, link is omitted)
|
||||
*/
|
||||
public $route_edit;
|
||||
|
||||
/**
|
||||
* True if delete functionality is enabled.
|
||||
*/
|
||||
public $delete_enabled;
|
||||
/**
|
||||
* True if delete functionality is enabled.
|
||||
*/
|
||||
public $delete_enabled;
|
||||
|
||||
/**
|
||||
* True if restore functionality is enabled.
|
||||
*/
|
||||
public $restore_enabled;
|
||||
/**
|
||||
* True if restore functionality is enabled.
|
||||
*/
|
||||
public $restore_enabled;
|
||||
|
||||
/**
|
||||
* How many records should be displayed on one page.
|
||||
*/
|
||||
public int $itemsPerPage = 30;
|
||||
/**
|
||||
* How many records should be displayed on one page.
|
||||
*/
|
||||
public int $itemsPerPage = 30;
|
||||
|
||||
public function mount(string $model, array $columns,
|
||||
array $sort_columns = [], $default_sort = '',
|
||||
$route_create = null, $route_edit = null, $delete_enabled = false, $restore_enabled = false)
|
||||
{
|
||||
$this->model = app()->make($model);
|
||||
$this->default_sort = $default_sort;
|
||||
$this->sort = $this->default_sort;
|
||||
$this->sort_columns = $sort_columns;
|
||||
$this->route_create = $route_create;
|
||||
$this->route_edit = $route_edit;
|
||||
$this->delete_enabled = (bool) $delete_enabled;
|
||||
$this->restore_enabled = (bool) $restore_enabled;
|
||||
public function mount(
|
||||
string $model,
|
||||
array $columns,
|
||||
array $sort_columns = [],
|
||||
$default_sort = '',
|
||||
$route_create = null,
|
||||
$route_edit = null,
|
||||
$delete_enabled = false,
|
||||
$restore_enabled = false
|
||||
) {
|
||||
$this->model = app()->make($model);
|
||||
$this->default_sort = $default_sort;
|
||||
$this->columns = $columns;
|
||||
$this->sort = $this->default_sort;
|
||||
$this->sort_columns = $sort_columns;
|
||||
$this->route_create = $route_create;
|
||||
$this->route_edit = $route_edit;
|
||||
$this->delete_enabled = (bool) $delete_enabled;
|
||||
$this->restore_enabled = (bool) $restore_enabled;
|
||||
|
||||
$this->setTrashed(false);
|
||||
$this->setTrashed(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the model name.
|
||||
*/
|
||||
public function getModelName() : string
|
||||
{
|
||||
return class_basename($this->model);
|
||||
}
|
||||
/**
|
||||
* Get the model name.
|
||||
*/
|
||||
public function getModelName(): string
|
||||
{
|
||||
return class_basename($this->model);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set trashed flag.
|
||||
*/
|
||||
public function setTrashed($value)
|
||||
{
|
||||
$this->trashed = (bool) $value;
|
||||
/**
|
||||
* Set trashed flag.
|
||||
*/
|
||||
public function setTrashed($value)
|
||||
{
|
||||
$this->trashed = (bool) $value;
|
||||
|
||||
if ($this->trashed) {
|
||||
// Add deleted_at columns.
|
||||
$this->columns['deleted_at'] = 'Deleted at';
|
||||
$this->sort_columns['deleted_at'] = 'deleted_at';
|
||||
} else {
|
||||
// Unset deleted_at columns.
|
||||
unset($this->columns['deleted_at']);
|
||||
unset($this->sort_columns['deleted_at']);
|
||||
if ($this->trashed) {
|
||||
// Add deleted_at columns.
|
||||
$this->columns['deleted_at'] = 'Deleted at';
|
||||
$this->sort_columns['deleted_at'] = 'deleted_at';
|
||||
} else {
|
||||
// Unset deleted_at columns.
|
||||
unset($this->columns['deleted_at']);
|
||||
unset($this->sort_columns['deleted_at']);
|
||||
|
||||
// if delete_at was the sorting column
|
||||
// revert back to default sorting.
|
||||
if ($this->sort == 'deleted_at') {
|
||||
$this->sort = $this->default_sort;
|
||||
}
|
||||
}
|
||||
}
|
||||
// if delete_at was the sorting column
|
||||
// revert back to default sorting.
|
||||
if ($this->sort == 'deleted_at') {
|
||||
$this->sort = $this->default_sort;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle trashed flag
|
||||
*/
|
||||
public function toggleTrashed()
|
||||
{
|
||||
$this->setTrashed(!$this->trashed);
|
||||
}
|
||||
/**
|
||||
* Toggle trashed flag
|
||||
*/
|
||||
public function toggleTrashed()
|
||||
{
|
||||
$this->setTrashed(!$this->trashed);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete an record
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
if (!$this->delete_enabled) {
|
||||
return;
|
||||
}
|
||||
/**
|
||||
* Delete an record
|
||||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
if (!$this->delete_enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
$record = $this->model->find($id);
|
||||
$record = $this->model->find($id);
|
||||
|
||||
if (!$record) {
|
||||
$this->danger('Record not found');
|
||||
return;
|
||||
}
|
||||
if (!$record) {
|
||||
$this->danger('Record not found');
|
||||
return;
|
||||
}
|
||||
|
||||
$record->delete();
|
||||
$record->delete();
|
||||
|
||||
$this->info(__($this->getModelName() . " #:id was deleted.", [ 'id' => $record->id ]));
|
||||
}
|
||||
$this->info(__($this->getModelName() . " #:id was deleted.", ['id' => $record->id]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore a delted record
|
||||
*/
|
||||
public function restore($id)
|
||||
{
|
||||
if (!$this->restore_enabled) {
|
||||
return;
|
||||
}
|
||||
/**
|
||||
* Restore a delted record
|
||||
*/
|
||||
public function restore($id)
|
||||
{
|
||||
if (!$this->restore_enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
$record = $this->model->withTrashed()->find($id);
|
||||
$record = $this->model->withTrashed()->find($id);
|
||||
|
||||
if (!$record) {
|
||||
$this->danger('Record not found');
|
||||
return;
|
||||
}
|
||||
if (!$record) {
|
||||
$this->danger('Record not found');
|
||||
return;
|
||||
}
|
||||
|
||||
$record->restore();
|
||||
$record->restore();
|
||||
|
||||
$this->info(__($this->getModelName() . " #:id was restored.", [ 'id' => $record->id ]));
|
||||
}
|
||||
$this->info(__($this->getModelName() . " #:id was restored.", ['id' => $record->id]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the datatable
|
||||
*/
|
||||
public function render()
|
||||
{
|
||||
$query = $this->model->query();
|
||||
$query = $this->model->query();
|
||||
|
||||
// Only include trashed if asked to.
|
||||
if ($this->trashed) {
|
||||
$query->onlyTrashed();
|
||||
}
|
||||
// Only include trashed if asked to.
|
||||
if ($this->trashed) {
|
||||
$query->onlyTrashed();
|
||||
}
|
||||
|
||||
// Check colums for "dot-notated" strings.
|
||||
// as we want to eager load those relationships
|
||||
foreach ($this->columns as $col => $_) {
|
||||
$p = strrpos($col, '.');
|
||||
if ($p !== false) {
|
||||
$rel = substr($col, 0, $p);
|
||||
$query->with($rel);
|
||||
}
|
||||
}
|
||||
// Check colums for "dot-notated" strings.
|
||||
// as we want to eager load those relationships
|
||||
foreach ($this->columns as $col => $_) {
|
||||
$p = strrpos($col, '.');
|
||||
if ($p !== false) {
|
||||
$rel = substr($col, 0, $p);
|
||||
$query->with($rel);
|
||||
}
|
||||
}
|
||||
|
||||
// Apply sorting.
|
||||
$this->sortApply($query);
|
||||
// Apply sorting.
|
||||
$this->sortApply($query);
|
||||
|
||||
$items = $query->paginate($this->itemsPerPage);
|
||||
$items = $query->paginate($this->itemsPerPage);
|
||||
|
||||
return view('livewire.datatable', ['items' => $items]);
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue