Skip to content

Commit 124bac1

Browse files
authored
Add customisation for QueryString specific to Filters (rappasoft#2011)
* Add customisation for QueryString specific to Filters * Move all QueryString Filter Code to HasQueryStringForFilter --------- Co-authored-by: lrljoe <[email protected]>
1 parent 2cd0344 commit 124bac1

File tree

6 files changed

+305
-50
lines changed

6 files changed

+305
-50
lines changed

docs/datatable/available-methods.md

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -76,43 +76,7 @@ public function configure(): void
7676

7777
## Query String
7878

79-
The query string is **enabled by default**, but if you ever needed to toggle it you can use the following methods:
80-
81-
### setQueryStringStatus
82-
83-
Enable/disable the query string.
84-
85-
```php
86-
public function configure(): void
87-
{
88-
$this->setQueryStringStatus(true);
89-
$this->setQueryStringStatus(false);
90-
}
91-
```
92-
93-
### setQueryStringEnabled
94-
95-
Enable the query string.
96-
97-
```php
98-
public function configure(): void
99-
{
100-
// Shorthand for $this->setQueryStringStatus(true)
101-
$this->setQueryStringEnabled();
102-
}
103-
```
104-
105-
### setQueryStringDisabled
106-
107-
Disable the query string.
108-
109-
```php
110-
public function configure(): void
111-
{
112-
// Shorthand for $this->setQueryStringStatus(false)
113-
$this->setQueryStringDisabled();
114-
}
115-
```
79+
The documentation for Query String now lives: [here](./query-string)
11680

11781
## Relationships
11882

docs/datatable/query-string.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
---
2+
title: Query String
3+
weight: 5
4+
---
5+
6+
The query string is **enabled by default**, but if you ever needed to toggle it you can use the following methods:
7+
8+
## Global
9+
### setQueryStringStatus
10+
11+
Enable/disable the query string.
12+
13+
```php
14+
public function configure(): void
15+
{
16+
$this->setQueryStringStatus(true);
17+
$this->setQueryStringStatus(false);
18+
}
19+
```
20+
21+
### setQueryStringEnabled
22+
23+
Enable the query string.
24+
25+
```php
26+
public function configure(): void
27+
{
28+
// Shorthand for $this->setQueryStringStatus(true)
29+
$this->setQueryStringEnabled();
30+
}
31+
```
32+
33+
### setQueryStringDisabled
34+
35+
Disable the query string.
36+
37+
```php
38+
public function configure(): void
39+
{
40+
// Shorthand for $this->setQueryStringStatus(false)
41+
$this->setQueryStringDisabled();
42+
}
43+
```
44+
45+
### setQueryStringAlias
46+
47+
Change the Alias in the URL, otherwise defaults to "$tablename"
48+
49+
```php
50+
public function configure(): void
51+
{
52+
$this->setQueryStringAlias('table1');
53+
}
54+
```
55+
56+
## Filters
57+
58+
The filter query string is **enabled by default**, but if you ever needed to toggle it you can use the following methods:
59+
60+
### setQueryStringStatusForFilter
61+
62+
Enable/disable the query string for the filters
63+
64+
```php
65+
public function configure(): void
66+
{
67+
$this->setQueryStringStatusForFilter(true);
68+
$this->setQueryStringStatusForFilter(false);
69+
}
70+
```
71+
72+
### setQueryStringForFilterEnabled
73+
74+
Enable the query string for the filters
75+
76+
```php
77+
public function configure(): void
78+
{
79+
// Shorthand for $this->setQueryStringStatusForFilter(true)
80+
$this->setQueryStringForFilterEnabled();
81+
}
82+
```
83+
84+
### setQueryStringForFilterDisabled
85+
86+
Disable the query string for the filters
87+
88+
```php
89+
public function configure(): void
90+
{
91+
// Shorthand for $this->setQueryStringStatusForFilter(false)
92+
$this->setQueryStringForFilterDisabled();
93+
}
94+
```
95+
96+
### setQueryStringAliasForFilter
97+
98+
Change the Alias in the URL for the filter, otherwise defaults to "$tablename-filters"
99+
100+
```php
101+
public function configure(): void
102+
{
103+
$this->setQueryStringAliasForFilter('filtervalues');
104+
}
105+
```

docs/datatable/styling.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Styling
3-
weight: 5
3+
weight: 6
44
---
55

66
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).
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings;
4+
5+
use Livewire\Attributes\Locked;
6+
7+
trait HasQueryStringForFilter
8+
{
9+
#[Locked]
10+
public ?bool $queryStringStatusForFilter;
11+
12+
protected ?string $queryStringAliasForFilter;
13+
14+
protected function queryStringHasQueryStringForFilter(): array
15+
{
16+
if ($this->queryStringForFilterIsEnabled()) {
17+
return [
18+
'appliedFilters' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAliasForFilter()],
19+
'filterComponents' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAliasForFilter()],
20+
];
21+
}
22+
23+
return [];
24+
}
25+
26+
public function setupQueryStringStatusForFilter(): void
27+
{
28+
if (! $this->hasQueryStringStatusForFilter()) {
29+
$this->setQueryStringForFilterEnabled();
30+
}
31+
}
32+
33+
public function hasQueryStringStatusForFilter(): bool
34+
{
35+
return isset($this->queryStringStatusForFilter);
36+
}
37+
38+
public function getQueryStringStatusForFilter(): bool
39+
{
40+
return $this->queryStringStatusForFilter ?? true;
41+
}
42+
43+
public function queryStringForFilterIsEnabled(): bool
44+
{
45+
$this->setupQueryStringStatusForFilter();
46+
47+
return ($this->queryStringIsEnabled() === true || $this->getQueryStringStatusForFilter() === true) && $this->filtersAreEnabled();
48+
}
49+
50+
public function setQueryStringStatusForFilter(bool $status): self
51+
{
52+
$this->queryStringStatusForFilter = $status;
53+
54+
return $this;
55+
}
56+
57+
public function setQueryStringForFilterEnabled(): self
58+
{
59+
$this->setQueryStringStatusForFilter(true);
60+
61+
return $this;
62+
}
63+
64+
public function setQueryStringForFilterDisabled(): self
65+
{
66+
$this->setQueryStringStatusForFilter(false);
67+
68+
return $this;
69+
}
70+
71+
public function hasQueryStringAliasForFilter(): bool
72+
{
73+
return isset($this->queryStringAliasForFilter);
74+
}
75+
76+
public function getQueryStringAliasForFilter(): string
77+
{
78+
return $this->queryStringAliasForFilter ?? $this->getQueryStringAlias().'-filters';
79+
}
80+
81+
public function setQueryStringAliasForFilter(string $alias): self
82+
{
83+
$this->queryStringAliasForFilter = $alias;
84+
85+
return $this;
86+
}
87+
}

src/Traits/WithFilters.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
use Livewire\Attributes\Locked;
88
use Rappasoft\LaravelLivewireTables\Events\FilterApplied;
99
use Rappasoft\LaravelLivewireTables\Traits\Configuration\FilterConfiguration;
10+
use Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings\HasQueryStringForFilter;
1011
use Rappasoft\LaravelLivewireTables\Traits\Helpers\FilterHelpers;
1112

1213
trait WithFilters
1314
{
1415
use FilterConfiguration,
1516
FilterHelpers;
17+
use HasQueryStringForFilter;
1618

1719
#[Locked]
1820
public bool $filtersStatus = true;
@@ -47,18 +49,6 @@ public function filters(): array
4749
return [];
4850
}
4951

50-
protected function queryStringWithFilters(): array
51-
{
52-
if ($this->queryStringIsEnabled() && $this->filtersAreEnabled()) {
53-
return [
54-
'appliedFilters' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAlias().'-filters'],
55-
'filterComponents' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAlias().'-filters'],
56-
];
57-
}
58-
59-
return [];
60-
}
61-
6252
public function applyFilters(): Builder
6353
{
6454
if ($this->filtersAreEnabled() && $this->hasFilters() && $this->hasAppliedFiltersWithValues()) {
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Tests\Traits\Core\QueryStrings;
4+
5+
use PHPUnit\Framework\Attributes\DataProvider;
6+
use PHPUnit\Framework\Attributes\Depends;
7+
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\PetsTable;
8+
use Rappasoft\LaravelLivewireTables\Tests\TestCase;
9+
10+
final class QueryStringForFiltersTest extends TestCase
11+
{
12+
public function test_can_get_default_filter_query_string_status(): void
13+
{
14+
$mock = new class extends PetsTable
15+
{
16+
public ?array $testAttributesArray;
17+
18+
public function configure(): void
19+
{
20+
$this->setDataTableFingerprint('test');
21+
}
22+
};
23+
24+
$mock->configure();
25+
$mock->boot();
26+
27+
$this->assertSame(true, $mock->getQueryStringStatusForFilter());
28+
}
29+
30+
public function test_can_disable_filter_query_string_status(): void
31+
{
32+
$mock = new class extends PetsTable
33+
{
34+
public ?array $testAttributesArray;
35+
36+
public function configure(): void
37+
{
38+
$this->setDataTableFingerprint('test');
39+
$this->setQueryStringForFilterDisabled();
40+
}
41+
};
42+
43+
$mock->configure();
44+
$mock->boot();
45+
46+
$this->assertSame(false, $mock->getQueryStringStatusForFilter());
47+
}
48+
49+
public function test_can_enable_filter_query_string_status(): void
50+
{
51+
$mock = new class extends PetsTable
52+
{
53+
public ?array $testAttributesArray;
54+
55+
public function configure(): void
56+
{
57+
$this->setDataTableFingerprint('test');
58+
$this->setQueryStringForFilterDisabled();
59+
}
60+
};
61+
62+
$mock->configure();
63+
$mock->boot();
64+
65+
$this->assertSame(false, $mock->getQueryStringStatusForFilter());
66+
$mock->setQueryStringForFilterEnabled();
67+
$this->assertSame(true, $mock->getQueryStringStatusForFilter());
68+
69+
}
70+
71+
public function test_can_get_default_filter_query_string_alias(): void
72+
{
73+
$mock = new class extends PetsTable
74+
{
75+
public ?array $testAttributesArray;
76+
77+
public function configure(): void
78+
{
79+
$this->setDataTableFingerprint('test');
80+
}
81+
};
82+
83+
$mock->configure();
84+
$mock->boot();
85+
86+
$this->assertSame('table-filters', $mock->getQueryStringAliasForFilter());
87+
88+
}
89+
90+
public function test_can_change_default_filter_query_string_alias(): void
91+
{
92+
$mock = new class extends PetsTable
93+
{
94+
public ?array $testAttributesArray;
95+
96+
public function configure(): void
97+
{
98+
$this->setDataTableFingerprint('test');
99+
}
100+
};
101+
102+
$mock->configure();
103+
$mock->boot();
104+
105+
$this->assertSame('table-filters', $mock->getQueryStringAliasForFilter());
106+
$mock->setQueryStringAliasForFilter('pet-filters');
107+
$this->assertSame('pet-filters', $mock->getQueryStringAliasForFilter());
108+
}
109+
}

0 commit comments

Comments
 (0)