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
emil2w ago
Do you mean selected as in bulk select?
Umar Farooq
Umar FarooqOP2w ago
yes
Umar Farooq
Umar FarooqOP2w ago
default selected records
No description
Umar Farooq
Umar FarooqOP2w ago
anyone please?
Cupergaming
Cupergaming2w 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 Ayaou2w ago
He want a record to be selected by default on the page load.
Umar Farooq
Umar FarooqOP2w ago
yes
Takashi
Takashi2w ago
You can easily set the deatult selected record I can help you
Mohamed Ayaou
Mohamed Ayaou2w ago
Can you please share the solutions here as these Help chats will be used by other people in the future
Takashi
Takashi2w ago
I think we can eaily solve it with including selected function in table defination part you get it done?
Umar Farooq
Umar FarooqOP6d ago
@Takashi what is the function name? I checked the table class but couldn't find any function that handles this functionality anyone?
Takashi
Takashi6d ago
$selectedRecordId = 1; return $table->columns([ ....columns.here ])->defaultSelected($selectedRecordId) hope it works
LeandroFerreira
defaultSelected ? Where is it?
Takashi
Takashi6d ago
That is the function to set default record
LeandroFerreira
where? Could you share where is it in Filament?
Takashi
Takashi6d 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
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
Takashi
Takashi6d ago
I am not sure where i found it. Only, I faced that problem, and solved it with that
LeandroFerreira
Are you using chatGPT to answer questions?
Takashi
Takashi6d ago
no I don't like AI in development Are you willing to share your source code with me?
LeandroFerreira
On what basis would you solve it?
Takashi
Takashi6d ago
based on the code i sent
LeandroFerreira
answering questions like "try ->whatever(xx) ?
Takashi
Takashi6d 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
This type of answer ins't helpful if the suggestion you provided doesn't exist
Takashi
Takashi6d ago
so, did you check it? who are you? contact me after checking it on your side
LeandroFerreira
let me know how it works.. did you try that before answering this question?
Takashi
Takashi6d ago
I already said about that
LeandroFerreira
again, this type of answer ins't helpful
Takashi
Takashi6d 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
@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 FarooqOP5d 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 FarooqOP5d ago
@LeandroFerreira the default selected records working fine but the selected count showing wrong number
No description
Mohamed Ayaou
Mohamed Ayaou5d ago
maybe because the array in the @LeandroFerreira has 3 items? try to modifie it.
LeandroFerreira
He is joking.. 😅
Umar Farooq
Umar FarooqOP5d 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 FarooqOP5d ago
LeandroFerreira
I'll try it and I let you know..
Want results from more Discord servers?
Add your server