我有这段代码来显示从数据库到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/