Justin George
Justin George

Reputation: 344

core:forEach in javascript

I have this code to display details from database to jsp. I'm using core:forEach to sort through the list of results.But now inorder to simplify jsp code I'm implementing the core:forEach in javascript.And I'm displaying the results in a dynamically created table. But now while using core:forEach in javascript, only the last value in the list is getting displayed. Code is as follows.

window.onload = function CreateTable()
{
    <core:forEach items="${requestScope.projectWiseDetails}" var="row">;  //first list sorting based on project name
      var tablecontents = "";
      tablecontents = "<table>";             //dynamic table
      tablecontents += "<tr>";
      tablecontents += "<td>" + "Resource Name" + "</td>";
      tablecontents += "</tr>";
        <core:forEach items="${row.onsite3to5YearsList}" var="resource">     //second sorting which comes under project name
          tablecontents += "<tr>";
          tablecontents += "<td>" +<core:out value="${resource.resourceName}"></core:out>+ "</td>";
        </core:forEach> 
     tablecontents += "</tr>";
     tablecontents += "</table>";
  </core:forEach>
  document.getElementById("tablespace").innerHTML = tablecontents;     //attaching the table to an element


}


 <p id="tablespace"></p>  //This is where the table gets generated

Upvotes: 0

Views: 67

Answers (1)

hutchbat
hutchbat

Reputation: 796

You're overriding tablecontents everytime, take var tablecontents = "" outside the loop, then replace tablecontents = "<table>" with tablecontents += "<table>".

window.onload = function CreateTable()
{

    var tablecontents = "";
    <core:forEach items="${requestScope.projectWiseDetails}" var="row">;  //first list 
    tablecontents += "<table>";
    // The rest of your code

Upvotes: 2

Related Questions