Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add QueryStringForSort #2018

Merged
merged 9 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions docs/datatable/query-string.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
```
32 changes: 22 additions & 10 deletions src/Traits/ComponentUtilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ trait ComponentUtilities

protected bool $useComputedProperties = true;

protected bool $hasRunConfigure = false;

/**
* Set any configuration options
*/
Expand All @@ -71,16 +73,7 @@ public function mountComponentUtilities(): void
*/
public function bootedComponentUtilities(): void
{
// Fire Lifecycle Hooks for configuring
$this->callHook('configuring');
$this->callTraitHook('configuring');

// Call the configure() method
$this->configure();

// Fire Lifecycle Hooks for configured
$this->callHook('configured');
$this->callTraitHook('configured');
$this->runCoreConfiguration();

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

}

protected function runCoreConfiguration(): void
{
if (! $this->hasRunConfigure) {
// Fire Lifecycle Hooks for configuring
$this->callHook('configuring');
$this->callTraitHook('configuring');

// Call the configure() method
$this->configure();

// Fire Lifecycle Hooks for configured
$this->callHook('configured');
$this->callTraitHook('configured');

$this->hasRunConfigure = true;

}
}

/**
* Returns a unique id for the table, used as an alias to identify one table from another session and query string to prevent conflicts
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/Configuration/QueryStringConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function setQueryStringAlias(string $queryStringAlias): self
public function setupQueryStringStatus(): void
{
if (! $this->hasQueryStringStatus()) {
$this->configure();
$this->runCoreConfiguration();
if (! $this->hasQueryStringStatus()) {
$this->setQueryStringEnabled();
}
Expand Down
59 changes: 59 additions & 0 deletions src/Traits/Core/QueryStrings/HasQueryString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings;

use Livewire\Attributes\Locked;

trait HasQueryString
{
#[Locked]
public array $queryStringConfig = [
'columns' => [],
'filter' => [],
'search' => [],
'sorts' => [],
];

protected function getQueryStringConfig(string $type): array
{
return array_merge(['status' => null, 'alias' => null], ($this->queryStringConfig[$type] ?? []));
}

protected function hasQueryStringConfigStatus(string $type): bool
{
return isset($this->getQueryStringConfig($type)['status']);
}

protected function getQueryStringConfigStatus(string $type): bool
{
return $this->getQueryStringConfig($type)['status'] ?? $this->getQueryStringStatus();
}

protected function hasQueryStringConfigAlias(string $type): bool
{
return isset($this->getQueryStringConfig($type)['alias']);
}

protected function getQueryStringConfigAlias(string $type): string
{
return $this->getQueryStringConfig($type)['alias'] ?? $this->getQueryStringAlias().'-'.$type;
}

protected function setQueryStringConfig(string $type, array $config): self
{
$this->queryStringConfig[$type] = array_merge($this->getQueryStringConfig($type), $config);

return $this;
}

protected function setQueryStringConfigStatus(string $type, bool $status): self
{
return $this->setQueryStringConfig($type, ['status' => $status]);

}

protected function setQueryStringConfigAlias(string $type, string $alias): self
{
return $this->setQueryStringConfig($type, ['alias' => $alias]);
}
}
2 changes: 1 addition & 1 deletion src/Traits/Core/QueryStrings/HasQueryStringForFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected function queryStringHasQueryStringForFilter(): array
] : [];
}

public function setupQueryStringStatusForFilter(): void
protected function setupQueryStringStatusForFilter(): void
{
if (! $this->hasQueryStringStatusForFilter()) {
$this->setQueryStringForFilterEnabled();
Expand Down
33 changes: 9 additions & 24 deletions src/Traits/Core/QueryStrings/HasQueryStringForSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,15 @@

namespace Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings;

use Livewire\Attributes\Locked;

trait HasQueryStringForSearch
{
#[Locked]
public ?bool $queryStringStatusForSearch;

protected ?string $queryStringAliasForSearch;

protected function queryStringHasQueryStringForSearch(): array
{
return ($this->queryStringForSearchEnabled() && $this->searchIsEnabled()) ? ['search' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAliasForSearch()]] : [];

}

public function setupQueryStringStatusForSearch(): void
protected function setupQueryStringStatusForSearch(): void
{
if (! $this->hasQueryStringStatusForSearch()) {
$this->setQueryStringForSearchEnabled();
Expand All @@ -26,12 +19,12 @@ public function setupQueryStringStatusForSearch(): void

public function hasQueryStringStatusForSearch(): bool
{
return isset($this->queryStringStatusForSearch);
return $this->hasQueryStringConfigStatus('search');
}

public function getQueryStringStatusForSearch(): bool
{
return $this->queryStringStatusForSearch ?? true;
return $this->getQueryStringConfigStatus('search');
}

public function queryStringForSearchEnabled(): bool
Expand All @@ -43,39 +36,31 @@ public function queryStringForSearchEnabled(): bool

public function setQueryStringStatusForSearch(bool $status): self
{
$this->queryStringStatusForSearch = $status;

return $this;
return $this->setQueryStringConfigStatus('search', $status);
}

public function setQueryStringForSearchEnabled(): self
{
$this->setQueryStringStatusForSearch(true);

return $this;
return $this->setQueryStringStatusForSearch(true);
}

public function setQueryStringForSearchDisabled(): self
{
$this->setQueryStringStatusForSearch(false);

return $this;
return $this->setQueryStringStatusForSearch(false);
}

public function hasQueryStringAliasForSearch(): bool
{
return isset($this->queryStringAliasForSearch);
return $this->hasQueryStringConfigAlias('search');
}

public function getQueryStringAliasForSearch(): string
{
return $this->queryStringAliasForSearch ?? $this->getQueryStringAlias().'-search';
return $this->getQueryStringConfigAlias('search');
}

public function setQueryStringAliasForSearch(string $alias): self
{
$this->queryStringAliasForSearch = $alias;

return $this;
return $this->setQueryStringConfigAlias('search', $alias);
}
}
66 changes: 66 additions & 0 deletions src/Traits/Core/QueryStrings/HasQueryStringForSort.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings;

trait HasQueryStringForSort
{
protected function queryStringHasQueryStringForSort(): array
{
return ($this->queryStringForSortEnabled() && $this->sortingIsEnabled()) ? ['sorts' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAliasForSort()]] : [];

}

protected function setupQueryStringStatusForSort(): void
{
if (! $this->hasQueryStringStatusForSort()) {
$this->setQueryStringForSortEnabled();
}
}

public function hasQueryStringStatusForSort(): bool
{
return $this->hasQueryStringConfigStatus('sorts');
}

public function getQueryStringStatusForSort(): bool
{
return $this->getQueryStringConfigStatus('sorts');
}

public function queryStringForSortEnabled(): bool
{
$this->setupQueryStringStatusForSort();

return $this->getQueryStringStatusForSort() && $this->sortingIsEnabled();
}

public function setQueryStringStatusForSort(bool $status): self
{
return $this->setQueryStringConfigStatus('sorts', $status);
}

public function setQueryStringForSortEnabled(): self
{
return $this->setQueryStringStatusForSort(true);
}

public function setQueryStringForSortDisabled(): self
{
return $this->setQueryStringStatusForSort(false);
}

public function hasQueryStringAliasForSort(): bool
{
return $this->hasQueryStringConfigAlias('sorts');
}

public function getQueryStringAliasForSort(): string
{
return $this->getQueryStringConfigAlias('sorts');
}

public function setQueryStringAliasForSort(string $alias): self
{
return $this->setQueryStringConfigAlias('sorts', $alias);
}
}
2 changes: 1 addition & 1 deletion src/Traits/HasAllTraits.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ trait HasAllTraits
use ComponentUtilities,
WithActions,
WithData,
WithQueryString,
WithColumns,
WithSorting,
WithSearch,
Expand All @@ -28,7 +29,6 @@ trait HasAllTraits
WithEvents,
WithFilters,
WithFooter,
WithQueryString,
WithRefresh,
WithReordering,
WithSecondaryHeader,
Expand Down
2 changes: 2 additions & 0 deletions src/Traits/WithQueryString.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

use Livewire\Attributes\Locked;
use Rappasoft\LaravelLivewireTables\Traits\Configuration\QueryStringConfiguration;
use Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings\HasQueryString;
use Rappasoft\LaravelLivewireTables\Traits\Helpers\QueryStringHelpers;

trait WithQueryString
{
use QueryStringConfiguration,
QueryStringHelpers;
use HasQueryString;

#[Locked]
public ?bool $queryStringStatus;
Expand Down
Loading