【问题标题】:set column width when exporting to pdf when using datatables.net使用 datatables.net 导出为 pdf 时设置列宽
【发布时间】:2015-12-03 09:08:32
【问题描述】:

嗨,我有这张表,我想导出,但是当内容很短时,一列的宽度太长了导出时,这是我的代码

$(document).ready(function() {
    var currentDate = new Date()
    var day = currentDate.getDate()
    var month = currentDate.getMonth() + 1
    var year = currentDate.getFullYear()

    var d = day + "-" + month + "-" + year;
    $('#detailTable').DataTable({
        dom: 'Bfrtip',
        buttons: [{
            extend: 'excelHtml5',
            title: d + ' Purchase Orders'
        }, {
            extend: 'csvHtml5',
            title: d + ' Purchase Orders'
        }, {
            extend: 'pdfHtml5',
            title: d + ' Purchase Orders',
            orientation: 'landscape',
            pageSize: 'LEGAL',
            exportOptions: {
                columns: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
            }
        }]
    });
});

有没有办法可以设置特定列的宽度?提前谢谢

【问题讨论】:

    标签: pdf datatables


    【解决方案1】:

    您可以使用 customize 回调来更改 PDFmakes 的“样式字典”。如果您想强制 #2 列的宽度为 100px 或最大 100px,请执行以下操作:

    {
      extend: 'pdfHtml5',
      title: d + ' Purchase Orders',
      orientation: 'landscape',
      pageSize: 'LEGAL',
      exportOptions: {
         columns: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
      },
      //-------------------------- 
      customize : function(doc) {
         doc.styles['td:nth-child(2)'] = { 
           width: '100px',
           'max-width': '100px'
         }
      }
    }
    

    这将通过 CSS 样式规则

    td:nth-child(2) {
      width: 100px;
      max-width: 100px;
    }
    

    到 PDFmake。请参阅文档 -> http://pdfmake.org/#/gettingstarted 转到“样式字典”部分

    【讨论】:

    • 嗨,我下载了 pdfmake.min.js 和 vfs_fonts.js 然后这是我当前的代码{ extend: 'pdfHtml5', title: d + ' Purchase Orders', orientation: 'landscape', pageSize: 'LEGAL', exportOptions: { columns: [0, 1, 2, 3, 4, 5, 6] }, customize : function(doc) { doc.defaultStyle ['td:nth-child(6)'] = { width: '500px', 'max-width': '500px' } } } 我无法调整输出虽然
    • @BourneShady - td:nth-child(i) 不是从零开始的,它是从 1 开始的。也许你对 td:nth-child(7) 更有运气...?
    猜你喜欢
    • 1970-01-01
    • 2019-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    • 1970-01-01
    • 2019-06-23
    • 2014-08-11
    相关资源
    最近更新 更多