add default sort in crud
This commit is contained in:
parent
35669e83d6
commit
5546bfb114
|
@ -207,9 +207,10 @@ abstract class CrudController extends AdminController
|
|||
protected function applySort(string $context, RepositoryQuery $query, Request $request)
|
||||
{
|
||||
$configuration = $this->getConfiguration();
|
||||
$defaultSort = $configuration->getDefaultSort($context);
|
||||
|
||||
$name = $request->query->get('_sort');
|
||||
$direction = strtolower($request->query->get('_sort_direction'));
|
||||
$name = $request->query->get('_sort', $defaultSort['label'] ?? null);
|
||||
$direction = strtolower($request->query->get('_sort_direction', $defaultSort['direction'] ?? 'asc'));
|
||||
|
||||
if (!in_array($direction, ['asc', 'desc'])) {
|
||||
$direction = 'asc';
|
||||
|
@ -238,6 +239,8 @@ abstract class CrudController extends AdminController
|
|||
'label' => $label,
|
||||
'direction' => $direction,
|
||||
];
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace App\Core\Crud;
|
||||
|
||||
use App\Core\Crud\Exception\CrudConfigurationException;
|
||||
|
||||
/**
|
||||
* class CrudConfiguration.
|
||||
*
|
||||
|
@ -20,6 +22,7 @@ class CrudConfiguration
|
|||
protected array $fields = [];
|
||||
protected array $maxPerPage = [];
|
||||
protected array $locales = [];
|
||||
protected array $defaultSort = [];
|
||||
protected ?string $defaultLocale = null;
|
||||
|
||||
protected static $self;
|
||||
|
@ -225,4 +228,21 @@ class CrudConfiguration
|
|||
{
|
||||
return !empty($this->locales);
|
||||
}
|
||||
|
||||
/* -- */
|
||||
|
||||
public function setDefaultSort(string $context, string $label, string $direction = 'asc'): self
|
||||
{
|
||||
$this->defaultSort[$context] = [
|
||||
'label' => $label,
|
||||
'direction' => $direction,
|
||||
];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDefaultSort(string $context)
|
||||
{
|
||||
return $this->defaultSort[$context] ?? null;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue