简体   繁体   中英

How add a list variable in html string?

Currently I am using an html string to create a pdf. In that html string I need to pass a list variable. The problem is I need to create a table using that list variable. But when I try to assign it to a variable inside the script parameter in the html string its not getting assigned.

let amountList= [
      { name: 'store Amount', amount: 0 },
      { name: 'reinventAmount', amount: 0 },
      { name: 'Cost Amount', amount: 90 },
      { name: 'Total Amount', amount: 90 }
    ];


let htmlStringUsedforPdf= `<html>
<body>
  <table>
      <thead>
     </thead>
     <tbody class="sub-text" id="testBody"></tbody>
    </table>
  <script>
      
  
    function loadTableData(items) {
      const table = document.getElementById("testBody");
     
    
      items.forEach( item => {
        let row = table.insertRow();
        let name= row.insertCell(0);
        name.innerHTML = item.name;
        let amount= row.insertCell(1);
        amount.innerHTML = item.amount;
      });
    }
    loadTableData(${amountList});
    
  </script>

<body>
</html>`

The table is created without the amountList data. 

Was able to sort out the problem by stringifying the list. Seems like it was not getting assigned because html was a string. Solution was

amountList = JSON.stringify(amountList)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM