Skip to content

Commit

Permalink
Make the previous and next buttons always work
Browse files Browse the repository at this point in the history
  • Loading branch information
bergice committed Jan 18, 2021
1 parent 8438e74 commit 6efef0d
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions src/Forms/GridField/GridFieldDetailForm_ItemRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\RequestHandler;
use SilverStripe\Core\Convert;
use SilverStripe\Forms\CompositeField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\HiddenField;
use SilverStripe\Forms\LiteralField;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\ORM\HasManyList;
Expand Down Expand Up @@ -564,27 +566,19 @@ public function getEditLink($id)
private function getAdjacentRecordID($offset)
{
$gridField = $this->getGridField();
/** @var DataList $list */
$list = $gridField->getManipulatedList();
$state = $gridField->getState(false);
$gridStateStr = $this->getStateManager()->getStateFromRequest($this->gridField, $this->getRequest());
if (!empty($gridStateStr)) {
$state->setValue($gridStateStr);
}
$data = $state->getData();
$paginator = $data->getData('GridFieldPaginator');
if (!$paginator) {
return false;
}

$currentPage = $paginator->getData('currentPage');
$itemsPerPage = $paginator->getData('itemsPerPage');

$limit = $itemsPerPage + 2;
$limitOffset = max(0, $itemsPerPage * ($currentPage-1) -1);

$map = $list->limit($limit, $limitOffset)->column('ID');
if ($offset > 0) {
$list = $list->where(["ID >= ?" => $this->record->ID])
->sort(['ID' => 'ASC']);
} else {
$list = $list->where(["ID <= ?" => $this->record->ID])
->sort(['ID' => 'DESC']);
}
$map = $list->limit(abs($offset)+1)->column('ID');
$index = array_search($this->record->ID, $map);
return isset($map[$index+$offset]) ? $map[$index+$offset] : false;
return isset($map[abs($offset)]) ? $map[abs($offset)] : false;
}

/**
Expand Down

0 comments on commit 6efef0d

Please sign in to comment.