伙计我正在使用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)
}
};
}
答案 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();
}