Align one column with pdfmake and datatables

与世无争的帅哥 提交于 2019-12-10 15:14:55

问题


Trying to align 1 column to the left with the other columns centered in PDFMAKE using a dynamically generated mysql table and datatables, the code i have works if you disable pagination, but with pagination it only justify's the first 25 rows that are displayed on the screen, any ideas?

$(document).ready(function() {
$("#loops").DataTable( {
  dom:
  "<'row'<'col-sm-3'l><'col-sm-6 text-center'B><'col-sm-3'f>>" +
  "<'row'<'col-sm-12'tr>>" +
  "<'row'<'col-sm-5'i><'col-sm-7'p>>",
  buttons: [
    {
      extend: 'pdfHtml5',
      pagesize: 'A3',
      text: 'Export to PDF',
      orientation: 'landscape',
      customize: function (doc) {
        var rowCount = document.getElementById("loops").rows.length;
            for (i = 0; i < rowCount+1; i++) {
                doc.content[1].table.body[i][5].alignment = 'left';
            };
        doc.styles.tableHeader.fontSize = 7;
        doc.defaultStyle.fontSize = 7;
        doc.content[1].table.widths = [60, 100, 70, 70, 60, '*', 90, 20, 20]
        doc.styles.tableBodyEven.alignment = 'center';
        doc.styles.tableBodyEven.noWrap = true;
        doc.styles.tableBodyOdd.alignment = 'center';
        doc.styles.tableBodyOdd.noWrap = true;
        doc.styles.tableBodyOdd.fillColor = '#f3f3f3';
      },
    },
  'copy', 'excel'
  ]
} );
} );

回答1:


Solved this by changing the code as follows

var rowCount = doc.content[1].table.body.length;
 for (i = 0; i < rowCount+1; i++) {
  doc.content[1].table.body[i][5].alignment = 'left';
};

the above code will change the fifth column to left justified. If you want to leave the header centered use

var rowCount = doc.content[1].table.body.length;
 for (i = 1; i < rowCount; i++) {
  doc.content[1].table.body[i][5].alignment = 'left';
};


来源:https://stackoverflow.com/questions/47894339/align-one-column-with-pdfmake-and-datatables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!