Skip to content

Commit 9a9c974

Browse files
tanthammarclaude
andcommitted
Add direction argument to table defaultGroup()
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 1d424bd commit 9a9c974

6 files changed

Lines changed: 91 additions & 3 deletions

File tree

packages/tables/docs/07-grouping.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ public function table(Table $table): Table
2525

2626
<AutoScreenshot name="tables/grouping" alt="Table with grouping" version="4.x" />
2727

28+
You may also pass a sort direction to the `defaultGroup()` method:
29+
30+
```php
31+
use Filament\Tables\Table;
32+
33+
public function table(Table $table): Table
34+
{
35+
return $table
36+
->defaultGroup('status', direction: 'desc');
37+
}
38+
```
39+
40+
The second parameter is optional and defaults to `'asc'`.
41+
2842
## Allowing users to choose between groupings
2943

3044
You may also allow users to pick between different groupings, by passing them in an array to the `groups()` method:

packages/tables/src/Concerns/CanGroupRecords.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ protected function applyGroupingToTableQuery(Builder $query): Builder
6161

6262
$group->applyEagerLoading($query);
6363

64-
$group->orderQuery($query, $this->getTableGroupingDirection() ?? 'asc');
64+
$group->orderQuery($query, $this->getTableGroupingDirection() ?? $this->getTable()->getDefaultGroupDirection());
6565

6666
return $query;
6767
}

packages/tables/src/Concerns/InteractsWithTable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function bootedInteractsWithTable(): void
9292
}
9393

9494
if ($this->getTable()->isDefaultGroupSelectable()) {
95-
$this->tableGrouping = "{$this->getTable()->getDefaultGroup()->getId()}:asc";
95+
$this->tableGrouping = "{$this->getTable()->getDefaultGroup()->getId()}:{$this->getTable()->getDefaultGroupDirection()}";
9696
}
9797

9898
$shouldPersistSearchInSession = $this->getTable()->persistsSearchInSession();

packages/tables/src/Table/Concerns/CanGroupRecords.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
use Filament\Support\Icons\Heroicon;
99
use Filament\Tables\Grouping\Group;
1010
use Filament\Tables\View\TablesIconAlias;
11+
use Illuminate\Support\Str;
1112

1213
trait CanGroupRecords
1314
{
1415
protected string | Group | Closure | null $defaultGroup = null;
1516

17+
protected string | Closure | null $defaultGroupDirection = 'asc';
18+
1619
/**
1720
* @var array<string, Group>
1821
*/
@@ -80,9 +83,10 @@ public function collapsedGroupsByDefault(bool | Closure $condition = true): stat
8083
return $this;
8184
}
8285

83-
public function defaultGroup(string | Group | Closure | null $group): static
86+
public function defaultGroup(string | Group | Closure | null $group, string | Closure | null $direction = 'asc'): static
8487
{
8588
$this->defaultGroup = $group;
89+
$this->defaultGroupDirection = $direction;
8690

8791
return $this;
8892
}
@@ -179,6 +183,11 @@ public function getDefaultGroup(): ?Group
179183
->table($this);
180184
}
181185

186+
public function getDefaultGroupDirection(): string
187+
{
188+
return Str::lower($this->evaluate($this->defaultGroupDirection) ?? 'asc');
189+
}
190+
182191
/**
183192
* @return array<string, Group>
184193
*/
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Filament\Tests\Fixtures\Livewire;
4+
5+
use Filament\Actions\Concerns\InteractsWithActions;
6+
use Filament\Actions\Contracts\HasActions;
7+
use Filament\Schemas\Concerns\InteractsWithSchemas;
8+
use Filament\Schemas\Contracts\HasSchemas;
9+
use Filament\Tables;
10+
use Filament\Tables\Table;
11+
use Filament\Tests\Fixtures\Models\Post;
12+
use Illuminate\Contracts\View\View;
13+
use Livewire\Component;
14+
15+
class DefaultGroupedPostsTable extends Component implements HasActions, HasSchemas, Tables\Contracts\HasTable
16+
{
17+
use InteractsWithActions;
18+
use InteractsWithSchemas;
19+
use Tables\Concerns\InteractsWithTable;
20+
21+
public function table(Table $table): Table
22+
{
23+
return $table
24+
->query(Post::query())
25+
->groups([
26+
Tables\Grouping\Group::make('title'),
27+
])
28+
->defaultGroup('title', 'desc')
29+
->columns([
30+
Tables\Columns\TextColumn::make('title'),
31+
]);
32+
}
33+
34+
public function render(): View
35+
{
36+
return view('livewire.table');
37+
}
38+
}

tests/src/Tables/GroupingTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Filament\Tables\Grouping\Group;
4+
use Filament\Tests\Fixtures\Livewire\DefaultGroupedPostsTable;
45
use Filament\Tests\Fixtures\Livewire\GroupedCustomDataTable;
56
use Filament\Tests\Fixtures\Livewire\PostsTable;
67
use Filament\Tests\Fixtures\Livewire\PostsTableWithoutSummarizers;
@@ -55,6 +56,32 @@
5556
});
5657
});
5758

59+
it('can group records by a default group in descending order', function (): void {
60+
Post::factory()->create(['title' => 'Apple Post']);
61+
Post::factory()->create(['title' => 'Cherry Post']);
62+
Post::factory()->create(['title' => 'Banana Post']);
63+
64+
$sortedPosts = Post::query()->orderByDesc('title')->orderBy('id')->get();
65+
66+
livewire(DefaultGroupedPostsTable::class)
67+
->assertSet('tableGrouping', 'title:desc')
68+
->assertCanSeeTableRecords($sortedPosts, inOrder: true);
69+
});
70+
71+
it('can set a `defaultGroup()` direction and get with `getDefaultGroupDirection()`', function (): void {
72+
$livewire = livewire(PostsTable::class)->instance();
73+
$table = $livewire->getTable();
74+
75+
expect($table->defaultGroup('title', 'DESC')->getDefaultGroupDirection())->toBe('desc');
76+
});
77+
78+
it('defaults the `defaultGroup()` direction to ascending', function (): void {
79+
$livewire = livewire(PostsTable::class)->instance();
80+
$table = $livewire->getTable();
81+
82+
expect($table->defaultGroup('title')->getDefaultGroupDirection())->toBe('asc');
83+
});
84+
5885
it('can group records by column', function (): void {
5986
// Create posts with different titles to group by
6087
Post::factory()->create(['title' => 'Apple Post']);

0 commit comments

Comments
 (0)