Skip to content

Instantly share code, notes, and snippets.

@geosem42
Last active December 19, 2024 20:36
Show Gist options
  • Select an option

  • Save geosem42/63c35e9ebb23344ec6a71e8756ae5023 to your computer and use it in GitHub Desktop.

Select an option

Save geosem42/63c35e9ebb23344ec6a71e8756ae5023 to your computer and use it in GitHub Desktop.
Make table records seletacble in Filament Laravel
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([
//
]);
}
}
class ListUsers extends ListRecords
{
use HasClickableTableRows;
protected static string $resource = UserResource::class;
protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}
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