pdfMake generate rows dynamically

丶灬走出姿态 提交于 2021-02-08 09:58:59

问题


guys I am using pdfMake to generate PDF's, and I have to generate rows dynamically. I have achieved this only generating table but I don't know how to generate rows dynamically

my code looks like this:

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)
        }
    };
}


回答1:


Success in my code, try this

 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



来源:https://stackoverflow.com/questions/43369114/pdfmake-generate-rows-dynamically

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