pdfmake icon indicating copy to clipboard operation
pdfmake copied to clipboard

Generate rows dynamically

Open gentios opened this issue 9 years ago • 9 comments

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 }, ];

gentios avatar Apr 12 '17 11:04 gentios

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.

liborm85 avatar Apr 12 '17 12:04 liborm85

rows

I want to achieve this layout dynamically @liborm85

gentios avatar Apr 12 '17 12:04 gentios

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 avatar Apr 12 '17 13:04 liborm85

@liborm85 sorry I haven't provided the code and what I have achieved. generatedarray

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:
tablegenerated

but I want to look like this:

rows

I don't know how to achieve this

gentios avatar Apr 12 '17 13:04 gentios

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.

liborm85 avatar Apr 13 '17 09:04 liborm85

This issue should be closed. I already fixed it, @liborm85 thank you for the support. Best Regards

gentios avatar Apr 13 '17 10:04 gentios

Hi Gentios , Please send me your code

PrabuGovindan avatar Nov 09 '17 08:11 PrabuGovindan

Hi @gentios can you please share this code?

rikrishiorg avatar Jan 03 '19 11:01 rikrishiorg

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

gentios avatar May 10 '20 11:05 gentios