Export to CSV/PDF without Tabletools

Export to CSV/PDF without Tabletools

tapsboytapsboy Posts: 11Questions: 0Answers: 0
edited December 2011 in Plug-ins
So currently, Tabletools is a great plugin for Export to CSV/PDF etc. I understand flash is used in order to locally save the exported data. Now that we are moving to a Flash-free future, has someone worked on implementing the same using Javascript (Not sure though if it is even possible with the latest JS features).

Also, is there a plugin that anyone may have written to implement it from server-side, which may not use flash?

Replies

  • fbasfbas Posts: 1,089Questions: 2Answers: 0
    edited December 2011
    JS will likely never be possible for security/access restrictions, but you can pretty easily modify existing server-side code to output csv, then give the user a link to your script to retrieve the csv.

    You might add a new flag to the server_processing.php file that indicates output_mode

    [code]
    // parse new querystring variable "o" to represent output_mode
    $output_mode = (isset($_REQUEST["o"])) ? $_REQUEST["o"] : "json";
    [/code]

    and check the $output_mode flag before outputting at the end of the script.

    [code]
    if ($output_mode == "csv") {
    header("Content-type: text/csv");
    header("Cache-Control: no-store, no-cache");
    header('Content-Disposition: attachment; filename="filename.csv"'); // you can change the default file name here

    // send the aaData and aColumns headers
    echo csv_encode( $output["aaData"], $aColumns ); // EDIT: this was originally listed as $output->aaData which doesn't work on $output. it's an associative array, not "object"
    }
    else {
    // default is to output json
    echo json_encode( $output );
    }
    [/code]

    and write a csv encoding routine
    [code]
    function csv_encode($aaData, $aHeaders = NULL) {
    // output headers
    if ($aHeaders) echo implode(',', $aHeaders ) . "\r\n";

    foreach ($aaData as $aRow) {
    echo implode(',', $aRow) . "\r\n";
    }
    }[/code]


    You could add slightly more robust code to wrap data with double quotes, or escape various characters
  • tapsboytapsboy Posts: 11Questions: 0Answers: 0
    Thanks fbas...... that gets me going in the right direction
This discussion has been closed.