Skip to content

Commit 77ba56e

Browse files
authored
5.2.1 (#112)
* Added the ability to redefine the entire table positioning when reordering instead of just reversing some possibly incorrect positions
1 parent 44b857d commit 77ba56e

File tree

4 files changed

+336
-188
lines changed

4 files changed

+336
-188
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes to this package will be documented in this file.
44

5+
## [5.2.1](https://github.com/Okipa/laravel-table/compare/5.2.0...5.2.1)
6+
7+
2022-11-14
8+
9+
* Added the ability to redefine the entire table positioning when reordering instead of just reversing some possibly incorrect positions
10+
511
## [5.2.0](https://github.com/Okipa/laravel-table/compare/5.1.2...5.2.0)
612

713
2022-10-28

src/Livewire/Table.php

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ protected function buildTable(AbstractTableConfiguration $config): array
162162
// Column actions
163163
$this->tableColumnActionsArray = $table->generateColumnActionsArray();
164164
// Reorder config
165-
$this->reorderConfig = $table->getReorderConfig($table->getRows()->getCollection(), $this->sortDir);
165+
$this->reorderConfig = $table->getReorderConfig($this->sortDir);
166166

167167
return [
168168
'columns' => $table->getColumns(),
@@ -180,33 +180,54 @@ protected function buildTable(AbstractTableConfiguration $config): array
180180
];
181181
}
182182

183-
public function reorder(array $list): void
183+
public function reorder(array $newPositions): void
184184
{
185185
[
186186
'modelClass' => $modelClass,
187187
'reorderAttribute' => $reorderAttribute,
188188
'sortDir' => $sortDir,
189-
'beforeReorderModelKeysWithPosition' => $beforeReorderModelKeysWithPosition,
189+
'beforeReorderAllModelKeysWithPosition' => $beforeReorderAllModelKeysWithPositionRawArray,
190190
] = $this->reorderConfig;
191-
$beforeReorderPaginatedModelKeysWithPosition = collect($beforeReorderModelKeysWithPosition)
192-
->sortKeys(descending: $sortDir === 'desc');
193-
$afterReorderPaginatedModelKeysWithPosition = collect($list)
191+
$beforeReorderAllModelKeysWithPositionCollection = collect($beforeReorderAllModelKeysWithPositionRawArray)
192+
->sortBy(callback: 'position', descending: $sortDir === 'desc');
193+
$afterReorderModelKeysWithPositionCollection = collect($newPositions)
194194
->sortBy('order')
195-
->pluck('value', 'order')
196-
->mapWithKeys(fn ($modelKey) => [
197-
array_search($modelKey, $beforeReorderModelKeysWithPosition, true) => $modelKey,
195+
->map(fn (array $newPosition) => [
196+
'modelKey' => $newPosition['value'],
197+
'position' => $beforeReorderAllModelKeysWithPositionCollection->firstWhere(
198+
'modelKey',
199+
$newPosition['value']
200+
)['position'],
198201
]);
199-
$beforeReorderPaginatedModelKeysWithIndex = $beforeReorderPaginatedModelKeysWithPosition->values();
200-
$afterReorderPaginatedModelKeysWithIndex = $afterReorderPaginatedModelKeysWithPosition->values();
201-
$reorderedPaginatedModelKeys = collect();
202-
foreach ($beforeReorderPaginatedModelKeysWithIndex as $beforeReorderPaginatedModelKey) {
203-
$afterReorderModelKeyIndex = $afterReorderPaginatedModelKeysWithIndex->search($beforeReorderPaginatedModelKey);
204-
$beforeReorderModelKeyIndex = $beforeReorderPaginatedModelKeysWithIndex->get($afterReorderModelKeyIndex);
205-
$modelKeyNewPosition = $beforeReorderPaginatedModelKeysWithPosition->search($beforeReorderModelKeyIndex);
206-
$reorderedPaginatedModelKeys->put($modelKeyNewPosition, $beforeReorderPaginatedModelKey);
202+
$beforeReorderModelKeysWithPositionCollection = $afterReorderModelKeysWithPositionCollection
203+
->map(fn (array $afterReorderModelKeyWithPosition) => $beforeReorderAllModelKeysWithPositionCollection
204+
->firstWhere('modelKey', $afterReorderModelKeyWithPosition['modelKey']))
205+
->sortBy(callback: 'position', descending: $sortDir === 'desc');
206+
$beforeReorderModelKeysWithIndexCollection = $beforeReorderModelKeysWithPositionCollection->pluck('modelKey');
207+
$afterReorderModelKeysWithIndexCollection = $afterReorderModelKeysWithPositionCollection->pluck('modelKey');
208+
$reorderedPositions = collect();
209+
foreach ($beforeReorderAllModelKeysWithPositionCollection as $beforeReorderModelKeysWithPosition) {
210+
$modelKey = $beforeReorderModelKeysWithPosition['modelKey'];
211+
$indexAfterReordering = $afterReorderModelKeysWithIndexCollection->search($modelKey);
212+
if ($indexAfterReordering === false) {
213+
$currentPosition = $beforeReorderAllModelKeysWithPositionCollection->firstWhere(
214+
'modelKey',
215+
$modelKey
216+
)['position'];
217+
$reorderedPositions->push(['modelKey' => $modelKey, 'position' => $currentPosition]);
218+
219+
continue;
220+
}
221+
$modelKeyCurrentOneWillReplace = $beforeReorderModelKeysWithIndexCollection->get($indexAfterReordering);
222+
$newPosition = $beforeReorderAllModelKeysWithPositionCollection->firstWhere(
223+
'modelKey',
224+
$modelKeyCurrentOneWillReplace
225+
)['position'];
226+
$reorderedPositions->push(['modelKey' => $modelKey, 'position' => $newPosition]);
207227
}
208-
foreach ($reorderedPaginatedModelKeys as $position => $modelKey) {
209-
app($modelClass)->find($modelKey)->update([$reorderAttribute => $position]);
228+
$startOrder = 1;
229+
foreach ($reorderedPositions->sortBy('position') as $reorderedPosition) {
230+
app($modelClass)->find($reorderedPosition['modelKey'])->update([$reorderAttribute => $startOrder++]);
210231
}
211232
$this->emit('laraveltable:action:feedback', __('The list has been reordered.'));
212233
}

src/Table.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,29 @@ public function getColumns(): Collection
113113
return $this->columns;
114114
}
115115

116-
public function getReorderConfig(Collection $rows, string|null $sortDir): array
116+
public function getReorderConfig(string|null $sortDir): array
117117
{
118118
if (! $this->getOrderColumn()) {
119119
return [];
120120
}
121+
$query = $this->model->query();
122+
// Query
123+
if ($this->queryClosure) {
124+
$query->where(fn ($subQueryQuery) => ($this->queryClosure)($query));
125+
}
126+
// Sort
127+
$query->orderBy($this->getOrderColumn()->getAttribute(), $sortDir);
121128

122129
return [
123130
'modelClass' => $this->model::class,
124131
'reorderAttribute' => $this->getOrderColumn()->getAttribute(),
125132
'sortDir' => $sortDir,
126-
'beforeReorderModelKeysWithPosition' => $rows
127-
->pluck($this->model->getKeyName(), $this->getOrderColumn()->getAttribute())
133+
'beforeReorderAllModelKeysWithPosition' => $query
134+
->get()
135+
->map(fn (Model $model) => [
136+
'modelKey' => (string) $model->getKey(),
137+
'position' => $model->getAttribute($this->getOrderColumn()->getAttribute()),
138+
])
128139
->toArray(),
129140
];
130141
}

0 commit comments

Comments
 (0)