2015-12-03 23 views
2

嗨我有這張表,我想導出,但一列的寬度是如此之長,當內容只是短暫的,它使佈局看起來不好時,它在導出時使用從datatables.net代碼導出爲PDF IM這裏是我的代碼當使用datatables.net導出爲pdf時設置列寬

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

是有辦法,我可以設置特定列的寬度?在此先感謝

回答

0

您可以使用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去節「風格詞典」

+0

您好我下載了pdfmake.min.js和vfs_fonts.js然後這裏是我當前的代碼'{ \t \t \t \t \t擴展:「pdfHtml5」, \t \t \t \t \t標題:d + '採購訂單', \t \t \t \t \t方向: '風景', \t \t \t \t \t每頁: '合法', \t \t \t \t \t exportOptions:{ \t \t \t \t \t列:[0,1,2,3,4,5,6] \t \t \t \t \t}, \t \t \t \t \t \t \t \t \t \t定製:功能(DOC) \t \t \t \t \t { \t \t \t \t \t doc.defaultStyle [ 'TD:第n個孩子(6)'] = { \t \t \t \t \t寬度: '500像素', \t \t \t \t \t「最大寬度':'500px' \t \t \t \t \t} \t \t \t \t \t} \t \t \t \t \t}'我不是能調整輸出壽 – BourneShady

+0

@BourneShady - 'TD:第n個孩子(我)'是不是從零開始的,它從1開始也許你有更多的運氣與'td:第n孩子(7)'...? – davidkonrad

+0

你好我有一個新的線或高度和高度跳躍的問題,它沒有在Excel的創建更新,我必須進入Excel並點擊單元格更新,即使如此寬度仍然在值的出口 –