Skip to content
This repository was archived by the owner on Oct 20, 2025. It is now read-only.

Commit 4abed07

Browse files
Add ability to use Closure as a sortable parameter in table's column (#353)
Co-authored-by: Pascal Baljet <[email protected]>
1 parent 52801c3 commit 4abed07

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

app/tests/Unit/SpladeQueryBuilderTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
namespace Tests\Unit;
44

5+
use App\Models\Address;
56
use Illuminate\Database\Query\Builder;
7+
use Illuminate\Support\Facades\DB;
68
use ProtoneMedia\Splade\SpladeQueryBuilder;
9+
use ProtoneMedia\Splade\SpladeTable;
710
use Tests\TestCase;
811

912
class SpladeQueryBuilderTest extends TestCase
@@ -21,4 +24,23 @@ public function it_can_parse_terms()
2124
$this->assertEquals(['0'], $builder->parseTermsIntoCollection('0')->all());
2225
$this->assertEquals(['1'], $builder->parseTermsIntoCollection('1')->all());
2326
}
27+
28+
/** @test */
29+
public function it_can_apply_sorting_with_a_closure()
30+
{
31+
$table = SpladeTable::for(Address::class)
32+
->column('two_columns', sortable: function ($builder, $direction) {
33+
$builder->orderBy('address', $direction)
34+
->orderBy('city', $direction);
35+
})
36+
->defaultSort('two_columns');
37+
38+
DB::enableQueryLog();
39+
$table->beforeRender();
40+
DB::disableQueryLog();
41+
$log = DB::getQueryLog();
42+
43+
$this->assertCount(1, $log);
44+
$this->assertEquals('select * from "addresses" order by "address" asc, "city" asc', $log[0]['query']);
45+
}
2446
}

src/SpladeQueryBuilder.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ private function applyConstraint(array $columns, string $terms)
194194
*/
195195
private function applySorting(Column $column)
196196
{
197+
if (is_callable($column->sortable)) {
198+
return ($column->sortable)($this->builder, $column->sorted);
199+
}
200+
197201
if (!$column->isNested()) {
198202
// Not a relationship, just a column on the table.
199203
return $this->builder->orderBy($column->key, $column->sorted);

src/Table/Column.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function __construct(
2121
public string $label,
2222
public bool $canBeHidden,
2323
public bool $hidden,
24-
public bool $sortable,
24+
public bool|Closure $sortable,
2525
public bool|string $sorted,
2626
public bool $highlight,
2727
public bool|Closure $exportAs,
@@ -70,7 +70,7 @@ public function toArray()
7070
'label' => $this->label,
7171
'can_be_hidden' => $this->canBeHidden,
7272
'hidden' => $this->hidden,
73-
'sortable' => $this->sortable,
73+
'sortable' => $this->sortable !== false,
7474
'sorted' => $this->sorted,
7575
'highlight' => $this->highlight,
7676
];

src/Table/HasColumns.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ProtoneMedia\Splade\Table;
44

5+
use Closure;
56
use Illuminate\Support\Collection;
67
use Illuminate\Support\Str;
78

@@ -48,7 +49,7 @@ public function column(
4849
string $label = null,
4950
bool|null $canBeHidden = null,
5051
bool $hidden = false,
51-
bool $sortable = false,
52+
bool|Closure $sortable = false,
5253
bool|string $searchable = false,
5354
bool|null $highlight = null,
5455
bool|callable $exportAs = true,

0 commit comments

Comments
 (0)