Laravel-datatables: Format output data - export options

Created on 30 Jan 2017  路  4Comments  路  Source: yajra/laravel-datatables

Summary of problem or feature request


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:

  1. Is there an example of how to extend buttons (exportOptions->format->body) in the Service Implementation?
  2. Can I somehow get the SQL query (with sorting, search box and filters applied) so I can code my own excel export?

Thanks.

System details

  • Operating System homestead virtual box
  • PHP Version 5.6.4
  • Laravel Version 5.3
  • Laravel-Datatables Version "yajra/laravel-datatables-oracle": "^6.21"
question

All 4 comments

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]
}
},

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Mopster picture Mopster  路  3Comments

vipin733 picture vipin733  路  3Comments

macnux picture macnux  路  3Comments

jackrsantana picture jackrsantana  路  3Comments

SGarridoDev picture SGarridoDev  路  3Comments