BulkActions - Copy multiple rows to clipboard

I've got a custom Action that extends BulkAction, and I'm using $this->dispatch to send the data for the clipboard to the client:
(this is in the ->action())
foreach ($records as $record) {
    //copy to clipboard logic
    $showName = $record->show->name;
    $tourName = $record->production->name;
    $date = $record->clipboardDate;
    $this->clipboard .= "$showName - $tourName - $date\n";
}
$this->dispatch('copy-to-clipboard', [$this->clipboard]);

This works fine - ish. It only uses the data from the previous time you ran bulk action, rather than waiting for the foreach to finish it seems the dispatch is running first, even though it should be synchronous?

The js in my blade file is:
<div x-data="{
    copyToClipboard(event) { 
        console.log(event.detail);
        navigator.clipboard.writeText(event.detail);
    }
}" 
x-on:copy-to-clipboard.window="copyToClipboard">
</div>


Any ideas why this dispatch is running before the foreach loop ends, or how I can achieve this another way?
Was this page helpful?