How to set the default selected records in the filament table?

How can I set the default selected record in the Filament table?
38 Replies
emil
emil2mo ago
Do you mean selected as in bulk select?
Umar Farooq
Umar FarooqOP2mo ago
yes
Umar Farooq
Umar FarooqOP2mo ago
default selected records
No description
Umar Farooq
Umar FarooqOP2mo ago
anyone please?
Cupergaming
Cupergaming2mo ago
What exactly do you mean by default selected? A selected record is the record you wish to preform an action on in almost all cases I believe.
Mohamed Ayaou
Mohamed Ayaou2mo ago
He want a record to be selected by default on the page load.
Umar Farooq
Umar FarooqOP2mo ago
yes
taka92
taka922mo ago
You can easily set the deatult selected record I can help you
Mohamed Ayaou
Mohamed Ayaou2mo ago
Can you please share the solutions here as these Help chats will be used by other people in the future
taka92
taka922mo ago
I think we can eaily solve it with including selected function in table defination part you get it done?
Umar Farooq
Umar FarooqOP2mo ago
@Takashi what is the function name? I checked the table class but couldn't find any function that handles this functionality anyone?
taka92
taka922mo ago
$selectedRecordId = 1; return $table->columns([ ....columns.here ])->defaultSelected($selectedRecordId) hope it works
LeandroFerreira
LeandroFerreira2mo ago
defaultSelected ? Where is it?
taka92
taka922mo ago
That is the function to set default record
LeandroFerreira
LeandroFerreira2mo ago
where? Could you share where is it in Filament?
taka92
taka922mo ago
use Filament\Tables; public function table(Table $table): Table { return $table ->columns([ Tables\Columns\TextColumn::make('name'), ]) ->bulkActions([ Tables\Actions\BulkAction::make('delete') ->action(fn (Collection $records) => $records->each->delete()), ]) ->defaultSelected(function () { return [1, 2, 3]; }); } I hope it works for you
LeandroFerreira
LeandroFerreira2mo ago
I don't want an example; I'd like to know where the source is that you used to suggest the defaultSelected method. For instance, the filters method exists in the HasFilters trait. Where is defaultSelected located in the Filament source code?
GitHub
filament/packages/tables/src/Table/Concerns/HasFilters.php at 3.x ·...
A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS. - filamentphp/filament
taka92
taka922mo ago
I am not sure where i found it. Only, I faced that problem, and solved it with that
LeandroFerreira
LeandroFerreira2mo ago
Are you using chatGPT to answer questions?
taka92
taka922mo ago
no I don't like AI in development Are you willing to share your source code with me?
LeandroFerreira
LeandroFerreira2mo ago
On what basis would you solve it?
taka92
taka922mo ago
based on the code i sent
LeandroFerreira
LeandroFerreira2mo ago
answering questions like "try ->whatever(xx) ?
taka92
taka922mo ago
what is the problem on that? can you share your source code? did you try to fix your problem with my code? what do you want from me? if you have no anyproblem, please don't bother me! I am here to help someone!
LeandroFerreira
LeandroFerreira2mo ago
This type of answer ins't helpful if the suggestion you provided doesn't exist
taka92
taka922mo ago
so, did you check it? who are you? contact me after checking it on your side
LeandroFerreira
LeandroFerreira2mo ago
let me know how it works.. did you try that before answering this question?
taka92
taka922mo ago
I already said about that
LeandroFerreira
LeandroFerreira2mo ago
again, this type of answer ins't helpful
taka92
taka922mo ago
again, don't bother me! I can't help anymore with only one sentence "How can I set the default selected record in the Filament table?" without any code, or detailed information...
LeandroFerreira
LeandroFerreira2mo ago
@Umar Farooq not sure if it is the better way, but you could try it: You can use a render hook in your ListPage
use Filament\Support\Facades\FilamentView;
use Filament\Tables\View\TablesRenderHook;
use Illuminate\Support\HtmlString;

public function mount(): void
{
parent::mount();

FilamentView::registerRenderHook(
TablesRenderHook::HEADER_AFTER,
fn (): string => new HtmlString('<div x-init="selectedRecords = [1, 2, 3]"></div>'),
);
}
use Filament\Support\Facades\FilamentView;
use Filament\Tables\View\TablesRenderHook;
use Illuminate\Support\HtmlString;

public function mount(): void
{
parent::mount();

FilamentView::registerRenderHook(
TablesRenderHook::HEADER_AFTER,
fn (): string => new HtmlString('<div x-init="selectedRecords = [1, 2, 3]"></div>'),
);
}
Umar Farooq
Umar FarooqOP2mo ago
I believe there's no such function as defaultSelected in the Table class not sure how it's work for you. @Takashi @Leandro Ferreira Thanks for the help. Let me check if it works
Umar Farooq
Umar FarooqOP2mo ago
@LeandroFerreira the default selected records working fine but the selected count showing wrong number
No description
Mohamed Ayaou
Mohamed Ayaou2mo ago
maybe because the array in the @LeandroFerreira has 3 items? try to modifie it.
LeandroFerreira
LeandroFerreira2mo ago
He is joking.. 😅
Umar Farooq
Umar FarooqOP2mo ago
@Mohamed Ayaou no, I only set 1 item in the array On the first page load, it's selected. When I click the selectable checkbox in the table header, it selects 2 rows from the table, but it shows 3 records selected even though there are only 2 items (rows) in the table.
Umar Farooq
Umar FarooqOP2mo ago
LeandroFerreira
LeandroFerreira2mo ago
I'll try it and I let you know..

Did you find this page helpful?