diff --git a/docs/datatable/available-methods.md b/docs/datatable/available-methods.md index 13d4091b3..3cf5c303c 100644 --- a/docs/datatable/available-methods.md +++ b/docs/datatable/available-methods.md @@ -76,43 +76,7 @@ public function configure(): void ## Query String -The query string is **enabled by default**, but if you ever needed to toggle it you can use the following methods: - -### setQueryStringStatus - -Enable/disable the query string. - -```php -public function configure(): void -{ - $this->setQueryStringStatus(true); - $this->setQueryStringStatus(false); -} -``` - -### setQueryStringEnabled - -Enable the query string. - -```php -public function configure(): void -{ - // Shorthand for $this->setQueryStringStatus(true) - $this->setQueryStringEnabled(); -} -``` - -### setQueryStringDisabled - -Disable the query string. - -```php -public function configure(): void -{ - // Shorthand for $this->setQueryStringStatus(false) - $this->setQueryStringDisabled(); -} -``` +The documentation for Query String now lives: [here](./query-string) ## Relationships diff --git a/docs/datatable/query-string.md b/docs/datatable/query-string.md new file mode 100644 index 000000000..8f1e2b3c1 --- /dev/null +++ b/docs/datatable/query-string.md @@ -0,0 +1,105 @@ +--- +title: Query String +weight: 5 +--- + +The query string is **enabled by default**, but if you ever needed to toggle it you can use the following methods: + +## Global +### setQueryStringStatus + +Enable/disable the query string. + +```php +public function configure(): void +{ + $this->setQueryStringStatus(true); + $this->setQueryStringStatus(false); +} +``` + +### setQueryStringEnabled + +Enable the query string. + +```php +public function configure(): void +{ + // Shorthand for $this->setQueryStringStatus(true) + $this->setQueryStringEnabled(); +} +``` + +### setQueryStringDisabled + +Disable the query string. + +```php +public function configure(): void +{ + // Shorthand for $this->setQueryStringStatus(false) + $this->setQueryStringDisabled(); +} +``` + +### setQueryStringAlias + +Change the Alias in the URL, otherwise defaults to "$tablename" + +```php +public function configure(): void +{ + $this->setQueryStringAlias('table1'); +} +``` + +## Filters + +The filter query string is **enabled by default**, but if you ever needed to toggle it you can use the following methods: + +### setQueryStringStatusForFilter + +Enable/disable the query string for the filters + +```php +public function configure(): void +{ + $this->setQueryStringStatusForFilter(true); + $this->setQueryStringStatusForFilter(false); +} +``` + +### setQueryStringForFilterEnabled + +Enable the query string for the filters + +```php +public function configure(): void +{ + // Shorthand for $this->setQueryStringStatusForFilter(true) + $this->setQueryStringForFilterEnabled(); +} +``` + +### setQueryStringForFilterDisabled + +Disable the query string for the filters + +```php +public function configure(): void +{ + // Shorthand for $this->setQueryStringStatusForFilter(false) + $this->setQueryStringForFilterDisabled(); +} +``` + +### setQueryStringAliasForFilter + +Change the Alias in the URL for the filter, otherwise defaults to "$tablename-filters" + +```php +public function configure(): void +{ + $this->setQueryStringAliasForFilter('filtervalues'); +} +``` \ No newline at end of file diff --git a/docs/datatable/styling.md b/docs/datatable/styling.md index 9353ad00e..937a6df94 100644 --- a/docs/datatable/styling.md +++ b/docs/datatable/styling.md @@ -1,6 +1,6 @@ --- title: Styling -weight: 5 +weight: 6 --- The package offers significant opportunities to customise the look & feel of the core table, as well as other elements (which are documented in the relevant sections). diff --git a/src/Traits/Core/QueryStrings/HasQueryStringForFilter.php b/src/Traits/Core/QueryStrings/HasQueryStringForFilter.php new file mode 100644 index 000000000..8a84ab222 --- /dev/null +++ b/src/Traits/Core/QueryStrings/HasQueryStringForFilter.php @@ -0,0 +1,87 @@ +queryStringForFilterIsEnabled()) { + return [ + 'appliedFilters' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAliasForFilter()], + 'filterComponents' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAliasForFilter()], + ]; + } + + return []; + } + + public function setupQueryStringStatusForFilter(): void + { + if (! $this->hasQueryStringStatusForFilter()) { + $this->setQueryStringForFilterEnabled(); + } + } + + public function hasQueryStringStatusForFilter(): bool + { + return isset($this->queryStringStatusForFilter); + } + + public function getQueryStringStatusForFilter(): bool + { + return $this->queryStringStatusForFilter ?? true; + } + + public function queryStringForFilterIsEnabled(): bool + { + $this->setupQueryStringStatusForFilter(); + + return ($this->queryStringIsEnabled() === true || $this->getQueryStringStatusForFilter() === true) && $this->filtersAreEnabled(); + } + + public function setQueryStringStatusForFilter(bool $status): self + { + $this->queryStringStatusForFilter = $status; + + return $this; + } + + public function setQueryStringForFilterEnabled(): self + { + $this->setQueryStringStatusForFilter(true); + + return $this; + } + + public function setQueryStringForFilterDisabled(): self + { + $this->setQueryStringStatusForFilter(false); + + return $this; + } + + public function hasQueryStringAliasForFilter(): bool + { + return isset($this->queryStringAliasForFilter); + } + + public function getQueryStringAliasForFilter(): string + { + return $this->queryStringAliasForFilter ?? $this->getQueryStringAlias().'-filters'; + } + + public function setQueryStringAliasForFilter(string $alias): self + { + $this->queryStringAliasForFilter = $alias; + + return $this; + } +} diff --git a/src/Traits/WithFilters.php b/src/Traits/WithFilters.php index 3c2ff58c5..e9469ea44 100644 --- a/src/Traits/WithFilters.php +++ b/src/Traits/WithFilters.php @@ -7,12 +7,14 @@ use Livewire\Attributes\Locked; use Rappasoft\LaravelLivewireTables\Events\FilterApplied; use Rappasoft\LaravelLivewireTables\Traits\Configuration\FilterConfiguration; +use Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings\HasQueryStringForFilter; use Rappasoft\LaravelLivewireTables\Traits\Helpers\FilterHelpers; trait WithFilters { use FilterConfiguration, FilterHelpers; + use HasQueryStringForFilter; #[Locked] public bool $filtersStatus = true; @@ -47,18 +49,6 @@ public function filters(): array return []; } - protected function queryStringWithFilters(): array - { - if ($this->queryStringIsEnabled() && $this->filtersAreEnabled()) { - return [ - 'appliedFilters' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAlias().'-filters'], - 'filterComponents' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAlias().'-filters'], - ]; - } - - return []; - } - public function applyFilters(): Builder { if ($this->filtersAreEnabled() && $this->hasFilters() && $this->hasAppliedFiltersWithValues()) { diff --git a/tests/Traits/Core/QueryStrings/QueryStringForFiltersTest.php b/tests/Traits/Core/QueryStrings/QueryStringForFiltersTest.php new file mode 100644 index 000000000..9387e6cdb --- /dev/null +++ b/tests/Traits/Core/QueryStrings/QueryStringForFiltersTest.php @@ -0,0 +1,109 @@ +setDataTableFingerprint('test'); + } + }; + + $mock->configure(); + $mock->boot(); + + $this->assertSame(true, $mock->getQueryStringStatusForFilter()); + } + + public function test_can_disable_filter_query_string_status(): void + { + $mock = new class extends PetsTable + { + public ?array $testAttributesArray; + + public function configure(): void + { + $this->setDataTableFingerprint('test'); + $this->setQueryStringForFilterDisabled(); + } + }; + + $mock->configure(); + $mock->boot(); + + $this->assertSame(false, $mock->getQueryStringStatusForFilter()); + } + + public function test_can_enable_filter_query_string_status(): void + { + $mock = new class extends PetsTable + { + public ?array $testAttributesArray; + + public function configure(): void + { + $this->setDataTableFingerprint('test'); + $this->setQueryStringForFilterDisabled(); + } + }; + + $mock->configure(); + $mock->boot(); + + $this->assertSame(false, $mock->getQueryStringStatusForFilter()); + $mock->setQueryStringForFilterEnabled(); + $this->assertSame(true, $mock->getQueryStringStatusForFilter()); + + } + + public function test_can_get_default_filter_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-filters', $mock->getQueryStringAliasForFilter()); + + } + + public function test_can_change_default_filter_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-filters', $mock->getQueryStringAliasForFilter()); + $mock->setQueryStringAliasForFilter('pet-filters'); + $this->assertSame('pet-filters', $mock->getQueryStringAliasForFilter()); + } +}