Open OzanKurt opened 6 months ago
No plans but open for a PR if you can. Thanks!
So...
I've been playing around with the export package and actually found out that integrating it with buttons is extremely simple.
I added this custom button which sends an ajax request to the server side and it gets handled by the exportQueue
method from WithExportQueue
.
DataTable.ext.buttons.exportQueue = {
className: 'buttons-export-queue',
text: function (dt) {
return '<i class="far fa-fw fa-file-excel"></i>';
},
action: function (e, dt, button, config) {
var url = _buildUrl(dt, 'exportQueue');
$.ajax({
url: url,
type: 'GET',
data: {},
complete: window.ajax_complete_handler,
});
}
};
There are a couple of ideas I've had:
1- We need to add a customizable callback function to the exportQueue()
method so that I could return a custom JSON response instead of just the $batch->id
public function exportQueue(): string
{
$job = new DataTableExportJob(
[self::class, $this->attributes],
request()->all(),
Auth::id() ?? 0,
$this->sheetName(),
);
$batch = Bus::batch([$job])->name('datatables-export')->dispatch();
return $batch->id;
}
2- I had to implement a getUser()
method inside the DataTableExportJob
to get the User
instance from the id
:
public function getUser(): ?User
{
return $this->user ? User::find($this->user) : null;
}
3- We need to be able to handle the errors that might occur during the Job:
4- I think the mail should be separated from the Job
"mail_type" => "send" | "queue"
, so that the mail could also be queued independently from the job.exportQueue()
and add a callback before we send the email, or maybe even listen for the completion on client side and trigger download (without needing an email).Maybe something like this:
public function afterExport(array $paths): bool
{
// The `$paths` can contain multiple paths of the file like:
// ['local' => 'localpath', 's3' => 's3path', 'otherDisk' => 'otherpath']
// do some stuff with the exported file, maybe save it to an archive inside the media-library.
// return `true` to continue with the email
// return `false` to skip email, and maybe send the file to every user in a team
}
5- I don't think we need a job_batch for this since it's only 1 job, we should be able to dispatch the single job alone. (I don't have much exp. with batches, so no judgement 😞)
6- Ability to add multiple destination disks
7- Ability to send the job to multiple users (this can be done manually if we implement "step 4")
9- Customizable mail_subject
10- A nicer Email Template from Laravels defaults 😄
Let me know what you think about these ideas.
I also believe that this package could be integrated inside the buttons package directly, only the livewire
parts will only be registered if livewire is installed.
So, inside the ExportServiceProvider we can add something like this:
if (class_exists('Livewire\\Livewire')) {
// Do livewire related inits
}
Good points provided @OzanKurt. Will also try to implement it if I can.
5: the batch job is needed to check the status for real-time response.
10: agree, we haven't used this feature so no enhancements were done yet
The package idea came from Laravel Daily
A non-livewire version would be much appreciated.
This would be extremely useful for me, in case you can implement it partially I could also continue.