Skip to content

Commit

Permalink
5.2.1 (#112)
Browse files Browse the repository at this point in the history
* Added the ability to redefine the entire table positioning when reordering instead of just reversing some possibly incorrect positions
  • Loading branch information
Okipa authored Nov 14, 2022
1 parent 44b857d commit 77ba56e
Show file tree
Hide file tree
Showing 4 changed files with 336 additions and 188 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

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

## [5.2.1](https://github.com/Okipa/laravel-table/compare/5.2.0...5.2.1)

2022-11-14

* Added the ability to redefine the entire table positioning when reordering instead of just reversing some possibly incorrect positions

## [5.2.0](https://github.com/Okipa/laravel-table/compare/5.1.2...5.2.0)

2022-10-28
Expand Down
59 changes: 40 additions & 19 deletions src/Livewire/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ protected function buildTable(AbstractTableConfiguration $config): array
// Column actions
$this->tableColumnActionsArray = $table->generateColumnActionsArray();
// Reorder config
$this->reorderConfig = $table->getReorderConfig($table->getRows()->getCollection(), $this->sortDir);
$this->reorderConfig = $table->getReorderConfig($this->sortDir);

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

public function reorder(array $list): void
public function reorder(array $newPositions): void
{
[
'modelClass' => $modelClass,
'reorderAttribute' => $reorderAttribute,
'sortDir' => $sortDir,
'beforeReorderModelKeysWithPosition' => $beforeReorderModelKeysWithPosition,
'beforeReorderAllModelKeysWithPosition' => $beforeReorderAllModelKeysWithPositionRawArray,
] = $this->reorderConfig;
$beforeReorderPaginatedModelKeysWithPosition = collect($beforeReorderModelKeysWithPosition)
->sortKeys(descending: $sortDir === 'desc');
$afterReorderPaginatedModelKeysWithPosition = collect($list)
$beforeReorderAllModelKeysWithPositionCollection = collect($beforeReorderAllModelKeysWithPositionRawArray)
->sortBy(callback: 'position', descending: $sortDir === 'desc');
$afterReorderModelKeysWithPositionCollection = collect($newPositions)
->sortBy('order')
->pluck('value', 'order')
->mapWithKeys(fn ($modelKey) => [
array_search($modelKey, $beforeReorderModelKeysWithPosition, true) => $modelKey,
->map(fn (array $newPosition) => [
'modelKey' => $newPosition['value'],
'position' => $beforeReorderAllModelKeysWithPositionCollection->firstWhere(
'modelKey',
$newPosition['value']
)['position'],
]);
$beforeReorderPaginatedModelKeysWithIndex = $beforeReorderPaginatedModelKeysWithPosition->values();
$afterReorderPaginatedModelKeysWithIndex = $afterReorderPaginatedModelKeysWithPosition->values();
$reorderedPaginatedModelKeys = collect();
foreach ($beforeReorderPaginatedModelKeysWithIndex as $beforeReorderPaginatedModelKey) {
$afterReorderModelKeyIndex = $afterReorderPaginatedModelKeysWithIndex->search($beforeReorderPaginatedModelKey);
$beforeReorderModelKeyIndex = $beforeReorderPaginatedModelKeysWithIndex->get($afterReorderModelKeyIndex);
$modelKeyNewPosition = $beforeReorderPaginatedModelKeysWithPosition->search($beforeReorderModelKeyIndex);
$reorderedPaginatedModelKeys->put($modelKeyNewPosition, $beforeReorderPaginatedModelKey);
$beforeReorderModelKeysWithPositionCollection = $afterReorderModelKeysWithPositionCollection
->map(fn (array $afterReorderModelKeyWithPosition) => $beforeReorderAllModelKeysWithPositionCollection
->firstWhere('modelKey', $afterReorderModelKeyWithPosition['modelKey']))
->sortBy(callback: 'position', descending: $sortDir === 'desc');
$beforeReorderModelKeysWithIndexCollection = $beforeReorderModelKeysWithPositionCollection->pluck('modelKey');
$afterReorderModelKeysWithIndexCollection = $afterReorderModelKeysWithPositionCollection->pluck('modelKey');
$reorderedPositions = collect();
foreach ($beforeReorderAllModelKeysWithPositionCollection as $beforeReorderModelKeysWithPosition) {
$modelKey = $beforeReorderModelKeysWithPosition['modelKey'];
$indexAfterReordering = $afterReorderModelKeysWithIndexCollection->search($modelKey);
if ($indexAfterReordering === false) {
$currentPosition = $beforeReorderAllModelKeysWithPositionCollection->firstWhere(
'modelKey',
$modelKey
)['position'];
$reorderedPositions->push(['modelKey' => $modelKey, 'position' => $currentPosition]);

continue;
}
$modelKeyCurrentOneWillReplace = $beforeReorderModelKeysWithIndexCollection->get($indexAfterReordering);
$newPosition = $beforeReorderAllModelKeysWithPositionCollection->firstWhere(
'modelKey',
$modelKeyCurrentOneWillReplace
)['position'];
$reorderedPositions->push(['modelKey' => $modelKey, 'position' => $newPosition]);
}
foreach ($reorderedPaginatedModelKeys as $position => $modelKey) {
app($modelClass)->find($modelKey)->update([$reorderAttribute => $position]);
$startOrder = 1;
foreach ($reorderedPositions->sortBy('position') as $reorderedPosition) {
app($modelClass)->find($reorderedPosition['modelKey'])->update([$reorderAttribute => $startOrder++]);
}
$this->emit('laraveltable:action:feedback', __('The list has been reordered.'));
}
Expand Down
17 changes: 14 additions & 3 deletions src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,29 @@ public function getColumns(): Collection
return $this->columns;
}

public function getReorderConfig(Collection $rows, string|null $sortDir): array
public function getReorderConfig(string|null $sortDir): array
{
if (! $this->getOrderColumn()) {
return [];
}
$query = $this->model->query();
// Query
if ($this->queryClosure) {
$query->where(fn ($subQueryQuery) => ($this->queryClosure)($query));
}
// Sort
$query->orderBy($this->getOrderColumn()->getAttribute(), $sortDir);

return [
'modelClass' => $this->model::class,
'reorderAttribute' => $this->getOrderColumn()->getAttribute(),
'sortDir' => $sortDir,
'beforeReorderModelKeysWithPosition' => $rows
->pluck($this->model->getKeyName(), $this->getOrderColumn()->getAttribute())
'beforeReorderAllModelKeysWithPosition' => $query
->get()
->map(fn (Model $model) => [
'modelKey' => (string) $model->getKey(),
'position' => $model->getAttribute($this->getOrderColumn()->getAttribute()),
])
->toArray(),
];
}
Expand Down
Loading

0 comments on commit 77ba56e

Please sign in to comment.