我需要在单个页面中导出多个数据表,并为页面上的所有表格提供一个导出按钮?
任何建议链接。 谢谢!
答案 0 :(得分:0)
是的,你可以。您需要获取所有表的对象并触发所有这些对象的导出功能。示例代码如下:
function exportAllDatatables() {
var table = $('#myTable').DataTable();
var table2 = $('#myTable2').DataTable();
//Trigger the button at index 2-1 (Wherever your export button in each table):
table.button( '2-1' ).trigger();
table2.button( '2-1' ).trigger();
}
这里引用导出按钮的触发功能: https://datatables.net/reference/api/button%28%29.trigger%28%29
答案 1 :(得分:-1)
是的,甚至可以使用按钮收集。 这是高级演示https://jsfiddle.net/4kpvba23/4/(可以同时导出两个或多个表的csv,excel,pdf)
正如Kavish Mittai所指出的,你也可以在收集中触发单键或多键。
按钮API (https://datatables.net/reference/api/button())
按钮API (https://datatables.net/reference/api/buttons())
按钮触发API (https://datatables.net/reference/api/buttons().trigger())
注意:请记住点击导出全部将提示"允许多次下载?" 在浏览器中避免有害下载,它依赖于用户 您无法禁用它的互动。
$(document).ready(function() {
var settings = {
"dom": "<'row'<'col-sm-3 col-left'l><'col-xs-6 col-right'B><'col-xs-3 col-right'f>r>t<'row'<'col-xs-3 col-left'i><'col-xs-9 col-right'p>>",
"buttons": [
'colvis',
{
extend: 'collection',
text: 'Select',
buttons: [
'selectAll',
'selectNone'
]
},
{
extend: 'collection',
text: 'Copy',
buttons: [{
extend: 'copy',
text: 'Copy Selected',
exportOptions: {
stripHtml: true,
modifier: {
selected: true
}
}
}, {
extend: 'copy',
text: 'Copy Current Page',
exportOptions: {
stripHtml: true,
modifier: {
selected: false,
page: 'current',
search: 'applied'
}
}
}]
},
{
extend: 'collection',
text: 'CSV',
buttons: [{
extend: 'csvHtml5',
text: 'Export to CSV Selected Rows',
exportOptions: {
columns: ':visible',
stripHtml: true,
modifier: {
selected: true
}
}
}, {
extend: 'csvHtml5',
text: 'Export to CSV Current Page',
exportOptions: {
columns: ':visible',
stripHtml: true,
modifier: {
selected: false,
page: 'current',
search: 'applied'
}
}
}]
},
{
extend: 'collection',
text: 'Excel',
buttons: [{
extend: 'excelHtml5',
text: 'Export to Excel Selected Rows',
exportOptions: {
columns: ':visible',
stripHtml: true,
modifier: {
selected: true
}
}
}, {
extend: 'excelHtml5',
text: 'Export to Excel Current Page',
exportOptions: {
columns: ':visible',
stripHtml: true,
modifier: {
selected: false,
page: 'current',
search: 'applied'
}
}
}]
},
{
extend: 'collection',
text: 'PDF',
buttons: [{
extend: 'pdfHtml5',
orientation: 'landscape',
pageSize: 'A4',
text: 'Export to PDF Selected Rows',
exportOptions: {
columns: ':visible',
stripHtml: true,
modifier: {
selected: true
}
}
}, {
extend: 'pdfHtml5',
orientation: 'landscape',
pageSize: 'A4',
text: 'Export to PDF Current Page',
exportOptions: {
columns: ':visible',
stripHtml: true,
modifier: {
selected: false,
page: 'current',
search: 'applied'
}
}
}]
}, {
extend: 'collection',
text: 'Print',
buttons: [{
extend: 'print',
text: 'Print Selected Rows',
orientation: 'landscape',
exportOptions: {
columns: ':visible',
stripHtml: false,
modifier: {
selected: true
}
}
}, {
extend: 'print',
text: 'Print Current Page',
orientation: 'landscape',
pageSize: 'A4',
exportOptions: {
columns: ':visible',
stripHtml: false,
modifier: {
selected: false,
page: 'current',
search: 'applied'
}
}
}]
}
]
};
var table = $('#example').DataTable(settings);
var table2 = $('#example2').DataTable(settings);
$(document).on('click', '#exportAll', function() {
/*
* For Table 1
* ===========
*/
//Index of "csv" button is 3 and in it "export to csv current page" is at index 1 so it'll be 3-1
//Index of "excel" button is 4 and in it "export to excel current page" is at index 1 so it'll be 4-1
//Index of "pdf" button is 5 and in it "export to pdf current page" is at index 1 so it'll be 5-1
table.buttons(['3-1', '4-1', '5-1']).trigger();
/*
* For Table 2
* ===========
*/
//Index of "csv" button is 3 and in it "export to csv current page" is at index 1 so it'll be 3-1
//Index of "excel" button is 4 and in it "export to excel current page" is at index 1 so it'll be 4-1
//Index of "pdf" button is 5 and in it "export to pdf current page" is at index 1 so it'll be 5-1
table2.buttons(['3-1', '4-1', '5-1']).trigger();
});
});
&#13;
#example_wrapper{
margin-bottom: 100px !important;
}
&#13;
<link href="https://cdn.datatables.net/v/dt/jszip-2.5.0/dt-1.10.16/af-2.2.2/b-1.5.0/b-colvis-1.5.0/b-flash-1.5.0/b-html5-1.5.0/b-print-1.5.0/cr-1.4.1/fc-3.2.4/fh-3.1.3/kt-2.3.2/r-2.2.1/rg-1.0.2/rr-1.2.3/sc-1.4.3/sl-1.2.4/datatables.min.css" rel="stylesheet"
/>
<button id="exportAll">Export All</button>
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
<tbody>
<tr>
<td>Herrod Chandler</td>
<td>Sales Assistant</td>
<td>San Francisco</td>
<td>59</td>
<td>2012/08/06</td>
<td>$137,500</td>
</tr>
<tr>
<td>Rhona Davidson</td>
<td>Integration Specialist</td>
<td>Tokyo</td>
<td>55</td>
<td>2010/10/14</td>
<td>$327,900</td>
</tr>
<tr>
<td>Colleen Hurst</td>
<td>Javascript Developer</td>
<td>San Francisco</td>
<td>39</td>
<td>2009/09/15</td>
<td>$205,500</td>
</tr>
<tr>
<td>Sonya Frost</td>
<td>Software Engineer</td>
<td>Edinburgh</td>
<td>23</td>
<td>2008/12/13</td>
<td>$103,600</td>
</tr>
<tr>
<td>Jena Gaines</td>
<td>Office Manager</td>
<td>London</td>
<td>30</td>
<td>2008/12/19</td>
<td>$90,560</td>
</tr>
<tr>
<td>Quinn Flynn</td>
<td>Support Lead</td>
<td>Edinburgh</td>
<td>22</td>
<td>2013/03/03</td>
<td>$342,000</td>
</tr>
<tr>
<td>Charde Marshall</td>
<td>Regional Director</td>
<td>San Francisco</td>
<td>36</td>
<td>2008/10/16</td>
<td>$470,600</td>
</tr>
<tr>
<td>Haley Kennedy</td>
<td>Senior Marketing Designer</td>
<td>London</td>
<td>43</td>
<td>2012/12/18</td>
<td>$313,500</td>
</tr>
<tr>
<td>Tatyana Fitzpatrick</td>
<td>Regional Director</td>
<td>London</td>
<td>19</td>
<td>2010/03/17</td>
<td>$385,750</td>
</tr>
<tr>
<td>Michael Silva</td>
<td>Marketing Designer</td>
<td>London</td>
<td>66</td>
<td>2012/11/27</td>
<td>$198,500</td>
</tr>
<tr>
<td>Paul Byrd</td>
<td>Chief Financial Officer (CFO)</td>
<td>New York</td>
<td>64</td>
<td>2010/06/09</td>
<td>$725,000</td>
</tr>
<tr>
<td>Gloria Little</td>
<td>Systems Administrator</td>
<td>New York</td>
<td>59</td>
<td>2009/04/10</td>
<td>$237,500</td>
</tr>
</tbody>
</table>
<table id="example2" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
<tbody>
<tr>
<td>Suki Burks</td>
<td>Developer</td>
<td>London</td>
<td>53</td>
<td>2009/10/22</td>
<td>$114,500</td>
</tr>
<tr>
<td>Prescott Bartlett</td>
<td>Technical Author</td>
<td>London</td>
<td>27</td>
<td>2011/05/07</td>
<td>$145,000</td>
</tr>
<tr>
<td>Gavin Cortez</td>
<td>Team Leader</td>
<td>San Francisco</td>
<td>22</td>
<td>2008/10/26</td>
<td>$235,500</td>
</tr>
<tr>
<td>Martena Mccray</td>
<td>Post-Sales support</td>
<td>Edinburgh</td>
<td>46</td>
<td>2011/03/09</td>
<td>$324,050</td>
</tr>
<tr>
<td>Unity Butler</td>
<td>Marketing Designer</td>
<td>San Francisco</td>
<td>47</td>
<td>2009/12/09</td>
<td>$85,675</td>
</tr>
<tr>
<td>Howard Hatfield</td>
<td>Office Manager</td>
<td>San Francisco</td>
<td>51</td>
<td>2008/12/16</td>
<td>$164,500</td>
</tr>
<tr>
<td>Hope Fuentes</td>
<td>Secretary</td>
<td>San Francisco</td>
<td>41</td>
<td>2010/02/12</td>
<td>$109,850</td>
</tr>
<tr>
<td>Vivian Harrell</td>
<td>Financial Controller</td>
<td>San Francisco</td>
<td>62</td>
<td>2009/02/14</td>
<td>$452,500</td>
</tr>
<tr>
<td>Timothy Mooney</td>
<td>Office Manager</td>
<td>London</td>
<td>37</td>
<td>2008/12/11</td>
<td>$136,200</td>
</tr>
<tr>
<td>Jackson Bradshaw</td>
<td>Director</td>
<td>New York</td>
<td>65</td>
<td>2008/09/26</td>
<td>$645,750</td>
</tr>
<tr>
<td>Olivia Liang</td>
<td>Support Engineer</td>
<td>Singapore</td>
<td>64</td>
<td>2011/02/03</td>
<td>$234,500</td>
</tr>
<tr>
<td>Bruno Nash</td>
<td>Software Engineer</td>
<td>London</td>
<td>38</td>
<td>2011/05/03</td>
<td>$163,500</td>
</tr>
<tr>
<td>Sakura Yamamoto</td>
<td>Support Engineer</td>
<td>Tokyo</td>
<td>37</td>
<td>2009/08/19</td>
<td>$139,575</td>
</tr>
<tr>
<td>Thor Walton</td>
<td>Developer</td>
<td>New York</td>
<td>61</td>
<td>2013/08/11</td>
<td>$98,540</td>
</tr>
<tr>
<td>Finn Camacho</td>
<td>Support Engineer</td>
<td>San Francisco</td>
<td>47</td>
<td>2009/07/07</td>
<td>$87,500</td>
</tr>
<tr>
<td>Zenaida Frank</td>
<td>Software Engineer</td>
<td>New York</td>
<td>63</td>
<td>2010/01/04</td>
<td>$125,250</td>
</tr>
<tr>
<td>Jennifer Acosta</td>
<td>Junior Javascript Developer</td>
<td>Edinburgh</td>
<td>43</td>
<td>2013/02/01</td>
<td>$75,650</td>
</tr>
</tbody>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/pdfmake.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/vfs_fonts.js"></script>
<script src="https://cdn.datatables.net/v/dt/jszip-2.5.0/dt-1.10.16/af-2.2.2/b-1.5.0/b-colvis-1.5.0/b-flash-1.5.0/b-html5-1.5.0/b-print-1.5.0/cr-1.4.1/fc-3.2.4/fh-3.1.3/kt-2.3.2/r-2.2.1/rg-1.0.2/rr-1.2.3/sc-1.4.3/sl-1.2.4/datatables.min.js"></script>
&#13;