TableTools and server side
TableTools and server side
mf_a2if
Posts: 32Questions: 0Answers: 0
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
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
This discussion has been closed.
Replies
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
I'll try like it, and I come back if I have any problems
Magali
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
[code] if (isset($_GET['filter1'])) $csv .= $_GET['filter1'];[/code]
See http://php.net/manual/en/function.isset.php
Steph
Thank you very much.
Magali
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
Thank you
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.
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