Last active
December 19, 2024 20:36
-
-
Save geosem42/63c35e9ebb23344ec6a71e8756ae5023 to your computer and use it in GitHub Desktop.
Make table records seletacble in Filament Laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use App\Traits\HasResourcePermissions; | |
| class UserResource extends Resource | |
| { | |
| public static function table(Table $table): Table | |
| { | |
| return $table | |
| ->columns([ | |
| // | |
| ]) | |
| ->recordUrl(null) // add this | |
| ->recordAction('handleRowClick') // add this | |
| ->filters([ | |
| // | |
| ]) | |
| ->actions([ | |
| // | |
| ]) | |
| ->bulkActions([ | |
| // | |
| ]); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ListUsers extends ListRecords | |
| { | |
| use HasClickableTableRows; | |
| protected static string $resource = UserResource::class; | |
| protected function getHeaderActions(): array | |
| { | |
| return [ | |
| Actions\CreateAction::make(), | |
| ]; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace App\Traits; | |
| trait HasClickableTableRows | |
| { | |
| public function handleRowClick($record): void | |
| { | |
| $this->js(<<<JS | |
| const checkbox = document.querySelector('input[type="checkbox"][value="{$record}"].fi-checkbox-input'); | |
| const scope = Alpine.evaluate(checkbox, 'selectedRecords'); | |
| if (!scope.includes('{$record}')) { | |
| scope.push('{$record}'); | |
| } else { | |
| scope.splice(scope.indexOf('{$record}'), 1); | |
| } | |
| JS); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment