Generate rows dynamically
Hi, is there an option like generating tables dynamically to generate rows dynamically for example this array
var externalDataRetrievedFromServer = [ { name: 'Bartek', age: 34 }, { name: 'John', age: 27 }, { name: 'Elizabeth', age: 30 }, ];
It's hard to say what tables and rows you want to create from variable externalDataRetrievedFromServer, but make dynamic content is not a problem, this is unrelated to the library pdfmake. It is up to the developer and his knowledge. Sufficient to work with only the basic array, for example: https://www.w3schools.com/js/js_arrays.asp and https://www.w3schools.com/js/js_array_methods.asp, etc.
I want to achieve this layout dynamically @liborm85
It depends on the format/structure of the source data. If I assume that this is a report from the accounting system and source data have rows with each have "code", "label", "base", "percent", "sum", etc. So just make a cycle (eg. forEach) of individual rows. And add rows to table with necessary informations.
Simple "steps" example:
var sourceData = input source data from somewhere
var bodyData = [];
sourceData.forEach(function(sourceRow) {
var dataRow = [];
dataRow.push(sourceRow.code);
dataRow.push(sourceRow.label);
dataRow.push(sourceRow.base);
dataRow.push(sourceRow.percent);
dataRow.push(sourceRow.sum);
...
bodyData.push(dataRow)
});
var dd = {
content: [
{
table: {
body: bodyData
}
}
]
}
Table can be make from a lot of sources to one table and add headers between as shown in figure.
@liborm85 sorry I haven't provided the code and what I have achieved.
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)
}
};
}
from this functions I have generated this layout:
but I want to look like this:
I don't know how to achieve this
Code is good, just a little tweaks and done.
Differences there are a lot. Mainly missing headlines - I suggest add rows table gradually:
- add row with first title ( "Wages and allowances" )
- add rows with items for this.
- add row with second title ( "Deductions" )
- add rows with items for this.
- etc.
Would be good - Main array/json with headlines and and in him title and items. Generating depends on the format/structure of the source data. And how much can change the content.
This issue should be closed. I already fixed it, @liborm85 thank you for the support. Best Regards
Hi Gentios , Please send me your code
Hi @gentios can you please share this code?
Hi, sorry for the late reply. Unfortunately I do not have this code anymore I have lost access to that repository
I remember I prebuilded the arrays as I wanted to look and then with a forEach I rendered them