add default-sorting of listings in frontend

Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann
2022-09-30 09:44:11 +02:00
parent fa826aa87e
commit e944a886b6
40 changed files with 51 additions and 26 deletions

View File

@@ -109,7 +109,7 @@ class Collection
return $this;
}
public function withPagination(array $columns): Collection
public function withPagination(array $columns, array $default_sorting = []): Collection
{
// Get only searchable columns
/*
@@ -122,7 +122,7 @@ class Collection
*/
// Prepare pagination
$this->pagination = new Pagination($columns, $this->count(), (int)Settings::Get('panel.paging'));
$this->pagination = new Pagination($columns, $this->count(), (int)Settings::Get('panel.paging'), $default_sorting);
$this->params = array_merge($this->params, $this->pagination->getApiCommandParams());
return $this;

View File

@@ -54,8 +54,9 @@ class Pagination
* @param array $fields
* @param int $total_entries
* @param int $perPage
* @param array $default_sorting array of key=sortfield,value=sortorder for default sorting
*/
public function __construct(array $fields = [], int $total_entries = 0, int $perPage = 20)
public function __construct(array $fields = [], int $total_entries = 0, int $perPage = 20, array $default_sorting = [])
{
$this->fields = $fields;
$this->entries = $total_entries;
@@ -87,8 +88,14 @@ class Pagination
$this->sortfield = $_REQUEST['sortfield'];
$this->addOrderBy($this->sortfield, $this->sortorder);
} else {
// add default ordering by given order
if (!empty($default_sorting)) {
$this->sortfield = array_key_first($default_sorting);
$this->sortorder = array_shift($default_sorting) ?? $this->sortorder;
$this->addOrderBy($this->sortfield, $this->sortorder);
}
// add default ordering by given fields
if (count($fields) > 0) {
if (count($fields) > 0 && empty($this->sortfield)) {
$orderfields = array_keys($fields);
$this->sortfield = $orderfields[0];
$this->addOrderBy($orderfields[0], $this->sortorder);