问题
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