Vue.js表格渲染:在指定行下方动态展示数据
<h2>Vue.js表格渲染:在指定行下方动态展示数据</h2>
<p>本文将介绍如何在Vue.js表格的指定行下方动态渲染数据。</p>
<p><strong>问题描述:</strong></p>
<p>需要将<code>cardList</code>中的数据渲染在class为'style'的行的下面,但当前方法报错。</p>
<p><strong>解决方案:</strong></p>
<p>在class为'style'的行的下面添加一个嵌套的<code><tr></code>标签,并使用<code>v-for</code>指令循环渲染<code>cardList</code>中的数据。</p>
<p>**代码示例:**html<template> <table id='myTable'> <tr> <th>时间</th> <th>姓名</th> <th>地址</th> </tr> <tbody> <tr v-for='item in tableData' :key='item.id'> <td>{{ item.date }}</td> <td>{{ item.name }}</td> <td>{{ item.address }}</td> </tr> <tr v-for='card in item.cardList' :key='card.cardId' class='style'> <td>{{ card.cardName }}</td> <td>{{ card.cardStatus }}</td> <td>{{ card.cardSize }}</td> </tr> </tbody> </table></template></p>
<script>export default { name: 'TableMerge', data() { return { checked: false, tableData: [ { id: 1, date: '2016-05-03', name: '王小虎', address: '上海市普陀区金沙江路 1518 弄', cardList: [ { cardName: '测试', cardStatus: 'success', cardSize: 'big', cardId: 100, }, { cardName: '测试', cardStatus: 'success', cardSize: 'big', cardId: 101, }, { cardName: '测试', cardStatus: 'success', cardSize: 'big', cardId: 102, }, { cardName: '测试', cardStatus: 'success', cardSize: 'big', cardId: 103, }, ], }, { id: 5, date: '2016-05-08', name: '王小虎', address: '上海市普陀区金沙江路 1518 弄', }, ], }; },};</script>
<style scoped>table { border-collapse: collapse;}th,td { padding: 10px; width: 200px; border: 1px #c4c7ce solid; text-align: center;}</style>
<p><strong>关键点:</strong></p>
<ul>
<li>在第一个<code>v-for</code>循环内部嵌套第二个<code>v-for</code>循环。* 使用<code>item.cardList</code>获取当前循环的<code>item</code>对象的<code>cardList</code>属性。* 使用<code>:key</code>绑定唯一的值,提高渲染性能。</li>
</ul>
<p>通过以上步骤,你就可以在Vue.js表格的指定行下方动态渲染<code>cardList</code>中的数据了。</p>
原文地址: https://www.cveoy.top/t/topic/fw7S 著作权归作者所有。请勿转载和采集!