How to implement action or trigger event after reordering rows in a table

I want to run some code after a user is done reordering how do i achieve this? is it even possible.
12 Replies
Grab 🇮🇹
Grab 🇮🇹3w ago
I got the same question. @RichtheKid have you found a solutions?
Matthew
Matthew3w ago
But I dont think that there is a function for it
Grab 🇮🇹
Grab 🇮🇹3w ago
I need to trigger an action when the user finish to reorder a table in a relation manager
Matthew
Matthew3w ago
or maybe from this callable:
->reorderRecordsTriggerAction(
fn (Action $action, bool $isReordering) => $action
->button()
->label($isReordering ? 'Disable reordering' : 'Enable reordering'),
);
->reorderRecordsTriggerAction(
fn (Action $action, bool $isReordering) => $action
->button()
->label($isReordering ? 'Disable reordering' : 'Enable reordering'),
);
Grab 🇮🇹
Grab 🇮🇹3w ago
I think this helps only to customize the button that enable reordering. Am I wrong?
Matthew
Matthew3w ago
1s
use Filament\Tables\Actions\Action;

// ....
->reorderRecordsTriggerAction(
fn (Action $action, bool $isReordering) => $action
->action(function () {
if (! $isReordering){
// Do something
}
})
->button()
->label($isReordering ? 'Disable reordering' : 'Enable reordering'),
);
use Filament\Tables\Actions\Action;

// ....
->reorderRecordsTriggerAction(
fn (Action $action, bool $isReordering) => $action
->action(function () {
if (! $isReordering){
// Do something
}
})
->button()
->label($isReordering ? 'Disable reordering' : 'Enable reordering'),
);
I havent tried this, but its a guess
Grab 🇮🇹
Grab 🇮🇹3w ago
Very interesting, I will try
->reorderRecordsTriggerAction( fn (TableAction $action, bool $isReordering) => $action ->action(function ($isReordering){ if (! $isReordering) { CalculateRegistrationPointsByPosition::make('CalculateRegistrationPointsByPosition'); } } ) ->button() ->label($isReordering ? 'End reordering' : 'Reorder'), )
I tried this implemetation adding $reordering var inside action function but it doesn't work When I click Reorder nothing happen, reordering with drag & drop doesn't start
Matthew
Matthew3w ago
Then its not possible atm. I asked Dan if I can make a PR for it, I will let you know if he agrees actually Try this:
->reorderRecordsTriggerAction(
fn (Action $action, bool $isReordering) => $action
->button()
->after(function () {
})
->label($isReordering ? 'Disable reordering' : 'Enable reordering'),
);
->reorderRecordsTriggerAction(
fn (Action $action, bool $isReordering) => $action
->button()
->after(function () {
})
->label($isReordering ? 'Disable reordering' : 'Enable reordering'),
);
Try using the $isReordering inside the after()
Grab 🇮🇹
Grab 🇮🇹3w ago
We are quite there
->reorderRecordsTriggerAction(
fn (TableAction $action, bool $isReordering) => $action
->button()
->after(function ($isReordering) {
if (! $isReordering) {
CalculateRegistrationPointsByPosition::make('CalculateRegistrationPointsByPosition');
}
})
->label($isReordering ? 'Disable reordering' : 'Enable reordering'),
)
->reorderRecordsTriggerAction(
fn (TableAction $action, bool $isReordering) => $action
->button()
->after(function ($isReordering) {
if (! $isReordering) {
CalculateRegistrationPointsByPosition::make('CalculateRegistrationPointsByPosition');
}
})
->label($isReordering ? 'Disable reordering' : 'Enable reordering'),
)
With this code reordering default behavior works but it seems that this action is not executed:
CalculateRegistrationPointsByPosition::make('CalculateRegistrationPointsByPosition');
CalculateRegistrationPointsByPosition::make('CalculateRegistrationPointsByPosition');
Matthew
Matthew3w ago
Yeah, then I think it just doesn't work :/
Grab 🇮🇹
Grab 🇮🇹3w ago
I also tried to simplify omitting $isRecording logic:
->reorderRecordsTriggerAction(
fn (Action $action, bool $isReordering) => $action
->button()
->after(function () {
CalculateRegistrationPointsByPosition::make('CalculateRegistrationPointsByPosition');
})
->label($isReordering ? 'Disable reordering' : 'Enable reordering'),
)
->reorderRecordsTriggerAction(
fn (Action $action, bool $isReordering) => $action
->button()
->after(function () {
CalculateRegistrationPointsByPosition::make('CalculateRegistrationPointsByPosition');
})
->label($isReordering ? 'Disable reordering' : 'Enable reordering'),
)
Doesn't work Thanks a lot for your support 🫶🏻 It was my fault, my action was not correct Now I need only a way to pass a parameter to a new function I just developed I need to access $ownerRecord inside reorderRecordsTriggerAction() it's working in a very rough way but it works Here is my full code:
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('user_id')
->columns([
...
])
->defaultSort('position')
->reorderable('position')
->reorderRecordsTriggerAction(function (Action $action, bool $isReordering, RegistrationsRelationManager $livewire)
{
$action
->button()
->label($isReordering ? 'Disable reordering' : 'Reorder');

// Access $ownerRecord using $livewire argument
$ownerRecord = $livewire->ownerRecord;
$records = $ownerRecord->registrations;

if(!$isRecording){
/* Your custom logic */
}

);
}
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('user_id')
->columns([
...
])
->defaultSort('position')
->reorderable('position')
->reorderRecordsTriggerAction(function (Action $action, bool $isReordering, RegistrationsRelationManager $livewire)
{
$action
->button()
->label($isReordering ? 'Disable reordering' : 'Reorder');

// Access $ownerRecord using $livewire argument
$ownerRecord = $livewire->ownerRecord;
$records = $ownerRecord->registrations;

if(!$isRecording){
/* Your custom logic */
}

);
}

Did you find this page helpful?