jQuery 表外的数据表导出按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35308273/
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
DataTable export button outside table
提问by void
I have multiple tables on my web page and each one is a DataTable, it is working fine.
我的网页上有多个表,每个表都是一个 DataTable,它工作正常。
I want to enable the export to excel functionality on each of the datatable but the button should be outside the table DOM (and each table should have its own button to export).
我想在每个数据表上启用导出到 excel 功能,但按钮应该在表 DOM 之外(并且每个表都应该有自己的按钮来导出)。
I can genrate the HTML5 button inside the table DOM using:
我可以使用以下方法在表格 DOM 中生成 HTML5 按钮:
$('#example').DataTable( {
dom: 'Bfrtip',
buttons: [
'copyHtml5',
'excelHtml5',
'csvHtml5',
'pdfHtml5'
]
} );
But i want someway by which i can attach a button outside the table DOM to act as a export to excel for specific table.
但我想要某种方式,通过它我可以在表格 DOM 之外附加一个按钮,作为特定表格的 excel 导出。
回答by davidkonrad
Initialize each tables buttons via a constructor, by that you can place the button elements in any container you want. If you want to place the buttons in a <div id="buttons"></div>
element outside the table, do this
通过构造函数初始化每个表格按钮,这样您就可以将按钮元素放置在您想要的任何容器中。如果要将按钮放置<div id="buttons"></div>
在表格外的元素中,请执行以下操作
var buttons = new $.fn.dataTable.Buttons(table, {
buttons: [
'copyHtml5',
'excelHtml5',
'csvHtml5',
'pdfHtml5'
]
}).container().appendTo($('#buttons'));
demo -> https://jsfiddle.net/qoqq3okg/
演示 -> https://jsfiddle.net/qoqq3okg/
I dont know your multiple tables setup, but now you just have to insert some elements along each <table>
element and inject buttons for each table into that element as described above.
我不知道您的多个表格设置,但现在您只需要沿每个<table>
元素插入一些元素,并将每个表格的按钮注入该元素,如上所述。
回答by void
You can add your class also,
您也可以添加您的课程,
buttons[0].classList.add('yourClassName');