2017-05-08 132 views
0

我有一些數據,我想要顯示它。 telnumbers和'teltype`必須位於其所有者的一列中。如何正確顯示錶格?

我有這樣的:

enter image description here

,這我的代碼:

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
 
<div xmlns:jsp="http://java.sun.com/JSP/Page" 
 
    xmlns:c="http://java.sun.com/jsp/jstl/core" 
 
    xmlns:spring="http://www.springframework.org/tags" 
 
    version="2.0"> 
 
    <jsp:directive.page contentType="text/html;charset=UTF-8"/> 
 
    <jsp:output omit-xml-declaration="yes"/> 
 

 
    <style> 
 
     table, th, td { 
 
      border: 1px solid black; 
 
     } 
 
    </style> 
 

 
    <h1>Contact list</h1> 
 

 
    <c:if test="${not empty contacts}"> 
 
     <table> 
 
      <thead> 
 
      <tr> 
 
       <th>First Name</th> 
 
       <th>Last Name</th> 
 
       <th>Birth Date</th> 
 
       <th>tel_type</th> 
 
       <th>tel_number</th> 
 
       <th>hobby</th> 
 
      </tr> 
 
      </thead> 
 
      <tbody> 
 
      <c:forEach items="${contacts}" var="contact"> 
 
       <jsp:useBean id="contact" scope="page" type="org.training.support.model.Contact"/> 
 
       <tr> 
 
       <td>"${contact.firstName}"</td> 
 
       <td>"${contact.lastName}"</td> 
 
       <td>"${contact.birthDate}"</td> 
 
        <c:forEach items="${contact.contactDetails}" var="telDetail"> 
 
         <jsp:useBean id="telDetail" scope="page" type="org.training.support.model.ContactTelDetail"/> 
 
          <td>"${telDetail.telNumber}"</td> 
 
          <td>"${telDetail.telType}"</td> 
 
        </c:forEach> 
 
        <c:forEach items="${contact.hobbies}" var="hobby"> 
 
         <jsp:useBean id="hobby" scope="page" type="org.training.support.model.Hobby"/> 
 
          <td>"${hobby.id}"</td> 
 
        </c:forEach> 
 
       </tr> 
 
      </c:forEach> 
 
      </tbody> 
 
     </table> 
 
    </c:if> 
 
</div>

+0

請不要爲每個數據使用TD。使用一些分隔符代替使用2 td – 2017-05-08 03:15:55

+0

我認爲這會幫助你。檢查「跨越兩列的單元格」。 https://www.w3schools.com/html/tryit.asp?filename=tryhtml_table_span。如果沒有幫助,請再次提問 – 2017-05-08 03:55:13

+0

對不起,但我不知道,我怎麼可以讓我的例子隨機colspan(這個很好的例子,但在我的情況下,它不工作,導致contact.contactDetails.size()未定義(隨機)( –

回答

1

在這裏,我已經添加了相關的代碼你的解決方案請在JSP中添加以下標籤。

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 

這裏是改變

<h1>Contact list</h1> 

<c:if test="${not empty contacts}"> 
    <table> 
     <thead> 
     <tr> 
      <th>First Name</th> 
      <th>Last Name</th> 
      <th>Birth Date</th> 
      <th colspan="${fn:length(contact.contactDetails)}">tel_type & tel_number</th> 
      <th colspan="${fn:length(contact.hobbies)}">hobby</th> 
     </tr> 
     </thead> 
     <tbody> 
     <c:forEach items="${contacts}" var="contact"> 
      <jsp:useBean id="contact" scope="page" type="org.training.support.model.Contact"/> 
      <tr> 
      <td>"${contact.firstName}"</td> 
      <td>"${contact.lastName}"</td> 
      <td>"${contact.birthDate}"</td> 
       <c:forEach items="${contact.contactDetails}" var="telDetail"> 
        <jsp:useBean id="telDetail" scope="page" type="org.training.support.model.ContactTelDetail"/> 
         <td>"${telDetail.telType}" - "${telDetail.telNumber}"</td>       
       </c:forEach> 
       <c:forEach items="${contact.hobbies}" var="hobby"> 
        <jsp:useBean id="hobby" scope="page" type="org.training.support.model.Hobby"/> 
         <td>"${hobby.id}"</td> 
       </c:forEach> 
      </tr> 
     </c:forEach> 
     </tbody> 
    </table> 
</c:if> 

而結果將是繼圖像

Example result

+0

哦,這更好。我正在尋找一些這樣的 –

0

對於一個表來正確顯示的 '日' 的數量必須匹配「tr」內的'td'數量如下:

<table> 
    <thead> 
    <tr> 
     <th>"Header 1"</th> 
     <th>"Header 2"</th> 
     <th>"Header 3"</th> 
    </tr> 
    </thead> 
    <tbody> 
    <tr> 
     <td> "Value1"</td> 
     <td> "Value2"</td> 
     <td> "Value3"</td> 
    </tr> 
    </tbody> 

在你的情況,而無需查看在你的表是很難說,但因爲使用的是:

<c:forEach items="${contact.contactDetails}" var="telDetail">

是給你超過6個TD在你的頭#可能是由於您的表中的值,檢查您是否有空值或空值。嘗試消除空值,並在這些字段上留下單個空間,然後再次嘗試或用一個for循環替換多個foreach,並按索引來尋址每個數組。

例如:

<c:if test="${not empty contacts}"> 
    <table> 
     <thead> 
     <tr> 
      <th>First Name</th> 
      <th>Last Name</th> 
      <th>Birth Date</th> 
      <th>tel_type</th> 
      <th>tel_number</th> 
      <th>hobby</th> 
     </tr> 
     </thead> 
     <tbody> 
     <c:forEach begin="0" end="${fn:length(contacts)}" var="i"> 
      <jsp:useBean id="contact" scope="page" type="org.training.support.model.Contact"/> 
      <tr> 
       <td>"${contacts[i].firstName}"</td> 
       <td>"${contacts[i].lastName}"</td> 
       <td>"${contacts[i].birthDate}"</td> 
       <td>"${contact.contactDetails[i].telNumber}"</td> 
       <td>"${contact.contactDetails[i].telType}"</td> 
       <td>"${contact.hobbies[i].id}"</td> 
      </tr> 
     </c:forEach> 
     </tbody> 
    </table> 
</c:if> 

確保您加入這個標籤庫在頁面的頂部,使FN的大小

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 
+0

你能幫我意識到你的意思嗎?我嘗試做一些不同的例子,但沒有成功( –

+0

我增加了一個我提出的例子,我怎麼不能測試它,因爲我沒有你的收藏或表格,但讓我知道,如果有人發現一個錯誤讓我知道,我只是想幫助 – AbelSurace

+0

噢我的天)我忘了
))但是,謝謝 –