F
Filament12mo ago
ndevfr

Is it possible to add "Previous" and "Next" buttons on Form ?

Hello, I want buttons to go to previous record or next record when I am editing one ? Is it possible ?
Solution:
This will do what you're asking ```php class EditTask extends EditRecord {...
Jump to solution
5 Replies
CalumThomson
CalumThomson11mo ago
I also have this need. Anyone know how 'Previous' and 'Next' buttons on a form to navigate through a table's result list of records can be achieved?
Matthew
Matthew11mo ago
Are you in v2 or v3? You can check with php artisan about
Solution
Matthew
Matthew11mo ago
This will do what you're asking
class EditTask extends EditRecord
{
protected static string $resource = TaskResource::class;

protected function getHeaderActions(): array
{
$nextRecord = $this->getNewRecord(true);
$prevRecord = $this->getNewRecord(false);

return [
DeleteAction::make(),
Action::make('next')
->url($nextRecord ? TaskResource::getUrl('edit', ['record' => $nextRecord->getKey()]) : '#')
->disabled(!$nextRecord),
Action::make('previous')
->url($prevRecord ? TaskResource::getUrl('edit', ['record' => $prevRecord->getKey()]) : '#')
->disabled(!$prevRecord),
];
}

protected function getNewRecord(bool $nextRecord){
$currentRecord = $this->getRecord();
if (!$currentRecord) {
return null;
}

$record = null;
if ($nextRecord){
$record = $currentRecord->where('id', '>', $currentRecord->getKey());
}else{
$record = $currentRecord->where('id', '<', $currentRecord->getKey())->orderBy('id', 'desc');
}
return $record->first();
}
}
class EditTask extends EditRecord
{
protected static string $resource = TaskResource::class;

protected function getHeaderActions(): array
{
$nextRecord = $this->getNewRecord(true);
$prevRecord = $this->getNewRecord(false);

return [
DeleteAction::make(),
Action::make('next')
->url($nextRecord ? TaskResource::getUrl('edit', ['record' => $nextRecord->getKey()]) : '#')
->disabled(!$nextRecord),
Action::make('previous')
->url($prevRecord ? TaskResource::getUrl('edit', ['record' => $prevRecord->getKey()]) : '#')
->disabled(!$prevRecord),
];
}

protected function getNewRecord(bool $nextRecord){
$currentRecord = $this->getRecord();
if (!$currentRecord) {
return null;
}

$record = null;
if ($nextRecord){
$record = $currentRecord->where('id', '>', $currentRecord->getKey());
}else{
$record = $currentRecord->where('id', '<', $currentRecord->getKey())->orderBy('id', 'desc');
}
return $record->first();
}
}
Matthew
Matthew11mo ago
In addition, it also has checks in case you reach the first/last record, then it will be disabled
CalumThomson
CalumThomson11mo ago
Thank you, this is great! I still need to do some work retaining search and sort options from the original table list, but this definitely has set me on the right path. Sorry I seem unable to mark this as the solution, but definitely works for me. (Filament v3 by the way)
Want results from more Discord servers?
Add your server