Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export CSV or PDF Based On Datatables Laravel 4

I have a datatables which contains the data retrieved from database. When I enter some keywords into search textbox (the search textbox is generated by datatables), the result of the table will be changes. This is good. But when I click export to csv or pdf, the result in csv or pdf will be retrieved from database instead of datatables.

How to export to csv/pdf based on the datatables plugin using laravel?

//datatable plugins

<link href="plugins/datatables/dataTables.bootstrap.css" rel="stylesheet" type="text/css" />
<script src="plugins/datatables/jquery.dataTables.min.js" type="text/javascript"></script>
<script src="plugins/datatables/dataTables.bootstrap.min.js" type="text/javascript"></script>

//php

public function sales_csv(){    

    // columns      
    $arrSelectFields = array(
        -- columns --
    );

    // query
    -- sql queries --

    // passing the columns which I want from the result set. Useful when we have not selected required fields
    $arrColumns = $arrSelectFields;

    // define the first row which will come as the first row in the csv
    $arrFirstRow = $arrSelectFields;

    // building the options array
    $options = array(
        'columns' => $arrColumns,
        'firstRow' => $arrFirstRow,
    );
    // creating the Files object from the Utility package.
    $Files = new Files;
    return $Files->convertToReportsSalesCSV($query, $options);
}
like image 965
hahahaha Avatar asked Aug 24 '15 02:08

hahahaha


1 Answers

I would probably post the keyword to my server and run the sql query again filtering those results and then create the csv/pdf

like image 106
MorningDew Avatar answered Nov 01 '22 01:11

MorningDew