建立一个自定义文件名数据表出口excelHtml5有选择文本 [英] Setting up a custom file name datatables export excelHtml5 with a select text

查看:775
本文介绍了建立一个自定义文件名数据表出口excelHtml5有选择文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何设置一个自定义的文件名中的DataTable按钮excelHtml5导出一个选择,我做了一个函数来传递的名字,但没有设置它生病后我的js code。与警报它反映了变化,但是当我调用Excel按钮,在数据表来了空。

I'm wondering how to set a custom filename to export in datatables buttons excelHtml5 with a select, I did a function to pass the name but is not setting it ill post my js code. with the alert it is reflecting the changes but when i call the excel button in datatables is coming empty.

下面是code:

var reportName = '24 afterhours ';
$('#example').DataTable({
   dom: 'Bfrtip',
   buttons: [
      {
         extend: 'excelHtml5',
         title: reportName
      },
      {
         extend: 'pdfHtml5',
         title: 'Data export'
      }
  ]
});

$('#campaing').change(function() {
   reportName += $(this).find(":selected").text() + ' report';
});

我想我可能失去了一些东西。

I think I might be missing something.

推荐答案

标题读的一次的时候了DataTable的初始化,然后值映射到内部配置对象。因此,如果你想动态更改设置,您必须更改内部配置对象,而不是试图改变只读配置设置。

title is read once when the dataTable is initialised, and then the value is mapped into the internal config object. Therefore, if you want to change settings dynamically, you must change that internal config object, not try to change the readonly configuration settings.

所以,做它周围的其他方法 - 创建为℃的事件侦听器;选择> 的内部按钮的init()回调本身。如果你有一个<选择> 可选的文件名,这样

So do it the other way around - create an event listener for the <select> inside the buttons init() callback itself. If you have a <select> with optional filenames like this

<select id="filename">
    <option value="filenameA">filename A</option>
    <option value="filenameB">filename B</option>
    <option value="filenameC">filename C</option>
</select>

然后就可以动态地改变出口文件名(== 标题 + 扩展)通过

buttons : [
   {
    extend: 'excelHtml5',
    title: 'filenameA', //default filename
    init: function(dt, node, config) {
        $("#filename").on('change', function() {
            config.title = this.value;
        })
    }
},

您可以在处理程序中改变其他配置性能,以及,例如,你可能希望改变 config.extension 到别的东西。

You can change the other config properties from within the handler as well, for example you might want change config.extension to something else.

这篇关于建立一个自定义文件名数据表出口excelHtml5有选择文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆