What is the best way to display all the selected details in a table format?

I am currently using PDFMake to create detailed profiles for each person I select in my table.

Check out the sample below:

https://i.sstatic.net/eFLDs.png

For every selection I make, I need to produce a separate document with their information in a PDF using PDFMake, along with their QR Code. I have successfully printed this in my console:

https://i.sstatic.net/WcHKA.png

Here is the code snippet for that:

var docDefinition;
  for (var vin = 0; vin < this.selected.length; vin++) {
    var nestedArr = new Array(this.selected);

    var s = nestedArr[0][vin].LAST_M + "\n" + nestedArr[0][vin].MEMB_N;
    console.log(s);

    docDefinition = {
      pageSize: "A6",
      pageMargins: [0, 0, 0, 0],
      content: { text: s }
    };
  }

  pdfMake.createPdf(docDefinition).print();
},

However, when it comes to generating the PDFs with their details, only the information of the last selected person is being displayed, not all the people I selected. This can be seen in the image below:

https://i.sstatic.net/iBlcO.png

I believe the issue lies within the docDefinition because it creates a new instance every time I generate a PDF and only captures the data from the last ticked person.

How can I go about solving this problem?

Answer №1

Your docDefinition gets overwritten in each iteration of the for loop, causing it to only store the last value. To fix this issue, you can modify your code as follows:

var docDefinition;
var text = []
for (var vin = 0; vin < this.selected.length; vin++) {
  var nestedArr = new Array(this.selected);

  var s = nestedArr[0][vin].LAST_M + "\n" + nestedArr[0][vin].MEMB_N;
  text.push(s)
}
docDefinition = {
  pageSize: "A6",
  pageMargins: [0, 0, 0, 0],
  content: { text: text.join("\n") }
};
pdfMake.createPdf(docDefinition).print();

Answer №2

This clever solution did the trick for me. I found the code on a helpful Stack Overflow question and also on GitHub.

 var docDefinition;
  var text = [];
  for (var vin = 0; vin < this.selected.length; vin++) {
    var nestedArr = new Array(this.selected);

    var s = nestedArr[0][vin].LAST_M + "\n" + nestedArr[0][vin].MEMB_N;
    text.push(s);
  }

  var docDefinition = {
    content: text.map(function(item) {
      return { text: item, pageBreak: "after" };
    })
  };
  pdfMake.createPdf(docDefinition).print();

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The navigation for both the Bootstrap 4 Card Slider and Half Slider is identical

I've noticed an issue with my Card slider and half slider. Whenever I navigate through the Card slider, the half slider also moves along with it. However, this is not the case when navigating the half slider; the Card slider remains stationary. It see ...

Troubleshooting Problem: Difficulty accessing Controller in AngularJS Module

I am facing difficulties with communication between my application and a module that I have developed. Below is the AngularJS module that I created. (function (document, window) { 'use strict'; var piCart = angular.module('piCart& ...

What is the alternative method of invoking a function within another function in React Native without utilizing a constructor?

Within my RegisterTaster function, I need to execute another function called endRegisterAlert. Since I'm not using a constructor and instead treating the class as a const in React Native, how can I achieve this? What is the best way to call the endRe ...

Retrieving information from a datatable in vb.net with an array

Working on a chart using highcharts with code behind in vb.net... I have a datatable structured like this: Date - speed - data 2011 10k 6 2011 18k 7 2012 20k 10 2012 10k 2 2013 14k 4 2013 20k 6 Previously, to ...

Conceal scroll bar within web browser view

Is there a way to remove the scrollbar on Android Chrome/Chromium by utilizing CSS or Javascript? I attempted: document.documentElement.style.overflow = 'hidden'; Although this solution works for Chrome on Windows, it doesn't have any eff ...

Can you explain the role of the next() function in middleware/routes following the app.use(express.static(...)) in Express

When dealing with serving static assets generated from React source code using npm run build, the following method can be used: app.use('/', express.static(path.join(__dirname, 'apps', 'home', 'build'))) To protect ...

Trigger an alert message upon loading the HTML page with search text

I need to search for specific text on a webpage and receive an alert if the text is found. <script type='text/javascript'> window.onload = function() { if ((document.documentElement.textContent || document.documentElement.innerText ...

Incorporating a string at the end of a URL using Nuxt's dynamic routing method

For the purpose of improving SEO, I am looking to append a string (such as a product title or category title) at the end of my dynamic route. My product page structure is set up like this: Pages: product (folder) - _id.vue Currently, to display a single ...

Show JSON array items

My php file (history.php) generates a JSON object $i=1; $q=mysql_query("select * from participants where phone='".mysql_real_escape_string($_GET['phone'])."' limit 10"); while($rs=mysql_fetch_array($q)){ $response[$i] = $rs[&ap ...

Error: The property 'postID' can not be read because it is undefined

I'm new to programming and I am working on creating a news/forum site as a practice project. I have set up a route /post/postID/postTitle to view individual posts. Initially, when I used only :postID, it worked fine. But after adding :postTitle, whene ...

Struggling to comprehend the node.js walker concept

I am struggling to grasp the concept of how the signature/header of the node.js walker functions. I understand that a walker can go through a directory and apply filters, but I'm unsure about how the signature of the .on function works. For example: ...

What are the steps for establishing a connection to Heroku using node-mongodb-native?

I'm feeling lost when it comes to connecting to MongoLab on Heroku. I came across an example that was supposed to help, but it just left me more confused. You can check it out here. After looking at both the web.js and deep.js files, I noticed they b ...

Utilizing the output of a callback function to execute res.render in a NodeJS application

Currently, I am utilizing oracledb for node in order to retrieve data from the database. Once the data is fetched, my goal is to transmit it to the client side using render() in express JS. Below is an example of the code structure: config.js module.expo ...

Determining the Deleted Tag in Bootstarp Vue Form Tags: A Guide to Identifying Removed Tags in the List

Hey there! I am a Vue newbie and I have a question regarding the Form Tags. By default, the Form-tag list is filled with all categories, but if a user removes any category (tag) from the list, I want to know which one was removed. I looked through the Boo ...

Methods for presenting a list of textboxes representing a price list when a checkbox is clicked using CSS

When updating the supplier and store, the item list dynamically populates. When a user clicks on an item they want to order, how can I display the quantity textbox to the left of the checkbox using CSS? Below is the code snippet for items and ordered quan ...

Is it possible to create diagrams using Vue.js?

Is there a way to create a diagram on the web and then render it as a json string? I'm looking for an npm plugin or any other idea that might help with this. Any suggestions? ...

Ways to incorporate a dynamic value into a chart created with chart.js?

I want to create a doughnut chart representing the number of individuals who have tested positive for Coronavirus and the number of deaths related to it. How can I transfer the same data from the top container into the chart? The confirmedCases and deaths ...

Preventing default behavior in a JQuery post request

Encountering an issue with jQuery functionality on a mobile device. function send() { $.post("scripts/post.php", { username: $("input[name=username]").val(), password: $("input[name=password]").val() }, function(data) { if ($(".data div" ...

Determine in React whether all values in an array of objects are false and then return true

Is there a way to determine if all objects in an array are false in React? Below is the structure of my array: [{"filter":"ASIA","key":0,"isChecked":false},{"filter":"INDIA","key":1,"isChecked":false},{"filter":"MANDYA","key":2,"isChecked":false},{"filter ...

Using Regex with JavaScript while ignoring letter case

I am trying to extract a query string from my URL using JavaScript, and I need to perform a case-insensitive comparison for the query string name. In order to achieve this, here is the code snippet that I am currently using: var results = new RegExp(&apos ...