TableTools and server side

TableTools and server side

mf_a2ifmf_a2if Posts: 32Questions: 0Answers: 0
edited January 2013 in TableTools
Hi everybody,

I use Table tools like this :

[code]
TableTools.BUTTONS.download = {
"sAction": "text",
"sTag": "default",
"sFieldBoundary": "",
"sFieldSeperator": "\t",
"sNewLine": "
",
"sToolTip": "",
"sButtonClass": "DTTT_button_text",
"sButtonClassHover": "DTTT_button_text_hover",
"sButtonText": "Download",
"mColumns": "all",
"bHeader": true,
"bFooter": true,
"sDiv": "",
"fnMouseover": null,
"fnMouseout": null,
"fnClick": function( nButton, oConfig ) {
var oParams = this.s.dt.oApi._fnAjaxParameters( this.s.dt );
var iframe = document.createElement('iframe');
iframe.style.height = "0px";
iframe.style.width = "0px";
iframe.src = oConfig.sUrl+"?"+$.param(oParams);
document.body.appendChild( iframe );
},
"fnSelect": null,
"fnComplete": null,
"fnInit": null
};

$(document).ready(function() {
// Affichage du tableau avec le détail des entrées
$('#tabDetail').dataTable({
"bProcessing": true,
"bServerSide": true,
"sDom": 'T<"clear">lfrtip',
"oTableTools": {
"aButtons": [{
"sExtends": "download",
"sButtonText": "Download CSV",
"sUrl": "generate_csv.php"
}]
},
"sAjaxSource": "serverSide_general.php",
"fnServerData": function( sUrl, aoData, fnCallback ) {
$.ajax( {
"url": sUrl,
"data": aoData,
"success": fnCallback,
"dataType": "json",
"cache": false
} );
}
})
[/code]

And my generate_csv.php :
[code]
<?php
header('Content-Type: application/csv-tab-delimited-table');
header('Content-Disposition: attachment');

$csv = '"Usager";"Prénom";"Prénom";"Prénom";"Prénom";"Prénom";"Email"'."\n";

echo json_encode($csv);

exit;
?>
[/code]

But, when i click on Download CSV, there's nothing happened.

Any idea ?

Magali

Replies

  • dwbiz05dwbiz05 Posts: 7Questions: 0Answers: 0
    I may be wrong, but I don't think you want to json_encode your information.

    I think all you want to do is print (echo) out your data.

    This is not pulling data from an ajax call, it's loading it into a new iframe created when the button is clicked so you can download it as a file.

    Hope that helps.

    Dave
  • mf_a2ifmf_a2if Posts: 32Questions: 0Answers: 0
    edited January 2013
    Ok, thank you for your response.

    I'll try like it, and I come back if I have any problems

    Magali
  • mf_a2ifmf_a2if Posts: 32Questions: 0Answers: 0
    Ok, that's right, it's working.

    Have you an idea, how can i send to my .php if my datas are filtering or not, and donwload datatable with filter ?

    If in my .php i do [quote]if($_GET['filter1'] != "") $csv .= $_GET[filter1];
    [/quote]

    I have an error like [quote]$_GET['filter1'] Undeffined[/quote].

    How send to the .php that I want use filter, or not ?

    Magali
  • essexstephessexsteph Posts: 57Questions: 0Answers: 0
    edited January 2013
    Try using

    [code] if (isset($_GET['filter1'])) $csv .= $_GET['filter1'];[/code]

    See http://php.net/manual/en/function.isset.php

    Steph
  • mf_a2ifmf_a2if Posts: 32Questions: 0Answers: 0
    Yes that's work.

    Thank you very much.

    Magali
  • mf_a2ifmf_a2if Posts: 32Questions: 0Answers: 0
    Hi,

    it's me again :)

    I can get my filters, all it's work, but for some reason, my date range is void however it's working whan I filtering datatable, and I can see it in Firebug.

    Have you an idea ?

    Magali
  • mf_a2ifmf_a2if Posts: 32Questions: 0Answers: 0
    No problem, with POST I can get them.

    Thank you
  • lucaslodiborgeslucaslodiborges Posts: 1Questions: 0Answers: 0
    Hi .. can someone please help me to implement export to excel server side by the Love of God?

    I'm tired of seeing articles and more items that do not lead anywhere.

    Can you help me step by step how to make the export work?

    Thank you.
  • essexstephessexsteph Posts: 57Questions: 0Answers: 0
    The trouble is that making a proper Excel file is non-trivial, whilst making a CSV is. So as Allan has pointed out elsewhere the client-side export to Excel button actually makes CSV (see http://datatables.net/forums/discussion/4043/export-to-excel-wrong-extention-.csv.).

    mf_a2if's original post above shows you how to do the javascript calling a csv generation file on the server. The server side generation file needs to stream the CSV with the headers set correctly. There's lots of articles on the web about how to do it, here's one to get you started http://ran.ge/2009/10/27/howto-create-stream-csv-php.

    Steph
This discussion has been closed.