【问题标题】:DataTables + PDFmake = color PDF table cells?DataTables + PDFmake = 颜色 PDF 表格单元格?
【发布时间】:2018-10-05 01:00:55
【问题描述】:

我正在尝试从下面的数组表中打印一个 PDF 文件,其中每个带有“软件工程师”一词的单元格都是蓝色的,而带有“伦敦”一词的单元格是红色的。想通过循环来执行此操作,一一扫描所有行或列(不是特定的行或列)。 但是我根本无法让它工作,并开始放弃。我猜它与 each() 方法有关?

each()

Table I am using

var editor; // use a global for the submit and return data rendering in the examples

$(document).ready(function() {
    editor = new $.fn.dataTable.Editor( {
        ajax: "../php/staff.php",
        table: "#example",
        fields: [ {
                label: "First name:",
                name: "first_name"
            }, {
                label: "Last name:",
                name: "last_name"
            }, {
                label: "Position:",
                name: "position"
            }, {
                label: "Office:",
                name: "office"
            }, {
                label: "Extension:",
                name: "extn"
            }, {
                label: "Start date:",
                name: "start_date",
                type: "datetime"
            }, {
                label: "Salary:",
                name: "salary"
            }
        ]
    } );

    $('#example').DataTable( {
        dom: "Bfrtip",
        ajax: "../php/staff.php",
        columns: [
            { data: null, render: function ( data, type, row ) {
                // Combine the first and last names into a single table field
                return data.first_name+' '+data.last_name;
            } },
            { data: "position" },
            { data: "office" },
            { data: "extn" },
            { data: "start_date" },
            { data: "salary", render: $.fn.dataTable.render.number( ',', '.', 0, '$' ) }
        ],
        select: true,
        buttons: [
            { extend: "create", editor: editor },
            { extend: "edit",   editor: editor },
            { extend: "remove", editor: editor }
        ]
    } );
} );

【问题讨论】:

    标签: loops datatables each pdfmake


    【解决方案1】:

    看看this 示例,如果年龄低于 40 岁,它会更改 PDF 中的单元格。这样的方法也适合您。

    代码的关键部分是这样的:

    buttons: [
      {
        extend: "pdfHtml5",
        customize: function(doc) {
          age = table.column(3).data().toArray();
          for (var i = 0; i < age.length; i++) {
            if (age[i] < 40) {
              doc.content[1].table.body[i+1][3].fillColor = 'blue';
            }
          }
        }
      }
    ]
    

    【讨论】:

    • 很好,它有效!如果我想用另一个 for 循环遍历所有单元格,如何从表中提取列数?我不想使用 for (var column = 0; column
    • table.columns().count() 会告诉您有多少列,您可以将其用作for 循环中的边界
    【解决方案2】:
    doc.content[1].table.body[0].forEach(function (h) {
            h.fillColor = '#000';
            alignment: 'center'
        });
     
        doc.styles.title = {
            color: '#2D1D10',
            fontSize: '16',
            alignment: 'center'
        }
    

    会做好的

    【讨论】:

      猜你喜欢
      • 2020-07-02
      • 2015-01-24
      • 2019-11-16
      • 2014-12-31
      • 1970-01-01
      • 1970-01-01
      • 2020-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多