I have a data table where one column is a thumbnail URI, that I render in the column as an image.
I use DataTables as a Service Implementation.
I also got column filters to narrow my results.
I am trying to export an excel file that has the image in the column. I try to extend buttons like in this example https://datatables.net/extensions/buttons/examples/html5/outputFormat-function.html but I can't make it work.
Two questions:
Thanks.
Is there an example of how to extend buttons (exportOptions->format->body) in the Service Implementation?
None, you cannot extend the button output via client-side/js script. But you can change the output on the backend.
Can I somehow get the SQL query (with sorting, search box and filters applied) so I can code my own excel export?
You can override the buildExcelFile() method and write your way on exporting to csv, excel & pdf. We use laravel-excel package so better check their docs.
protected function buildExcelFile()
{
/** @var \Maatwebsite\Excel\Excel $excel */
$excel = app('excel');
return $excel->create($this->getFilename(), function (LaravelExcelWriter $excel) {
$excel->sheet('exported-data', function (LaravelExcelWorksheet $sheet) {
$sheet->fromArray($this->getDataForExport());
});
});
}
Thanks!!
I added the buildExcelFile function in the *DataTable.php file
This is part of the code I ended up with:
protected function buildExcelFile()
{
ini_set('max_execution_time', 300);
$excel = app('excel');
$extraColumns=count($this->getDataForExport()['0'])-6; // $this->getDataForExport() is where the data is at!
return \Maatwebsite\Excel\Facades\Excel::create('Laravel Excel', function($excel) use ($extraColumns) {
$excel->sheet('Excel sheet', function($sheet) use ($extraColumns) {
// lots of formatting code here
})->export('xls');
}
Great, but I suggest you remove the ->export('xls') part since DataTable will handle the export type according to the request.
Sir I face the problem I am not able to set the width of excel when I use data table button of excel it it any way to set the width when I call button extend code
{
extend: 'excelHtml5',
exportOptions: {
columns: [0]
}
},