You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VueJS中实现Laravel Blade风格的嵌套表格渲染?

Got it, let's translate that Laravel Blade nested table logic into Vue.js properly. The key is swapping out Blade's @if for Vue's v-if directive and structuring the template to handle conditional child rows correctly. Here's how to make it work:

Vue.js Implementation of Nested Table (Replacing Blade Logic)

First, we'll use a <template> tag to group each parent row and its optional child row—this is important because <tbody> only accepts <tr> elements as direct children, and <template> acts as a renderless wrapper that doesn't add extra DOM nodes.

Here's the complete template code:

<table class="parent-table">
  <thead>
    <tr>
      <th>Drawing ID</th>
      <th>Request Name</th>
      <th>Status</th>
      <!-- Add all your parent table columns here -->
    </tr>
  </thead>
  <tbody>
    <!-- Wrap parent + child rows in a template with v-for -->
    <template v-for="(drawing, index) in event.drawingrequests" :key="index">
      <!-- Parent table row -->
      <tr>
        <td>{{ drawing.id }}</td>
        <td>{{ drawing.request_name }}</td>
        <td>{{ drawing.status }}</td>
        <!-- Fill in your parent row data fields -->
      </tr>

      <!-- Conditional child row with nested table -->
      <tr v-if="drawing.childs && drawing.childs.length > 0">
        <!-- Span all parent columns to make the child table full-width -->
        <td :colspan="3"> <!-- Replace 3 with the actual number of parent columns -->
          <table class="child-table">
            <thead>
              <tr>
                <th>Child ID</th>
                <th>Child Description</th>
                <th>Child Status</th>
                <!-- Add your child table columns here -->
              </tr>
            </thead>
            <tbody>
              <tr v-for="(child, childIndex) in drawing.childs" :key="childIndex">
                <td>{{ child.id }}</td>
                <td>{{ child.description }}</td>
                <td>{{ child.status }}</td>
                <!-- Fill in your child row data fields -->
              </tr>
            </tbody>
          </table>
        </td>
      </tr>
    </template>
  </tbody>
</table>

Key Details Breakdown:

  1. Grouping Rows with <template>:
    The <template> tag lets us apply the v-for loop to both the parent row and the optional child row without breaking the table structure. This ensures each drawing entry gets its own parent row plus a child row (if needed).

  2. Conditional Child Rendering:
    v-if="drawing.childs && drawing.childs.length > 0" checks two things to avoid errors:

    • Does drawing.childs exist (prevents issues if it's null/undefined)?
    • Does it contain at least one child item?
      Only if both are true will the nested table row render.
  3. Colspan for Proper Nesting:
    The :colspan binding makes the child table's cell span all columns of the parent table, so it looks visually nested instead of being stuck in a single column.

  4. Key Bindings:
    We add :key to both loops to help Vue efficiently update the DOM when your data changes. For better performance, use unique identifiers (like item IDs) instead of indexes if available.

Optional Styling for Clarity:

Add a bit of CSS to make the child table stand out as nested:

.child-table {
  width: 95%;
  margin-left: 2rem;
  border-collapse: collapse;
  font-size: 0.9em;
}
.child-table th, .child-table td {
  padding: 0.5rem;
  border: 1px solid #eee;
}

内容的提问来源于stack exchange,提问作者crisleiria

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 22:54:07