【问题标题】:how to change export file name dynamic in data Tables?如何在数据表中动态更改导出文件名?
【发布时间】:2023-03-16 21:58:05
【问题描述】:
 $("#dataTable").DataTable({                    
                    dom: 'Bfrtip',
                    buttons: [
                             { extend: 'excel', text:'export to excel',title:'1'},
                    ],
})

我可以通过以下代码更改按钮的文本,但我无法获取title属性。

var table= $("#dataTable").DataTable();
tabele.button(0).text('excel');

【问题讨论】:

    标签: button datatables export filenames


    【解决方案1】:

    一旦在对象中设置并初始化数据表,它就无法更改。不过,您可以从 init 上的页面元素动态设置它。

    ("#dataTable").each( function(index) {
        var exportTitle = $("#somePageElement").text();
        $(this).DataTable({
            dom: 'Bfrtip',
            buttons: [
                {
                    extend: 'excel',
                    title: exportTitle 
                },
                {
                    extend: 'pdf',
                    title: exportTitle 
                }
            ]
        });
    

    这篇文章对如何处理这个问题也有很好的建议。 Setting up a custom file name datatables export excelHtml5 with a select text

    【讨论】:

      【解决方案2】:

      就我而言,我必须重写按钮的操作,添加一些内容来更改标题,然后调用原始按钮方法。

      在本例中扩展 CSV:

       $("#dataTable").DataTable({                    
          dom: 'Bfrtip',
          buttons: [
              {extend: 'csv', 
              text:'export to csv',
              title:'1',
              action: function(e, dt, button, config) {
              config.title = "New title";
      
              // This checks whether the HTML5 or flash version needs
              // to be called, so it's not needed if the button you are using 
              // doesn't have that distinction
              if ($.fn.dataTable.ext.buttons.csvHtml5.available( dt, config )) {
                  $.fn.dataTable.ext.buttons.csvHtml5.action(e, dt, button, config);
              }
              else {
                  $.fn.dataTable.ext.buttons.csvFlash.action(e, dt, button, config);
              }
          }}
          ]
      })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-04
        • 2013-02-27
        • 2022-01-23
        • 1970-01-01
        • 2021-12-23
        • 1970-01-01
        • 2021-07-03
        • 1970-01-01
        相关资源
        最近更新 更多