diff --git a/docs/datatable/query-string.md b/docs/datatable/query-string.md index f0763a643..c830ef15a 100644 --- a/docs/datatable/query-string.md +++ b/docs/datatable/query-string.md @@ -153,4 +153,55 @@ public function configure(): void { $this->setQueryStringAliasForSearch('search'); } +``` + +## Sorts + +The sorts query string is **enabled by default**, but if you ever needed to toggle it you can use the following methods: + +### setQueryStringStatusForSort + +Enable/disable the query string for sort + +```php +public function configure(): void +{ + $this->setQueryStringStatusForSort(true); + $this->setQueryStringStatusForSort(false); +} +``` + +### setQueryStringForSortEnabled + +Enable the query string for sort + +```php +public function configure(): void +{ + // Shorthand for $this->setQueryStringStatusForSort(true) + $this->setQueryStringForSortEnabled(); +} +``` + +### setQueryStringForSortDisabled + +Disable the query string for sort + +```php +public function configure(): void +{ + // Shorthand for $this->setQueryStringStatusForSort(false) + $this->setQueryStringForSortDisabled(); +} +``` + +### setQueryStringAliasForSort + +Change the Alias in the URL for the sorts, otherwise defaults to "$tablename-sorts" + +```php +public function configure(): void +{ + $this->setQueryStringAliasForSort('sorts'); +} ``` \ No newline at end of file diff --git a/src/Traits/Core/QueryStrings/HasQueryStringForSort.php b/src/Traits/Core/QueryStrings/HasQueryStringForSort.php new file mode 100644 index 000000000..06597f80c --- /dev/null +++ b/src/Traits/Core/QueryStrings/HasQueryStringForSort.php @@ -0,0 +1,76 @@ +queryStringForSortEnabled() && $this->sortingIsEnabled()) ? ['sorts' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAliasForSort()]] : []; + + } + public function setupQueryStringStatusForSort(): void + { + if (! $this->hasQueryStringStatusForSort()) { + $this->setQueryStringForSortEnabled(); + } + } + + public function hasQueryStringStatusForSort(): bool + { + return isset($this->queryStringStatusForSort); + } + + public function getQueryStringStatusForSort(): bool + { + return $this->queryStringStatusForSort ?? true; + } + + public function queryStringForSortEnabled(): bool + { + $this->setupQueryStringStatusForSort(); + + return $this->getQueryStringStatusForSort() && $this->sortingIsEnabled(); + } + + public function setQueryStringStatusForSort(bool $status): self + { + $this->queryStringStatusForSort = $status; + + return $this; + } + + public function setQueryStringForSortEnabled(): self + { + return $this->setQueryStringStatusForSort(true); + } + + public function setQueryStringForSortDisabled(): self + { + return $this->setQueryStringStatusForSort(false); + } + + public function hasQueryStringAliasForSort(): bool + { + return isset($this->queryStringAliasForSort); + } + + public function getQueryStringAliasForSort(): string + { + return $this->queryStringAliasForSort ?? $this->getQueryStringAlias().'-sorts'; + } + + public function setQueryStringAliasForSort(string $alias): self + { + $this->queryStringAliasForSort = $alias; + + return $this; + } +} diff --git a/src/Traits/WithSorting.php b/src/Traits/WithSorting.php index 78c8e9a73..d9bc7bb48 100644 --- a/src/Traits/WithSorting.php +++ b/src/Traits/WithSorting.php @@ -7,12 +7,14 @@ use Livewire\Attributes\Locked; use Rappasoft\LaravelLivewireTables\Traits\Configuration\SortingConfiguration; use Rappasoft\LaravelLivewireTables\Traits\Helpers\SortingHelpers; +use Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings\HasQueryStringForSort; trait WithSorting { use SortingConfiguration, SortingHelpers; - + use HasQueryStringForSort; + public array $sorts = []; public Collection $sortableColumns; @@ -31,16 +33,6 @@ trait WithSorting public string $defaultSortingLabelDesc = 'Z-A'; - public function queryStringWithSorting(): array - { - if ($this->queryStringIsEnabled() && $this->sortingIsEnabled()) { - return [ - 'sorts' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAlias().'-sorts'], - ]; - } - - return []; - } public function sortBy(string $columnSelectName): ?string { diff --git a/tests/Traits/Core/QueryStrings/QueryStringForSortTest.php b/tests/Traits/Core/QueryStrings/QueryStringForSortTest.php new file mode 100644 index 000000000..7db6d3eb9 --- /dev/null +++ b/tests/Traits/Core/QueryStrings/QueryStringForSortTest.php @@ -0,0 +1,110 @@ +setDataTableFingerprint('test'); + } + }; + + $mock->configure(); + $mock->boot(); + + $this->assertSame(true, $mock->getQueryStringStatusForSort()); + } + + public function test_can_disable_sort_query_string_status(): void + { + $mock = new class extends PetsTable + { + public ?array $testAttributesArray; + + public function configure(): void + { + $this->setDataTableFingerprint('test'); + $this->setQueryStringForSortDisabled(); + } + }; + + $mock->configure(); + $mock->boot(); + + $this->assertSame(false, $mock->getQueryStringStatusForSort()); + } + + public function test_can_enable_sort_query_string_status(): void + { + $mock = new class extends PetsTable + { + public ?array $testAttributesArray; + + public function configure(): void + { + $this->setDataTableFingerprint('test'); + $this->setQueryStringForSortDisabled(); + } + }; + + $mock->configure(); + $mock->boot(); + + $this->assertSame(false, $mock->getQueryStringStatusForSort()); + $mock->setQueryStringForSortEnabled(); + $this->assertSame(true, $mock->getQueryStringStatusForSort()); + + } + + public function test_can_get_default_sort_query_string_alias(): void + { + $mock = new class extends PetsTable + { + public ?array $testAttributesArray; + + public function configure(): void + { + $this->setDataTableFingerprint('test'); + } + }; + + $mock->configure(); + $mock->boot(); + + $this->assertSame('table-sorts', $mock->getQueryStringAliasForSort()); + + } + + public function test_can_change_default_sort_query_string_alias(): void + { + $mock = new class extends PetsTable + { + public ?array $testAttributesArray; + + public function configure(): void + { + $this->setDataTableFingerprint('test'); + } + }; + + $mock->configure(); + $mock->boot(); + + $this->assertSame('table-sorts', $mock->getQueryStringAliasForSort()); + $mock->setQueryStringAliasForSort('pet-sorts'); + $this->assertSame('pet-sorts', $mock->getQueryStringAliasForSort()); + $this->assertTrue($mock->hasQueryStringAliasForSort()); + } +}