Vuetify:如何在v-data-table中添加额外的项行



我正试图在v-data表的底部添加一行。额外的一行是sum行,但我需要从api提供的数组中提取sum,而不是在前端进行计算。所以我的问题是如何将sum数组映射为表底部的额外一行?我知道总体结构是这样的,但我不知道如何完成。如果有任何帮助,我将不胜感激,谢谢!

<v-data-table>
<template v-for="s in sum">
</template>
</v-data-table>

您可以使用body.append插槽

以下是一个基于vuetify docs 示例的修改示例

<div id="app">
<v-app id="inspire">
<v-data-table
:headers="headers"
:items="desserts"
class="elevation-1"
>

<template v-slot:body.append>
<tr>
<td/>
<td>{{total.calories}}</td>
<td>{{total.fat}}</td>
<td>{{total.carbs}}</td>
<td>{{total.protein}}</td>
<td/>
</tr>
</template>

</v-data-table>
</v-app>
</div>
new Vue({
el: '#app',
vuetify: new Vuetify(),
data () {
return {
headers: [
{
text: 'Dessert (100g serving)',
align: 'start',
value: 'name',
},
{ text: 'Calories', value: 'calories' },
{ text: 'Fat (g)', value: 'fat' },
{ text: 'Carbs (g)', value: 'carbs' },
{ text: 'Protein (g)', value: 'protein' },
{ text: 'Iron (%)', value: 'iron' },
],
desserts: [
{
name: 'Frozen Yogurt',
calories: 159,
fat: 6.0,
carbs: 24,
protein: 4.0,
iron: '1%',
},
// .... etc
{
name: 'KitKat',
calories: 518,
fat: 26.0,
carbs: 65,
protein: 7,
iron: '6%',
},
],
}
},
computed:{
total(){
const sums = {
calories: 0,
fat: 0,
carbs: 0,
protein:0,
}
this.desserts.forEach(({calories, fat, carbs, protein}) => {
sums.calories += calories;
sums.fat += fat;
sums.carbs += carbs;
sums.protein += protein;
})
return sums
}
}
})

最新更新