Skip to content

Commit 542e797

Browse files
authored
Add QueryStringForSort (rappasoft#2018)
* Add QueryStringForSort * Adjust for SortQueryString * Tests * Tweaks to Configure Approach --------- Co-authored-by: lrljoe <[email protected]>
1 parent 351757e commit 542e797

File tree

11 files changed

+324
-48
lines changed

11 files changed

+324
-48
lines changed

docs/datatable/query-string.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,55 @@ public function configure(): void
153153
{
154154
$this->setQueryStringAliasForSearch('search');
155155
}
156+
```
157+
158+
## Sorts
159+
160+
The sorts query string is **enabled by default**, but if you ever needed to toggle it you can use the following methods:
161+
162+
### setQueryStringStatusForSort
163+
164+
Enable/disable the query string for sort
165+
166+
```php
167+
public function configure(): void
168+
{
169+
$this->setQueryStringStatusForSort(true);
170+
$this->setQueryStringStatusForSort(false);
171+
}
172+
```
173+
174+
### setQueryStringForSortEnabled
175+
176+
Enable the query string for sort
177+
178+
```php
179+
public function configure(): void
180+
{
181+
// Shorthand for $this->setQueryStringStatusForSort(true)
182+
$this->setQueryStringForSortEnabled();
183+
}
184+
```
185+
186+
### setQueryStringForSortDisabled
187+
188+
Disable the query string for sort
189+
190+
```php
191+
public function configure(): void
192+
{
193+
// Shorthand for $this->setQueryStringStatusForSort(false)
194+
$this->setQueryStringForSortDisabled();
195+
}
196+
```
197+
198+
### setQueryStringAliasForSort
199+
200+
Change the Alias in the URL for the sorts, otherwise defaults to "$tablename-sorts"
201+
202+
```php
203+
public function configure(): void
204+
{
205+
$this->setQueryStringAliasForSort('sorts');
206+
}
156207
```

src/Traits/ComponentUtilities.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ trait ComponentUtilities
4848

4949
protected bool $useComputedProperties = true;
5050

51+
protected bool $hasRunConfigure = false;
52+
5153
/**
5254
* Set any configuration options
5355
*/
@@ -71,16 +73,7 @@ public function mountComponentUtilities(): void
7173
*/
7274
public function bootedComponentUtilities(): void
7375
{
74-
// Fire Lifecycle Hooks for configuring
75-
$this->callHook('configuring');
76-
$this->callTraitHook('configuring');
77-
78-
// Call the configure() method
79-
$this->configure();
80-
81-
// Fire Lifecycle Hooks for configured
82-
$this->callHook('configured');
83-
$this->callTraitHook('configured');
76+
$this->runCoreConfiguration();
8477

8578
// Make sure a primary key is set
8679
if (! $this->hasPrimaryKey()) {
@@ -89,6 +82,25 @@ public function bootedComponentUtilities(): void
8982

9083
}
9184

85+
protected function runCoreConfiguration(): void
86+
{
87+
if (! $this->hasRunConfigure) {
88+
// Fire Lifecycle Hooks for configuring
89+
$this->callHook('configuring');
90+
$this->callTraitHook('configuring');
91+
92+
// Call the configure() method
93+
$this->configure();
94+
95+
// Fire Lifecycle Hooks for configured
96+
$this->callHook('configured');
97+
$this->callTraitHook('configured');
98+
99+
$this->hasRunConfigure = true;
100+
101+
}
102+
}
103+
92104
/**
93105
* Returns a unique id for the table, used as an alias to identify one table from another session and query string to prevent conflicts
94106
*/

src/Traits/Configuration/QueryStringConfiguration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function setQueryStringAlias(string $queryStringAlias): self
1414
public function setupQueryStringStatus(): void
1515
{
1616
if (! $this->hasQueryStringStatus()) {
17-
$this->configure();
17+
$this->runCoreConfiguration();
1818
if (! $this->hasQueryStringStatus()) {
1919
$this->setQueryStringEnabled();
2020
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings;
4+
5+
use Livewire\Attributes\Locked;
6+
7+
trait HasQueryString
8+
{
9+
#[Locked]
10+
public array $queryStringConfig = [
11+
'columns' => [],
12+
'filter' => [],
13+
'search' => [],
14+
'sorts' => [],
15+
];
16+
17+
protected function getQueryStringConfig(string $type): array
18+
{
19+
return array_merge(['status' => null, 'alias' => null], ($this->queryStringConfig[$type] ?? []));
20+
}
21+
22+
protected function hasQueryStringConfigStatus(string $type): bool
23+
{
24+
return isset($this->getQueryStringConfig($type)['status']);
25+
}
26+
27+
protected function getQueryStringConfigStatus(string $type): bool
28+
{
29+
return $this->getQueryStringConfig($type)['status'] ?? $this->getQueryStringStatus();
30+
}
31+
32+
protected function hasQueryStringConfigAlias(string $type): bool
33+
{
34+
return isset($this->getQueryStringConfig($type)['alias']);
35+
}
36+
37+
protected function getQueryStringConfigAlias(string $type): string
38+
{
39+
return $this->getQueryStringConfig($type)['alias'] ?? $this->getQueryStringAlias().'-'.$type;
40+
}
41+
42+
protected function setQueryStringConfig(string $type, array $config): self
43+
{
44+
$this->queryStringConfig[$type] = array_merge($this->getQueryStringConfig($type), $config);
45+
46+
return $this;
47+
}
48+
49+
protected function setQueryStringConfigStatus(string $type, bool $status): self
50+
{
51+
return $this->setQueryStringConfig($type, ['status' => $status]);
52+
53+
}
54+
55+
protected function setQueryStringConfigAlias(string $type, string $alias): self
56+
{
57+
return $this->setQueryStringConfig($type, ['alias' => $alias]);
58+
}
59+
}

src/Traits/Core/QueryStrings/HasQueryStringForFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ protected function queryStringHasQueryStringForFilter(): array
2020
] : [];
2121
}
2222

23-
public function setupQueryStringStatusForFilter(): void
23+
protected function setupQueryStringStatusForFilter(): void
2424
{
2525
if (! $this->hasQueryStringStatusForFilter()) {
2626
$this->setQueryStringForFilterEnabled();

src/Traits/Core/QueryStrings/HasQueryStringForSearch.php

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,15 @@
22

33
namespace Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings;
44

5-
use Livewire\Attributes\Locked;
6-
75
trait HasQueryStringForSearch
86
{
9-
#[Locked]
10-
public ?bool $queryStringStatusForSearch;
11-
12-
protected ?string $queryStringAliasForSearch;
13-
147
protected function queryStringHasQueryStringForSearch(): array
158
{
169
return ($this->queryStringForSearchEnabled() && $this->searchIsEnabled()) ? ['search' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAliasForSearch()]] : [];
1710

1811
}
1912

20-
public function setupQueryStringStatusForSearch(): void
13+
protected function setupQueryStringStatusForSearch(): void
2114
{
2215
if (! $this->hasQueryStringStatusForSearch()) {
2316
$this->setQueryStringForSearchEnabled();
@@ -26,12 +19,12 @@ public function setupQueryStringStatusForSearch(): void
2619

2720
public function hasQueryStringStatusForSearch(): bool
2821
{
29-
return isset($this->queryStringStatusForSearch);
22+
return $this->hasQueryStringConfigStatus('search');
3023
}
3124

3225
public function getQueryStringStatusForSearch(): bool
3326
{
34-
return $this->queryStringStatusForSearch ?? true;
27+
return $this->getQueryStringConfigStatus('search');
3528
}
3629

3730
public function queryStringForSearchEnabled(): bool
@@ -43,39 +36,31 @@ public function queryStringForSearchEnabled(): bool
4336

4437
public function setQueryStringStatusForSearch(bool $status): self
4538
{
46-
$this->queryStringStatusForSearch = $status;
47-
48-
return $this;
39+
return $this->setQueryStringConfigStatus('search', $status);
4940
}
5041

5142
public function setQueryStringForSearchEnabled(): self
5243
{
53-
$this->setQueryStringStatusForSearch(true);
54-
55-
return $this;
44+
return $this->setQueryStringStatusForSearch(true);
5645
}
5746

5847
public function setQueryStringForSearchDisabled(): self
5948
{
60-
$this->setQueryStringStatusForSearch(false);
61-
62-
return $this;
49+
return $this->setQueryStringStatusForSearch(false);
6350
}
6451

6552
public function hasQueryStringAliasForSearch(): bool
6653
{
67-
return isset($this->queryStringAliasForSearch);
54+
return $this->hasQueryStringConfigAlias('search');
6855
}
6956

7057
public function getQueryStringAliasForSearch(): string
7158
{
72-
return $this->queryStringAliasForSearch ?? $this->getQueryStringAlias().'-search';
59+
return $this->getQueryStringConfigAlias('search');
7360
}
7461

7562
public function setQueryStringAliasForSearch(string $alias): self
7663
{
77-
$this->queryStringAliasForSearch = $alias;
78-
79-
return $this;
64+
return $this->setQueryStringConfigAlias('search', $alias);
8065
}
8166
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings;
4+
5+
trait HasQueryStringForSort
6+
{
7+
protected function queryStringHasQueryStringForSort(): array
8+
{
9+
return ($this->queryStringForSortEnabled() && $this->sortingIsEnabled()) ? ['sorts' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAliasForSort()]] : [];
10+
11+
}
12+
13+
protected function setupQueryStringStatusForSort(): void
14+
{
15+
if (! $this->hasQueryStringStatusForSort()) {
16+
$this->setQueryStringForSortEnabled();
17+
}
18+
}
19+
20+
public function hasQueryStringStatusForSort(): bool
21+
{
22+
return $this->hasQueryStringConfigStatus('sorts');
23+
}
24+
25+
public function getQueryStringStatusForSort(): bool
26+
{
27+
return $this->getQueryStringConfigStatus('sorts');
28+
}
29+
30+
public function queryStringForSortEnabled(): bool
31+
{
32+
$this->setupQueryStringStatusForSort();
33+
34+
return $this->getQueryStringStatusForSort() && $this->sortingIsEnabled();
35+
}
36+
37+
public function setQueryStringStatusForSort(bool $status): self
38+
{
39+
return $this->setQueryStringConfigStatus('sorts', $status);
40+
}
41+
42+
public function setQueryStringForSortEnabled(): self
43+
{
44+
return $this->setQueryStringStatusForSort(true);
45+
}
46+
47+
public function setQueryStringForSortDisabled(): self
48+
{
49+
return $this->setQueryStringStatusForSort(false);
50+
}
51+
52+
public function hasQueryStringAliasForSort(): bool
53+
{
54+
return $this->hasQueryStringConfigAlias('sorts');
55+
}
56+
57+
public function getQueryStringAliasForSort(): string
58+
{
59+
return $this->getQueryStringConfigAlias('sorts');
60+
}
61+
62+
public function setQueryStringAliasForSort(string $alias): self
63+
{
64+
return $this->setQueryStringConfigAlias('sorts', $alias);
65+
}
66+
}

src/Traits/HasAllTraits.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ trait HasAllTraits
1414
use ComponentUtilities,
1515
WithActions,
1616
WithData,
17+
WithQueryString,
1718
WithColumns,
1819
WithSorting,
1920
WithSearch,
@@ -28,7 +29,6 @@ trait HasAllTraits
2829
WithEvents,
2930
WithFilters,
3031
WithFooter,
31-
WithQueryString,
3232
WithRefresh,
3333
WithReordering,
3434
WithSecondaryHeader,

src/Traits/WithQueryString.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
use Livewire\Attributes\Locked;
66
use Rappasoft\LaravelLivewireTables\Traits\Configuration\QueryStringConfiguration;
7+
use Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings\HasQueryString;
78
use Rappasoft\LaravelLivewireTables\Traits\Helpers\QueryStringHelpers;
89

910
trait WithQueryString
1011
{
1112
use QueryStringConfiguration,
1213
QueryStringHelpers;
14+
use HasQueryString;
1315

1416
#[Locked]
1517
public ?bool $queryStringStatus;

0 commit comments

Comments
 (0)