简体   繁体   English

您如何显示嵌套表?

[英]How do you display nested tables?

I have a table inside table in html as follows: 我在html表中有一个表,如下所示:

<table class="sortable draggable">
    <thead>
        <tr>
            <th class="col-salesOrderId">Order Number</th>
            <th class="col-orderDate">Date of Order</th>
            <th class="col-party">Party</th>
            <th class="col-edit">Edit</th>
            <th class="col-delete">Delete</th>
        </tr>
    </thead>
    <tbody>
        {#orders}
        <tr>
            <td class="col-salesOrderId">{.salesOrderId}</td>
            <td class="col-orderDate">{@formatDate date=orderDate format="DD-MM-YYYY" /}</td>
            <td class="col-party">{.party.partyName}</td>
            <td class="col-edit">
                <button class="btn btn-info btn-edit">
                    &nbsp;
                </button>
            </td>
            <td class="col-delete">
                <button class="btn btn-danger btn-delete">
                    &nbsp;
                </button>
            </td>
        </tr>
        <tr>
          <table class="sortable draggable row-details">
            <thead>
                <tr>
                    <th class="col-itemName">Item Name</th>
                    <th class="col-quantity">Quantity</th>
                    <th class="col-rate">Rate</th>
                    <th class="col-amount">Amount</th>
                </tr>
            </thead>
            <tbody>
                {#items}
                  <tr>
                    <td>{.item.itemName}</td>
                    <td>{.quantity}</td>
                    <td>{.rate}</td>
                      <td>{.quantity * .rate}</td>
                  </tr>
                {/items}
            </tbody>
          </table>
        </tr>
        {/orders}
    </tbody>
</table>

I get the output as shown below: 我得到的输出如下所示:

在此处输入图片说明

Why I get such an output? 为什么我得到这样的输出? I expected to see nested tables. 我希望看到嵌套表。

You need to nest the child <table> tag inside a <td> tag, not inside a <tr> tag. 您需要将子<table>标记嵌套在<td>标记内,而不是<tr>标记内。 Doing this should make it display properly, as only a <td> or <th> tag can go directly inside a <tr> tag. 这样做应该可以使其正确显示,因为只有<td><th>标记可以直接放在<tr>标记内。

<table>
    <tr>
         <td> <!-- must be in td -->
             <table> <!-- nested table -->
                   <tr>
                         <td>

                         </td>
                   </tr>
             </table>
         </td>
    </tr>
</table>

your nested table need to be inside of td or th. 您的嵌套表必须位于td或th之内。

Your HTML has several errors, starting with this: 您的HTML包含以下几个错误:

{#orders}

As others have mentioned, this is also bad: 正如其他人所提到的,这也是不好的:

<tr>↩          <table class="sortable draggable row-details"

Do yourself a big favor and start using an HTML validator like W3C's . 帮自己一个忙,开始使用像W3C的 HTML验证器。 It will find problems like this quickly. 它将很快发现类似的问题。 (It will also find other things to complain about that you might not need to fix, but when it helps, it will save a lot of time.) (它还会发现其他一些问题,使您抱怨您可能不需要修复,但是如果有帮助,它将节省大量时间。)

Also, start using the Chrome inspector to see what it's done when your markup goes haywire. 另外,开始使用Chrome检查器查看在标记变乱时所做的事情。 In this case, you can see that Chrome closed your first table, instead of nesting it. 在这种情况下,您会看到Chrome关闭了第一个表,而不是嵌套它。 When Chrome messes with your HTML like this, it's a sign you might have an error in that spot. 当Chrome像这样弄乱您的HTML时,表明您可能在该位置有错误。

  </tr></tbody></table>
                {#items}

                {/items}
            <table class="sortable draggable row-details">
            <thead>
                <tr>
                    <th class="col-itemName">Item Name</th>
                    <th class="col-quantity">Quantity</th>

The <table> tag needs to be inside <td> or <th> tag for it to be nested. <table>标记必须位于<td><th>标记内才能嵌套。 In your code, you have put the <table> tag as a child of <tr> tag which is wrong. 在您的代码中,您将<table>标记作为<tr>标记的子代放置了,这是错误的。 It should be child of <td> or <th> . 它应该是<td><th>子代。

Inserting <td> or <th> between <tr> and <table> will give the output correctly. <tr><table>之间插入<td><th> <table>将正确输出。

Here is working link for reference: Nested Tables in HTML 这是可供参考的工作链接: HTML中的嵌套表

Example: 例:

 <table border="1"> <thead> <tr> <th>Item 1 <th>Item 2 </tr> </thead> <tbody> <tr> <td> <table border="1"> <tr> <td>1 <td>2 </tr> <tr> <td>1 <td>2 </tr> </table> <td>A </tr> </tbody> </table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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