javascript 在运行时修改 DataTables TableTools 默认 PDF 导出文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7516120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:22:28  来源:igfitidea点击:

Modify DataTables TableTools default PDF export filename at runtime

javascriptjquerydatatables

提问by Chris

I am using JQuery DataTables TableTools plugin and am defining a default filename for the PDF. However, I am using datatables with ajax, and have a date range selector, so the page isnt refreshed and therefore I am unable to provide a new default filename when then criteria changes.

我正在使用 JQuery DataTables TableTools 插件并为 PDF 定义默认文件名。但是,我使用带有 ajax 的数据表,并且有一个日期范围选择器,因此页面不会刷新,因此当条件更改时我无法提供新的默认文件名。

Does anyone know how I can change the default filename at runtime, after datatables has been initialized with table tools, i.e modify the config directly?

有谁知道如何在运行时更改默认文件名,在使用表工具初始化数据表后,即直接修改配置?

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

            }

回答by qais

I guess you want some dynamically generated name. Create a function that returns the (string) file name.

我猜您想要一些动态生成的名称。创建一个返回(字符串)文件名的函数。

function getCustomFileName(){ 
    var docDate = $("#from").val();
    var filter = $("#example_filter input").val();
    var oSettings = oTable.fnSettings();
    var fileName = docDate+"_"+filter;
    return fileName;
}

And use the function inside $(document).readybut outside $('#dTable').dataTable({ }).

并使用内部$(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"
                ]

            }