javascript - 核心:forEach in javascript

标签 javascript html jsp foreach

我有这段代码来显示从数据库到jsp的详细信息。我正在使用 core:forEach 对结果列表进行排序。但现在为了简化 jsp 代码,我在 javascript 中实现 core:forEach。并且我在动态创建的表中显示结果。但现在在 javascript 中使用 core:forEach 时,仅显示列表中的最后一个值。代码如下。

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

最佳答案

你正在超越tablecontents每次,拿var tablecontents = ""在循环之外,然后替换 tablecontents = "<table>"tablecontents += "<table>"

window.onload = function CreateTable()
{

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

关于javascript - 核心:forEach in javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24358983/

相关文章:

html - 插入值时对齐文本区域和按钮,按钮不会向下移动

javascript - html5 audioElement 的多个实例,可以用 $(this) 完成吗?

java - 从数据库中检索值。如何从Action类设置为Action Form和JSP?

java - 警告 : Form token does not match the session token null

javascript - 在 SciTE 中测试和编辑 JavaScript(独立的和 HTML 中的)?

javascript - [node][mocha]使用 mocha 测试时无法访问全局变量

javascript - 使用 javascript 和 jQuery .each() 循环应用 CSS 转换属性时出现问题

javascript - 如何通过单击按钮使 html 文档出现在另一个 html 文档中,并对其进行样式设置

jsp - JSF 与普通的 JSP + Beans?

php - 框架头脑 Storm