Skip to content

Commit

Permalink
Add QueryStringForSort
Browse files Browse the repository at this point in the history
  • Loading branch information
lrljoe authored Oct 27, 2024
1 parent 351757e commit 3271df3
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 11 deletions.
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');
}
```
76 changes: 76 additions & 0 deletions src/Traits/Core/QueryStrings/HasQueryStringForSort.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings;

use Livewire\Attributes\Locked;

trait HasQueryStringForSort
{
#[Locked]
public ?bool $queryStringStatusForSort;

protected ?string $queryStringAliasForSort;

protected function queryStringHasQueryStringForSort(): array
{
return ($this->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;
}
}
14 changes: 3 additions & 11 deletions src/Traits/WithSorting.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down
110 changes: 110 additions & 0 deletions tests/Traits/Core/QueryStrings/QueryStringForSortTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Tests\Traits\Core\QueryStrings;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Depends;
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\PetsTable;
use Rappasoft\LaravelLivewireTables\Tests\TestCase;

final class QueryStringForSortTest extends TestCase
{
public function test_can_get_default_sort_query_string_status(): void
{
$mock = new class extends PetsTable
{
public ?array $testAttributesArray;

public function configure(): void
{
$this->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());
}
}

0 comments on commit 3271df3

Please sign in to comment.