【问题标题】:Using rowspan in a forEach loop with ejs?在带有ejs的forEach循环中使用rowspan?
【发布时间】:2020-07-26 13:20:03
【问题描述】:

我有一个引导表,它通过循环显示我的 mongoDB 集合中的数据。我试图在我的表的最后一个td 上使用rowspan,称为“其他”,以跨越到表的末尾。但是,因为它是一个循环,所以它会创建一个嵌套的 rowspan 并弄乱表格。有没有办法让行跨度不会像这样嵌套?它需要在forEach 循环中,因为数据是从我的名为环境的集合中提取的,它只能在forEach 中使用(如果在循环之外使用它会给我错误“环境未定义” )

当我尝试在最后一列使用 rowspan 时它的外观:

表格的html:

<table class="table">
  <thead>
    <tr>
      <th scope="col">Something</th>
      <th scope="col">Code Version</th>
      <th scope="col">Region</th>
      <th scope="col">Something</th>
      <th scope="col">Something</th>
      <th scope="col">Something->Membership</th>
      <th scope="col">SBMO</th>
      <th scope="col">Something</th>
      <th scope="col">IVR</th>
      <th scope="col">CPM</th>
      <th scope="col">Other</th>
    </tr>
  </thead>
  <tbody>

    <!-- loop through each entry in mongoDB collection: environment -->
    <% environment.forEach(function(environment){ %>
    <tr>
        <td>
          <%= environment.something %>
        </td>
        <td>
          <%= environment.codeVersion %>
        </td>
        <td>
          <%= environment.region %>
        </td>
        <td>
          <%= environment.something %>
        </td>
        <td>
          <%= environment.something %>
        </td>
        <td>
          <%= environment.membership %>
        </td>
        <td>
          <%= environment.SBMO %>
        </td>
        <td>
          <%= environment.something %>
        </td>
        <td>
          <%= environment.IVR %>
        </td>
        <td>
          <%= environment.CPM %>
        </td>

<!-- this is the td that i'm having issues with -->
        <td rowspan=50 class="other">
          <%= environment.other %>
         </td>
    </tr>

    <% }); %> <!-- end loop through environment -->
  </tbody>
</table>

&lt;td rowspan=10 class="other"&gt;&lt;%= environment.other%&gt;&lt;/td&gt; 上的检查器元素:

【问题讨论】:

  • 您能分享一下您遇到问题的论文 td 中的检查员元素吗?
  • @JérômeW 刚刚添加了截图
  • 我加了一个答案,试试看告诉我怎么回事^^

标签: html twitter-bootstrap foreach html-table ejs


【解决方案1】:

尝试将您的 td 与 rowspan 放在您的 &lt;/tr&gt; 之后,如下所示。

代替:

   <td rowspan=50 class="other">
       <div><%= environment.other %></div>
   </td>
</tr>

<% }); %> <!-- end loop through environment -->

试试这个:

</tr>
   <td rowspan=50 class="other">
       <div><%= environment.other %></div>
   </td>

<% }); %> <!-- end loop through environment -->

【讨论】:

  • 我试过了,但似乎没有什么不同,因为它仍然在 ejs forEach 循环中。如果我把它从循环中取出,我就不能调用environment.other,因为变量environment只能在从db调用它的循环中使用
  • @Emily 也许你能找到一种方法让你的“其他”数据脱离循环?例如,如果您可以使用environment.other[0] 或类似的方式获取它,那么您可以使用此数据隔离您的 td 并且不将其包含在您的循环中。我希望我的解释清楚明白^^
猜你喜欢
  • 1970-01-01
  • 2019-01-02
  • 2020-01-21
  • 1970-01-01
  • 2016-12-09
  • 2017-03-25
  • 2018-11-07
  • 1970-01-01
  • 2016-08-21
相关资源
最近更新 更多