pdfMake动态生成行

时间:2017-04-12 12:08:24

标签: angular pdfmake

伙计我正在使用pdfMake生成PDF,我必须动态生成行。我已经实现了这个仅生成表,但我不知道如何动态生成行

我的代码如下:

generateRows(payrolls){
var tempObj = {}
var tempArr = [];
for(var i=0; i<payrolls.length; i++){

   tempArr.push(
     { 
       ID: payrolls[i].wageTypeId, 
       description: payrolls[i].wageType.description,
       amount: payrolls[i].amount,
       unit: payrolls[i].unit,
       total: payrolls[i].total
      }
  );
}
return tempArr;

}

buildTableBody(data, columns) {
    var body = [];

    body.push(columns);

    data.forEach(function(row) {
        var dataRow = [];

        columns.forEach(function(column) {
            dataRow.push(row[column]);
        })

        body.push(dataRow);
    });

    return body;
}

table(data, columns) {
    return {
        table: {
            headerRows: 1,
            body: this.buildTableBody(data, columns)
        }
    };
}

enter image description here

1 个答案:

答案 0 :(得分:0)

我的代码成功,试试这个

 table(data, columns) {
    return {
      table: {
        headerRows: 1,
        body: this.buildTableBody(data, columns)
      }
    };
  }

generateRows(payrolls){
var tempObj = {}
var tempArr = [];
for(var i=0; i<payrolls.length; i++){

   tempArr.push(
     { 
       ID: payrolls[i].wageTypeId, 
       description: payrolls[i].wageType.description,
       amount: payrolls[i].amount,
       unit: payrolls[i].unit,
       total: payrolls[i].total
      }
  );
var dd = {
          content: [
            { text: 'Dynamic parts', style: 'header' },
            this.table(tempArr, ['ID', 'description', 'amount', 'unit', 'total'])
          ]
        };
}
pdfMake.createPdf(dd).download();
}

Screenshoot work in my code