Skip to content

Commit f282095

Browse files
committed
Merge branch 'feature/sorting-callbacks' into develop
2 parents ddf0ac9 + adafafd commit f282095

File tree

5 files changed

+57
-4
lines changed

5 files changed

+57
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ All notable changes to `laravel-livewire-tables` will be documented in this file
66

77
### Added
88

9+
- Added callback to column's sortable() method to customize sorting functionality per column.
910
- Support for polling `keep-alive` and `visible`.
1011

1112
### Changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,9 @@ public function columns(): array
192192
{
193193
return [
194194
Column::make('Type') // Column text and optional column name, column name will be snake case of text if not defined
195-
->sortable() // Whether or not the heading can be clicked to sort
195+
->sortable(), // Whether or not the heading can be clicked to sort
196196
Column::make('Name')
197-
->sortable()
197+
->sortable(),
198198
Column::make('Permissions'),
199199
Column::make('Other', 'my_other_column')
200200
->sortable() // Allows the column to interact with the sorting methods
@@ -204,6 +204,17 @@ public function columns(): array
204204
}
205205
```
206206

207+
#### Customizing Sort Behavior
208+
209+
If you need more control over the sort behavior of a column, you can pass a callback to the sortable() method:
210+
211+
```php
212+
Column::make(__('Address'))
213+
->sortable(function(Builder $query, $direction) {
214+
return $query->orderBy(UserAttribute::select('address')->whereColumn('user_attributes.user_id', 'users.id'), $direction);
215+
})
216+
```
217+
207218
#### Configuring Sort Names
208219

209220
When clicking sortable column headers, the component will use the column name to define the sorting pill in the UI, if you don't like the way the name is rendered, you can overwrite it:

src/DataTableComponent.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,18 @@ public function render()
190190
'rows' => $this->rows,
191191
]);
192192
}
193+
194+
/**
195+
* Get a column object by its field
196+
*
197+
* @param string $column
198+
*
199+
* @return mixed
200+
*/
201+
protected function getColumn(string $column)
202+
{
203+
return collect($this->columns())
204+
->where('column', $column)
205+
->first();
206+
}
193207
}

src/Traits/WithSorting.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ public function sortBy(string $field): ?string
3030
public function applySorting(Builder $query): Builder
3131
{
3232
foreach ($this->sorts as $field => $direction) {
33-
$query->orderBy($field, $direction);
33+
if (optional($this->getColumn($field))->hasSortCallback()) {
34+
$query = app()->call($this->getColumn($field)->getSortCallback(), ['query' => $query, 'direction' => $direction]);
35+
} else {
36+
$query->orderBy($field, $direction);
37+
}
3438
}
3539

3640
return $query;

src/Views/Column.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ class Column
2424
*/
2525
public bool $sortable = false;
2626

27+
/**
28+
* @var
29+
*/
30+
public $sortCallback;
31+
2732
/**
2833
* @var string|null
2934
*/
@@ -103,10 +108,12 @@ public function isBlank(): bool
103108
/**
104109
* @return $this
105110
*/
106-
public function sortable(): self
111+
public function sortable($callback = null): self
107112
{
108113
$this->sortable = true;
109114

115+
$this->sortCallback = $callback;
116+
110117
return $this;
111118
}
112119

@@ -192,4 +199,20 @@ public function formatted($row, $column = null)
192199

193200
return $value;
194201
}
202+
203+
/**
204+
* @return bool
205+
*/
206+
public function hasSortCallback(): bool
207+
{
208+
return $this->sortCallback !== null;
209+
}
210+
211+
/**
212+
* @return callable|null
213+
*/
214+
public function getSortCallback(): ?callable
215+
{
216+
return $this->sortCallback;
217+
}
195218
}

0 commit comments

Comments
 (0)