Skip to content

Commit

Permalink
Merge pull request #308 from kinglozzer/patch-1
Browse files Browse the repository at this point in the history
FIX: Fixed move to prev/next page functionality (fixes #307)
  • Loading branch information
GuySartorelli authored May 15, 2024
2 parents 1bd74e0 + 80a4d9c commit cd41f31
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions src/GridFieldOrderableRows.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,28 +521,44 @@ public function handleMoveToPage(GridField $grid, $request)

if ($to == 'prev') {
$swap = $list->limit(1, ($page - 1) * $per - 1)->first();
$values[$swap->ID] = $swap->$field;

$order[] = $id;
$order[] = $swap->ID;

foreach ($existing as $_id => $sort) {
if ($id != $_id) {
$order[] = $_id;
$order[$swap->$field] = $id;

reset($existing);
$isMovingFirstItemOnPage = (key($existing) == $id);
$swappedItemNewSort = current($existing);
$order[$swappedItemNewSort] = $swap->ID;

// We want the item that's being swapped from the previous page to appear at the start
// of the current page, so we have to adjust the sort order of all the items between the
// start of the page and the location of the item we're moving
if (!$isMovingFirstItemOnPage) {
foreach ($existing as $_id => $sort) {
if ($id == $_id) {
break;
}
$order[$sort + 1] = $_id;
}
}
} elseif ($to == 'next') {
$swap = $list->limit(1, $page * $per)->first();
$values[$swap->ID] = $swap->$field;

foreach ($existing as $_id => $sort) {
if ($id != $_id) {
$order[] = $_id;
$order[$swap->$field] = $id;

end($existing);
$isMovingLastItemOnPage = (key($existing) == $id);
$swappedItemNewSort = current($existing);
$order[$swappedItemNewSort] = $swap->ID;

// We want the item that's being swapped from the next page to appear at the end
// of the current page, so we have to adjust the sort order of all the items between the
// end of the page and the location of the item we're moving
if (!$isMovingLastItemOnPage) {
foreach (array_reverse($existing, true) as $_id => $sort) {
if ($id == $_id) {
break;
}
$order[$sort - 1] = $_id;
}
}

$order[] = $swap->ID;
$order[] = $id;
} else {
$this->httpError(400, 'Invalid page target');
}
Expand Down

0 comments on commit cd41f31

Please sign in to comment.