Get CSV content only, disable download

Get CSV content only, disable download

TronikTronik Posts: 122Questions: 28Answers: 1

Hi,

How can I use the csv export button to:

  • Grab the csv content into variable and send to a php script, instead of downloading the file.
  • Choose which columns to include.

Answers

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421
    edited August 2019

    You would need to use "exportOptions" and "customize".

    On this page you'll find a comment from zebral at the bottom. From the customize function you could do an ajax call passing the "csv" variable. Eventually you should be able to empty that variable so nothing gets downloaded. But there might be a different way for this.
    https://datatables.net/reference/button/csv

    In "exportOptions" return true for the columns you want to keep and false for the columns you don't want to include.

  • TronikTronik Posts: 122Questions: 28Answers: 1

    Ok Thanks will try that and get back with results.
    My initial thought was to use the ”action” parameter but I didnt find any way to prevent the download.

  • TronikTronik Posts: 122Questions: 28Answers: 1

    Hi again,

    Im using the following code, which successfully get the csv content without downloading.
    It is saved into 'data' variable as an object.

    I want to send this to a php script.

    Can I use DataTables API to create the csv file (not downloading) and send it as a file through ajax, like FormData.

    Or should I send the js object and use own php script to create csv?

    {
                extend: "csv",
                action: function ( e, dt, node, config ) {
                       
                            var data = dt.buttons.exportData();
                            
                console.log(data);
                        
                             $.ajax({
                                        type: 'POST',
                                        url: 'php/get_csv.php',
                                        data: data,
                                        success:function(response) {
                            
                                             console.log(response);      
                                       }
                              });
                 },
                filename: "file_csv",
                text: "file_csv",
                title: null,
                fieldBoundary: "",
                fieldSeparator: ";",
                exportOptions: {
                   // stripHtml: false
                }
    }
    
    
    
    
    
  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421

    I don't know to be honest. I would send whatever is easier and causes less work for you.

  • TronikTronik Posts: 122Questions: 28Answers: 1

    I solved this by creating the CSV serverside with PHP, from the POST:ed js array.

    foreach($_POST['body'] as $row){
    
    $csv_string[] = implode(';', $row);
    
    }
    
    $csv_string = implode(PHP_EOL, $csv_string);
    
    
This discussion has been closed.