@@ -162,7 +162,7 @@ protected function buildTable(AbstractTableConfiguration $config): array
162
162
// Column actions
163
163
$ this ->tableColumnActionsArray = $ table ->generateColumnActionsArray ();
164
164
// Reorder config
165
- $ this ->reorderConfig = $ table ->getReorderConfig ($ table -> getRows ()-> getCollection (), $ this ->sortDir );
165
+ $ this ->reorderConfig = $ table ->getReorderConfig ($ this ->sortDir );
166
166
167
167
return [
168
168
'columns ' => $ table ->getColumns (),
@@ -180,33 +180,54 @@ protected function buildTable(AbstractTableConfiguration $config): array
180
180
];
181
181
}
182
182
183
- public function reorder (array $ list ): void
183
+ public function reorder (array $ newPositions ): void
184
184
{
185
185
[
186
186
'modelClass ' => $ modelClass ,
187
187
'reorderAttribute ' => $ reorderAttribute ,
188
188
'sortDir ' => $ sortDir ,
189
- 'beforeReorderModelKeysWithPosition ' => $ beforeReorderModelKeysWithPosition ,
189
+ 'beforeReorderAllModelKeysWithPosition ' => $ beforeReorderAllModelKeysWithPositionRawArray ,
190
190
] = $ 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 )
194
194
->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 ' ],
198
201
]);
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 ]);
207
227
}
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 ++]);
210
231
}
211
232
$ this ->emit ('laraveltable:action:feedback ' , __ ('The list has been reordered. ' ));
212
233
}
0 commit comments