在使用datatables.net时导出为pdf时设置列宽 [英] set column width when exporting to pdf when using datatables.net

查看:111
本文介绍了在使用datatables.net时导出为pdf时设置列宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个表,我想导出,但一列的宽度是这么长的时间内容是短的,它使布局不好看,当它导出到pdf im使用datatables.net的代码导出时,这里是我的代码

hi i have this table that i wanted to export but the width of one column is so long when the content is only short and it makes the layout not looking good when it is exported to pdf im using the codes from datatables.net when exporting and here is my code

$(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]
            }
        }]
    });
});

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

is there a way where i can set the width of a specific column? thanks in advance

推荐答案

您可以使用自定义回调来更改PDFmakes '风格字典。如果要强制#2列为宽度为 100px 或max 100px ,请执行以下操作:

You can use the customize callback to alter PDFmakes' "style dictionary". If you want to force the #2 column to be 100px or max 100px in width, do this :

{
  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 转到样式字典部分

to PDFmake. See the documentation -> http://pdfmake.org/#/gettingstarted go for section "Style dictionaries"

这篇关于在使用datatables.net时导出为pdf时设置列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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