如何在jQuery中导出DataTables时将所有表头转为大写?
How to Convert DataTables Headers to Uppercase on Export
Got it, let's get your DataTables headers showing up in uppercase whenever you export data (PDF, CSV, Excel, Copy). The trick is using the header callback function in DataTables' exportOptions—this lets you tweak the header text right before it's added to the exported content.
Updated Code for Your Configuration
Here's your modified DataTables setup with the necessary changes applied to all export buttons:
$('#tableFormData').DataTable({ "stateSave": true, dom: 'Bfrtip', buttons: [ { extend: 'pdfHtml5', title: 'Form Data.', orientation: 'landscape', pageSize: 'A4', pageMargins: [0, 0, 0, 0], margin: [0, 0, 0, 0], text: '<u>E</u>xport Page (PDF)', key: { key: 'e', altKey: false }, content: [{ style: 'fullWidth' }], styles: { fullWidth: { fontSize: 18, bold: true, alignment: 'right', margin: [0, 0, 0, 0] } }, download: 'download', exportOptions: { modifier: { alignment: 'center' }, // Convert PDF export headers to uppercase header: function (data) { return data.toUpperCase(); }, columnGap: 1 } }, { extend: 'copy', exportOptions: { header: function (data) { return data.toUpperCase(); } } }, { extend: 'csv', exportOptions: { header: function (data) { return data.toUpperCase(); } } }, { extend: 'excel', exportOptions: { header: function (data) { return data.toUpperCase(); } } } ] });
Quick Breakdown
- The
headercallback insideexportOptionstakes the original header text (data), converts it to uppercase withtoUpperCase(), and returns the modified text. - We added this callback to each export button to ensure all formats (PDF, Copy, CSV, Excel) have uppercase headers. If you want to avoid repeating this code for every button, you can set global export options instead:
$('#tableFormData').DataTable({ "stateSave": true, dom: 'Bfrtip', // Global settings for all export buttons buttons: { exportOptions: { header: function (data) { return data.toUpperCase(); } }, buttons: [ // Insert your original button configurations here (pdfHtml5, copy, csv, excel) ] } });
This global setup will apply the uppercase header rule to every export button automatically, so you don't have to duplicate the logic.
内容的提问来源于stack exchange,提问作者Pravin Durge
相关产品推荐
相关产品推荐

