【问题标题】:Nested table tag in htmlhtml中的嵌套表格标签
【发布时间】:2019-11-26 03:47:45
【问题描述】:

每当用户单击父单元格时,我都会尝试显示子表。这是我的代码 sn-p。它仅适用于第二条记录之后的第一行,它没有将数据加载到表中。 (显示纯数据)任何很棒的帮助。谢谢!

<table class="table table-borderless table-hover">
        <thead>
            <tr>
                <th>&nbsp;</th>
                <th scope="col">student.Id</th>
                .............
            </tr>
        </thead>
        <tbody>
        @foreach($data as $student)
            <tr class="clickable" data-toggle="collapse" id="row{{ $student['id'] }}" data-target=".row{{ $student['id'] }}">
                <td>{{ $student['id'] }}</td>
            </tr>
            @if ($student['subjects'])
              <tr>
               <td colspan="3">
                <table class="table table-sm table-dark collapse row{{ $student['id'] }}">
                    <thead>
                        <tr>
                            <th scope="col">subject.Id</th>
                             ..............
                        </tr>
                    </thead>
                    <tbody>
                    @foreach($student['subjects'] as $subject)
                        <tr>
                            <td>{{$subject['id']}}</td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </td>
            </tr>
            @endif
        @endforeach
        </tbody>
    </table>

【问题讨论】:

  • &lt;tbody&gt; 元素的唯一允许的子元素是&lt;tr&gt; 元素(请参阅html.spec.whatwg.org/multipage/tables.html#the-tbody-element),因此您现在生成的 HTML 将是无效的。根据您要执行的操作,您可以将该表移动到与学生 ID 相同的 &lt;td&gt; 中,或者在模板中包含该表的位置添加包装器 &lt;tr&gt;

标签: html asp.net-mvc razor html-table


【解决方案1】:

您的基本表格语义是错误的。要嵌套table,子table应该进入&lt;td&gt;

table,
th,
td {
  border: 1px solid black;
}
<table style="width:100%">
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Age</th>
  </tr>
  <tr>
    <td>Jill</td>
    <td>Smith</td>
    <td>50</td>
  </tr>
  <tr>
    <td colspan="3">
      <table style="width:100%">
        <tr>
          <th>Firstname</th>
          <th>Lastname</th>
          <th>Age</th>
        </tr>
        <tr>
          <td>Jill</td>
          <td>Smith</td>
          <td>50</td>
        </tr>
      </table>
    </td>
  </tr>
</table>

【讨论】:

  • 谢谢乔斯佩。它正在工作,但我在父表行下得到跨行
  • 是不是因为border: 1px solid black;??
  • 不,我猜这是&lt;td colspan="3"&gt;的bcse@
  • 不,你能用你正在尝试的代码更新帖子吗/
  • 很可能与样式有关,但我没有足够的信息来调试代码。就主要问题而言,以上是解决它的解决方案。请将新问题作为不同的帖子发布。
猜你喜欢
  • 1970-01-01
  • 2014-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-18
相关资源
最近更新 更多