gpt4 book ai didi

javascript - vue.js 渲染表与 rowspans

转载 作者:行者123 更新时间:2023-11-30 11:18:53 24 4
gpt4 key购买 nike

我是 vue.js 的新手,我找不到使用 vue 将以下数据渲染到带有行跨度的 html 表中的方法。

{
"title":"Monthly Sales",
"monthlySales":[
{
"product":"P123",
"months":[
{
"month":"January",
"unitPrice":"$80",
"unitsSold":2200
},
{
"month":"February",
"unitPrice":"$82",
"unitsSold":1900
},
{
"month":"March",
"unitPrice":"$81",
"unitsSold":1800
}
]
},
{
"product":"Q456",
"months":[
{
"month":"January",
"unitPrice":"$20",
"unitsSold":200
},
{
"month":"February",
"unitPrice":"$22",
"unitsSold":100
}
]
}
]
}

我想创建这样的输出:http://jsbin.com/hucufezayu/edit?html,output

enter image description here

我们如何用这些数据渲染这种表格?

最佳答案

这应该可以解决问题:

<template>
<div id="app">
<table border="1" style="border-collapse: collapse">
<thead>
<th>Product</th>
<th>Month</th>
<th>Unit price</th>
<th>No. sold</th>
</thead>
<tbody>
<template v-for="mSale in salesData.monthlySales">
<tr v-for="(month, key) in mSale.months">
<td v-if="key == 0" :rowspan="mSale.months.length"> {{mSale.product}}</td>
<td>{{month.month}}</td>
<td>{{month.unitPrice}}</td>
<td>{{month.unitsSold}}</td>
</tr>
</template>
</tbody>
</table>
</div>
</template>

<script>
export default {
name: 'app',
data() {
return {
salesData: jsonData
}
}
}
</script>

关于javascript - vue.js 渲染表与 rowspans,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50555192/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com