Vue 2,没有网页包.我想一次渲染两个trs,主要和细节可展开行.这就是我想要实现的目标:

<table>
   <tbody>
     <div v-for="item in items">
         <tr></tr>
         <tr class="detail-row"></tr>
     </div>
   </tbody>
</table>

问题是<div>是一个身体不健康的子元素.如何在每个for循环迭代中渲染两个<tr>

推荐答案

这就是在支持template的浏览器中解决这个问题的方法.

<table>
   <tbody>
     <template v-for="item in items">
         <tr></tr>
         <tr class="detail-row"></tr>
     </template>
   </tbody>
</table>

如果需要支持not支持template的浏览器,我通常会使用渲染功能.

以下是两者的一个工作示例.

console.clear()

new Vue({
  el: "#app",
  data: {
    items: [{
        master: "Master",
        detail: "Detail"
      },
      {
        master: "Master",
        detail: "Detail"
      },
    ]
  }
})

new Vue({
  el: "#app2",
  data: {
    items: [{
        master: "Master",
        detail: "Detail"
      },
      {
        master: "Master",
        detail: "Detail"
      },
    ]
  },
  render(h){
    // build the rows
    let rows = []
    for (let item of this.items){
      rows.push(h("tr", [h("td", [item.master])]))
      rows.push(h("tr", {class: "detail-row"}, [h("td", [item.detail])]))
    }
    
    // add rows to body
    let body = h("tbody", rows)
    
    // return the table
    return h("table", [body])
  }
})
.detail-row{
 background-color: lightgray;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.js"></script>

<h2>Using template</h2>
<div id="app">
  <table>
    <tbody>
      <template v-for="item in items">
        <tr><td>{{item.master}}</td></tr>
        <tr class="detail-row"><td>{{item.detail}}</td></tr>
      </template>
    </tbody>
  </table>
</div>
  
<h2>Using render function</h2>
<div id="app2"></div>

Vue.js相关问答推荐

如何在AXIOS调用响应中动态导入视频并创建它的绝对路径?

如何在Vue 3中将动态特性从主零部件同步到子零部件

vuejs:在确认提示之前更改 HTML

更新 Shopware 管理 CMS 组件中的编辑器视图

作为props 传递的原始 ref 的 Vue 3 react 性

在 v-for 中定义变量有什么问题吗?

当try 更新 Vue 3 中的关联数组中的单个元素时,所有值都会更新

处理多张卡片中的按钮

如何在 vue2.7 中删除 slot-scope

Defer推迟执行,直到外部脚本加载到 Vue.js

VueJS 插件的如何react式绑定?

如何在 vue.js 2 的其他组件中调用方法?

如何将 axios/axios 拦截器全局附加到 Nuxt?

为什么 CSS 关键帧动画在具有范围样式的 Vue 组件中被 destruct ?

如何将 mapActions 与 vue + Typescript 类组件一起使用?

我可以使用react路由创建别名路由吗?

为什么 Vue.js 使用 VDOM?

Vue.js 3 - 替换/更新react性对象而不会失go react性

如何在 VeeValidate 中自定义错误信息(字段名称)?

vuejs 中缺少 webpack 配置