F
Filament12mo ago
Jordy

Select Table record for bulk action on click

I want to select a record when clicked on, the CanSelectRecord trait only has a deselect all method, but no select one. I have tried
protected function getTableRecordActionUsing()
{
return fn($record) =>
$this->selectedTableRecords[] = $record;
}
protected function getTableRecordActionUsing()
{
return fn($record) =>
$this->selectedTableRecords[] = $record;
}
But this threw an error. Any help is appreciated
14 Replies
Dennis Koch
Dennis Koch12mo ago
But this threw an error.
And you didn't think it would help to share that error? I guess there is no $this and it needs to be the id and not the full record:
return fn ($livewire, $record) => $livewire->selectedTableRecords[] = $record->id;
return fn ($livewire, $record) => $livewire->selectedTableRecords[] = $record->id;
Jordy
Jordy12mo ago
I will try your idea. original error:
Jordy
Jordy12mo ago
where are you getting the $livewire from? it is not passed in automatically for me throws too few arguments
Dennis Koch
Dennis Koch12mo ago
Oh then you probably were right with $this
Jordy
Jordy12mo ago
using the id as you said throws the same error in a different flavor: return fn($record) => $this->selectedTableRecords[] = $record->id;
Dennis Koch
Dennis Koch12mo ago
Right. It expects a method that returns an action. So you need to wrap your logic in another method or an action
Jordy
Jordy12mo ago
swapped out getTableRecordActionUsing out for getTableRecordAction()
protected function getTableRecordAction()
{
return 'select';
}

public function select($record)
{
// dd($record);
$this->selectedTableRecords[] = $record;
}
protected function getTableRecordAction()
{
return 'select';
}

public function select($record)
{
// dd($record);
$this->selectedTableRecords[] = $record;
}
the dd does indeed dd the id of said record but when clicking now it does nothing- with no error, when diedumping dd($this->selectedTableRecords); it does dump the right id.. but it doesnt select it in the frontend
Dennis Koch
Dennis Koch12mo ago
I don’t really know if this is used in the Frontend since it’s alpine based. Guess you need to check out the source code
Jordy
Jordy12mo ago
I do not have time to do so at the moment(this is for work haha), besides its more of a nice to have Will leave this open for a day or two in case someone else might want to look into it. Cheers for trying to help!
Alvaro Leon
Alvaro Leon12mo ago
Have you tried to use getAllSelectableTableRecordKeys() ?
Dennis Koch
Dennis Koch12mo ago
That’s for fetching them not setting
Alvaro Leon
Alvaro Leon12mo ago
Yeah but
php
$this->selectedTableRecords[] = getAllSelectableTableRecordKeys();
php
$this->selectedTableRecords[] = getAllSelectableTableRecordKeys();
should work?
Dennis Koch
Dennis Koch12mo ago
Not sure how that should work to select a specific record
Jordy
Jordy12mo ago
I do not want to select all records.. And this has the same problem, it throws no error but does not select them either. It seems the table is not automatically updated when the selectedTableRecords array is changed. and I dont know what event is emitted normally, that should fix it.. I think