Repository navigation
Expand file tree
/
Copy pathSortableTable.php
More file actions
138 lines (111 loc) · 3.34 KB
/
Copy pathSortableTable.php
File metadata and controls
138 lines (111 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
declare(strict_types=1);
namespace NyonCode\WireSortable;
use NyonCode\WireTable\Table;
/**
* Extended Table with reorderable rows and columns.
*
* Row reorder: toggle mode with drag & drop, saves to DB order column.
* Column reorder: per-user column ordering, persisted to DB.
*
* Usage:
* SortableTable::make()
* ->model(Task::class)
* ->reorderable('sort_order')
* ->columnReorderable()
* ->columns([...])
*/
class SortableTable extends Table
{
protected bool $reorderable = false;
protected bool $alwaysReorderable = false;
protected ?string $orderColumn = null;
protected bool $paginatedWhileReordering = false;
protected bool $columnReorderable = false;
/**
* Enable drag & drop row reordering.
*
* @param string|null $orderColumn DB column storing the sort position
* @param bool $condition Conditionally enable
*/
public function reorderable(?string $orderColumn = null, bool $condition = true): static
{
$this->reorderable = $condition;
if ($orderColumn !== null) {
$this->orderColumn = $orderColumn;
}
return $this;
}
public function isReorderable(): bool
{
return $this->reorderable;
}
/**
* Always keep row reordering active (no toggle button); implies reorderable.
*
* @param string|null $orderColumn DB column storing the sort position
*/
public function alwaysReorderable(?string $orderColumn = null): static
{
$this->reorderable = true;
$this->alwaysReorderable = true;
if ($orderColumn !== null) {
$this->orderColumn = $orderColumn;
}
return $this;
}
public function isAlwaysReorderable(): bool
{
return $this->alwaysReorderable;
}
public function getOrderColumn(): string
{
if ($this->orderColumn !== null) {
return $this->orderColumn;
}
// Read the package config when a container is available; fall back to the
// documented default so the table works without a booted app (unit tests).
if (app()->bound('config')) {
return config('wire-sortable.order_column', 'sort_order');
}
return 'sort_order';
}
/**
* Keep pagination enabled while in row reorder mode.
*/
public function paginatedWhileReordering(bool $enabled = true): static
{
$this->paginatedWhileReordering = $enabled;
return $this;
}
public function isPaginatedWhileReordering(): bool
{
return $this->paginatedWhileReordering;
}
/**
* Enable user-specific column reordering.
*
* Column order is persisted per user + model in the DB.
*/
public function columnReorderable(bool $enabled = true): static
{
$this->columnReorderable = $enabled;
return $this;
}
public function isColumnReorderable(): bool
{
return $this->columnReorderable;
}
/**
* Get the Blade view name for this table.
*
* Called by WithTable via method_exists on the table instance.
*/
public function getViewName(): string
{
if ($this->isReorderable() || $this->isColumnReorderable()) {
return 'wire-sortable::tables.index';
}
return 'wire-table::tables.index';
}
}