Cache the last visited page in the list widget (#3432)

Stores the last visited page of list widgets in the session to restore to on next page load. Does not apply when filters / searches are applied. Credit to @tobias-kuendig
This commit is contained in:
Tobias Kündig 2018-03-08 16:52:21 +00:00 committed by Luke Towers
parent 13596c8629
commit 55d49cbf2b

View File

@ -263,6 +263,9 @@ class Lists extends WidgetBase
if ($this->showPagination) {
$this->vars['pageCurrent'] = $this->records->currentPage();
// Store the currently visited page number in the session so the same
// data can be displayed when the user returns to this list.
$this->putSession('lastVisitedPage', $this->vars['pageCurrent']);
if ($this->showPageNumbers) {
$this->vars['recordTotal'] = $this->records->total();
$this->vars['pageLast'] = $this->records->lastPage();
@ -540,8 +543,13 @@ class Lists extends WidgetBase
$records = $model->getNested();
}
elseif ($this->showPagination) {
$method = $this->showPageNumbers ? 'paginate' : 'simplePaginate';
$records = $model->{$method}($this->recordsPerPage, $this->currentPageNumber);
$method = $this->showPageNumbers ? 'paginate' : 'simplePaginate';
$currentPageNumber = $this->currentPageNumber;
if (!$currentPageNumber && empty($this->searchTerm)) {
// Restore the last visited page from the session if available.
$currentPageNumber = $this->getSession('lastVisitedPage');
}
$records = $model->{$method}($this->recordsPerPage, $currentPageNumber);
}
else {
$records = $model->get();