Skip to content

Commit db64da8

Browse files
committed
Sorting Bug Fixes
1 parent 3d17b6d commit db64da8

File tree

14 files changed

+374
-275
lines changed

14 files changed

+374
-275
lines changed

resources/views/components/table/th/th.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
$customSortButtonAttributes = $allThAttributes['sortButtonAttributes'];
88
$customLabelAttributes = $allThAttributes['labelAttributes'];
99
$customIconAttributes = $this->getThSortIconAttributes($column);
10-
$direction = $column->hasField() ? $this->getSort($column->getColumnSelectName()) : $this->getSort($column->getSlug()) ?? null;
10+
$direction = $column->hasField() ? $this->getSort($column->getColumnSortKey()) : $this->getSort($column->getSlug()) ?? null;
1111
@endphp
1212

1313
<th {{

resources/views/components/tools/sorting-pills.blade.php

Lines changed: 104 additions & 116 deletions
Large diffs are not rendered by default.

resources/views/components/tools/tools.blade.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
>
1313
@includeWhen(!$this->showActionsInToolbar(), 'livewire-tables::includes.toolbar.items.actions', $this->getToolbarActionAttributes())
1414

15+
@if(!(method_exists($this, 'showSortPillsSection') ? $this->showSortPillsSection() : false) && !(method_exists($this, 'showFilterPillsSection') ? $this->showFilterPillsSection() : false))
16+
<div class="">
17+
18+
</div>
19+
@endif
20+
1521
@if(method_exists($this, 'showSortPillsSection') ? $this->showSortPillsSection() : false)
1622
<x-livewire-tables::tools.sorting-pills />
1723
@endif

src/Collections/ColumnCollection.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,27 @@ public function selected() {
2323
return $this->reject(fn (Column $column) => $column->isSelectable() && ! $column->isSelected());
2424
}
2525

26+
public function sortable() {
27+
return $this->reject(fn (Column $column) => !$column->isSortable() && !$column->hasSortCallback());
28+
}
29+
30+
public function visibleSortableColumns() {
31+
return $this
32+
->visible()
33+
->selected()
34+
->sortable();
35+
}
36+
37+
public function visibleSortableColumnsKeyed(array $sortKeys = []) {
38+
return $this
39+
->visible()
40+
->selected()
41+
->sortable()
42+
->whereIn('slug', $sortKeys)
43+
->keyBy('slug');
44+
}
45+
46+
2647
/**
2748
* Undocumented function
2849
*

src/Features/Columns/Views/Column.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Column
4545
protected ?string $field = null;
4646

4747
// The table of the columns or relationship
48-
protected ?string $table = null;
48+
public ?string $table = null;
4949

5050
protected bool $html = false;
5151

@@ -81,6 +81,7 @@ public function __construct(string $title, ?string $from = null)
8181
$this->field = Str::snake($title);
8282
$this->hash = md5($this->field);
8383
}
84+
$this->setDefaultSlug();
8485
}
8586

8687
/**

src/Features/Columns/Views/Traits/HasSlug.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,20 @@ trait HasSlug
88
{
99
protected ?string $customSlug = null;
1010

11+
public ?string $slug = null;
12+
13+
public function setDefaultSlug(): self
14+
{
15+
if(!isset($this->slug))
16+
{
17+
$this->slug = Str::slug($this->hasCustomSlug() ? $this->getCustomSlug() : $this->getTitle());
18+
}
19+
return $this;
20+
}
21+
1122
public function getSlug(): string
1223
{
13-
return Str::slug($this->hasCustomSlug() ? $this->getCustomSlug() : $this->getTitle());
24+
return Str::slug($this->hasCustomSlug() ? $this->getCustomSlug() : ($this->slug ?? $this->getTitle()));
1425
}
1526

1627
public function getCustomSlug(): string

src/Features/Columns/Views/Traits/Helpers/ColumnHelpers.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,21 @@ public function getTable(): ?string
6262
public function getColumn(): ?string
6363
{
6464
if ($this->isBaseColumn()) {
65+
6566
return $this->getField();
6667
}
6768
return $this->getRelationString().'.'.$this->getField();
6869
}
70+
71+
public function getColumnForQuery(?string $baseTableName = null): string
72+
{
73+
if ($this->isBaseColumn() && isset($baseTableName)) {
74+
75+
return $baseTableName . '.' . $this->getField();
76+
}
77+
return str_replace('.', '_', $this->getRelationString()).'.'.$this->getField();
78+
79+
}
6980

7081
public function getColumnSelectName(): ?string
7182
{

src/Features/Columns/Views/Traits/IsColumn.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ trait IsColumn
4444
protected ?string $field = null;
4545

4646
// The table of the columns or relationship
47-
protected ?string $table = null;
47+
public ?string $table = null;
4848

4949
protected bool $html = false;
5050

src/Features/Columns/Views/Traits/IsSortable.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function getCustomSortingPillTitle(): ?string
7979

8080
public function hasCustomSortingPillTitle(): bool
8181
{
82-
return $this->getCustomSortingPillTitle() !== null;
82+
return isset($this->sortingPillTitle) && $this->sortingPillTitle !== null;
8383
}
8484

8585
public function hasCustomSortingPillDirections(): bool
@@ -136,6 +136,7 @@ public function getSortingPillDirectionLabel(string $direction, ?string $default
136136
*/
137137
public function getColumnSortKey(): string
138138
{
139-
return $this->isSortable() ? $this->getColumnSelectName() : $this->getSlug();
139+
140+
return ($this->getSlug() ?? $this->getColumnSelectName()) ?? 'tst';
140141
}
141142
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Features\Sorting\Concerns;
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
use Livewire\Attributes\{Computed,On};
7+
use Rappasoft\LaravelLivewireTables\Features\Columns\Views\Column;
8+
9+
trait HandlesSortingPills
10+
{
11+
/**
12+
* Undocumented function
13+
*
14+
* @return boolean
15+
*/
16+
public function getSortingPillsStatus(): bool
17+
{
18+
return $this->sortingConfig['sortingPillsStatus'] ?? true;
19+
}
20+
21+
/**
22+
* Undocumented function
23+
*
24+
* @return string
25+
*/
26+
#[Computed]
27+
public function getDefaultSortingLabelAsc(): string
28+
{
29+
return $this->sortingConfig['defaultSortingLabelAsc'] ?? 'A-Z';
30+
}
31+
32+
33+
/**
34+
* Undocumented function
35+
*
36+
* @return string
37+
*/
38+
#[Computed]
39+
public function getDefaultSortingLabelDesc(): string
40+
{
41+
return $this->sortingConfig['defaultSortingLabelDesc'] ?? 'Z-A';
42+
}
43+
44+
45+
/**
46+
* Undocumented function
47+
*
48+
* @return array<mixed>
49+
*/
50+
public function getSortsForPills(): array
51+
{
52+
$sortedCols = [];
53+
$defaultSortingLabelAsc = $this->getDefaultSortingLabelAsc();
54+
$defaultSortingLabelDesc = $this->getDefaultSortingLabelDesc();
55+
$sortKeys = array_keys($this->sorts);
56+
foreach($this->getColumns()
57+
->visibleSortableColumns()
58+
->reject(fn (Column $column) => !in_array($column->getSlug(), $sortKeys) && !in_array($column->getTitle(), $sortKeys)) as $sortedColumn)
59+
{
60+
$columnSelectName = $sortedColumn->getSlug();
61+
$direction = $this->sorts[$columnSelectName];
62+
$sortingPillTitle = $sortedColumn->getSortingPillTitle();
63+
$sortingPillDirectionLabel = $sortedColumn->getSortingPillDirectionLabel($direction, $defaultSortingLabelAsc, $defaultSortingLabelDesc);
64+
65+
$sortedCols[] = [
66+
'columnSelectName' => $columnSelectName,
67+
'direction' => $direction,
68+
'sortingPillTitle' => $sortingPillTitle,
69+
'sortingPillDirectionLabel' => $sortingPillDirectionLabel,
70+
];
71+
}
72+
return $sortedCols;
73+
}
74+
75+
76+
/**
77+
* Undocumented function
78+
*
79+
* @return boolean
80+
*/
81+
public function sortingPillsAreEnabled(): bool
82+
{
83+
return $this->getSortingPillsStatus() === true;
84+
}
85+
86+
/**
87+
* Undocumented function
88+
*
89+
* @return boolean
90+
*/
91+
public function sortingPillsAreDisabled(): bool
92+
{
93+
return $this->getSortingPillsStatus() === false;
94+
}
95+
96+
/**
97+
* Undocumented function
98+
*
99+
* @return boolean
100+
*/
101+
#[Computed]
102+
public function showSortPillsSection(): bool
103+
{
104+
return $this->sortingIsEnabled() && $this->sortingPillsAreEnabled() && $this->hasSorts();
105+
}
106+
107+
/**
108+
* Undocumented function
109+
*
110+
* @param boolean $status
111+
* @return self
112+
*/
113+
public function setSortingPillsStatus(bool $status): self
114+
{
115+
$this->sortingConfig['sortingPillsStatus'] = $status;
116+
117+
return $this;
118+
}
119+
120+
/**
121+
* Undocumented function
122+
*
123+
* @return self
124+
*/
125+
public function setSortingPillsEnabled(): self
126+
{
127+
return $this->setSortingPillsStatus(true);
128+
}
129+
130+
/**
131+
* Undocumented function
132+
*
133+
* @return self
134+
*/
135+
public function setSortingPillsDisabled(): self
136+
{
137+
return $this->setSortingPillsStatus(false);
138+
}
139+
140+
/**
141+
* Undocumented function
142+
*
143+
* @param string $asc
144+
* @param string $desc
145+
* @return self
146+
*/
147+
public function setDefaultSortingLabels(string $asc, string $desc): self
148+
{
149+
$this->sortingConfig['defaultSortingLabelAsc'] = $asc;
150+
$this->sortingConfig['defaultSortingLabelDesc'] = $desc;
151+
152+
return $this;
153+
}
154+
155+
}

0 commit comments

Comments
 (0)