2011-09-22 91 views
5

我正在使用JQuery DataTables TableTools插件,並且正在爲PDF定義默認文件名。但是,我使用帶有ajax的數據表,並且有一個日期範圍選擇器,所以頁面不會刷新,因此當條件發生更改時,我無法提供新的默認文件名。修改DataTables TableTools在運行時默認PDF導出文件名

有沒有人知道我可以在運行時更改默認文件名,在datatables初始化後使用表工具,即直接修改配置?

   "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" 
       ] 

      } 

回答

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" 
       ] 

      }