我正在使用JQuery DataTables TableTools插件,并定义了PDF的默认文件名。但是,我使用带有ajax的数据表,并且有一个日期范围选择器,因此页面没有刷新,因此当条件更改时,我无法提供新的默认文件名。
有没有人知道在使用表格工具初始化数据表之后如何在运行时更改默认文件名,即直接修改配置?
"oTableTools": {
"sSwfPath": "js/DataTables/copy_cvs_xls_pdf.swf",
"aButtons": [
"copy",
"csv",
"xls",
{
"sExtends": "pdf",
"sTitle": "Report Name",
"sPdfMessage": "Summary Info",
"sFileName": "<?php print('How do i use jquery to change this after the table has been initialized'); ?>.pdf",
"sPdfOrientation": "landscape"
},
"print"
]
}
答案 0 :(得分:9)
我想你想要一些动态生成的名字。创建一个返回(字符串)文件名的函数。
function getCustomFileName(){
var docDate = $("#from").val();
var filter = $("#example_filter input").val();
var oSettings = oTable.fnSettings();
var fileName = docDate+"_"+filter;
return fileName;
}
并使用$(document).ready
内的功能,但在$('#dTable').dataTable({ })
之外。
"oTableTools": {
"sSwfPath": "js/DataTables/copy_cvs_xls_pdf.swf",
"aButtons": [
"copy",
"csv",
"xls",
{
"sExtends": "pdf",
"sTitle": "Report Name",
"sPdfMessage": "Summary Info",
"sPdfOrientation": "landscape"
"fnClick": function( nButton, oConfig, flash )
{
customName = getCustomFileName()+".pdf";
flash.setFileName( customName );
this.fnSetText( flash,
"title:"+ this.fnGetTitle(oConfig) +"\n"+
"message:"+ oConfig.sPdfMessage +"\n"+
"colWidth:"+ this.fnCalcColRatios(oConfig) +"\n"+
"orientation:"+ oConfig.sPdfOrientation +"\n"+
"size:"+ oConfig.sPdfSize +"\n"+
"--/TableToolsOpts--\n" +
this.fnGetTableData(oConfig)
);
}
},
"print"
]
}