I am trying to create a button above a table to toggle between showing records that have been soft deleted (in addition to showing the regular records) and just showing the regular records. Everything works correctly for the most part, I'm just being held up on two things. The first is the button label doesn't seem to be syncing correctly. Here is my code:
Action::make('showDeleted')
->label($this->showDeleted ? 'Hide Deleted' : 'Show Deleted')
->action(function () {
$this->showDeleted = !$this->showDeleted;
}),
Again, the functionality is fine, the records show when I press the button and go away when I press the button again, but for some reason the label updates weirdly. First press doesn't update it, but the second and subsequent presses do and make the labels out of sync with what is actually being shown on the table. If I reverse the label order (->label($this->showDeleted ? 'Show Deleted' : 'Hide Deleted')
), the page loads with "Hide Deleted", doesn't update on first press, and then updates with subsequent presses, which does end up reflecting what is on the table, but is still wrong. I would like to use a button here and not a toggle. Any suggestions?
The second one isn't as big of a deal, but when the deleted records show up, I'd like to make it so the rows that are marked as soft deleted show with a different color. I tried this but it didn't do anything:
protected function getTableRowClasses(Model $record): ?string
{
// Apply the background color directly for deleted rows
return $record->trashed() ? 'background-color: #f7fafc;' : null;
}
Figured out a solution. Apparently, my first solution is a direct expression that is only evaluated once when the action is created rather than dynamically. This made it update out of sync. I don't really get it to be honest. Here is the revised solution:
Action::make('showDeleted')
->label(fn () => $this->showDeleted ? 'Hide Deleted' : 'Show Deleted')
->action(function () {
$this->showDeleted = !$this->showDeleted;
}),
In this, the label uses what's called a closure. Again, don't really understand it, but more info might be found in the Filament docs: https://filamentphp.com/docs/2.x/forms/advanced. As for the updating colors, I was never able to figure that out. I just added a custom label with an action I wanted to do specifically for the deleted ones (meaning a little text button would show on the side saying "Restore").