【发布时间】:2020-04-07 13:44:52
【问题描述】:
我在从列表中添加/删除项目时遇到了一些问题(数据来自 API,但我在这里做了一个小例子)。
我正在使用 Vue 和 Vuetify 的数据表。
目前,数量未出现,因此总计为 NaN。
代码如下:
<template>
<v-container fluid>
<v-layout row wrap>
<v-flex sm12>
<v-data-table
:headers="headers"
:items="hadwareItems"
hide-actions
class="elevation-0"
>
<template v-slot:items="props">
<td>{{ props.item.name }}</td>
<td class="text-xs-left">{{ props.item.qtyAvailable }}</td>
<td class="text-xs-left">
<v-btn
fab
small
:disabled="props.item.qtyAvailable <= 0"
outline
class="mx-2 hardware-qty-btn"
color="primary"
@click="removeItem(props.item)"
>
<p class="mb-0 title font-weight-medium text-xs-left">-</p>
</v-btn>
{{ props.item.qty }}
<v-btn
fab
small
:disabled="props.item.qtyAvailable <= 0"
outline
class="mx-2 hardware-qty-btn"
color="primary"
@click="addItem(props.item)"
>
<p class="mb-0 title font-weight-medium">+</p>
</v-btn>
</td>
<td class="text-xs-left">{{ props.item.price }}</td>
<td class="text-xs-left">
{{ props.item.totalPrice = props.item.qty * props.item.price }}
</td>
</template>
</v-data-table>
</v-flex>
</v-layout>
</v-container>
</template>
这里是 JS:
<script>
export default {
data() {
return {
headers: [
{
text: "Android Options",
align: "left",
sortable: false,
value: "name"
},
{ text: "Qty Available", value: "qtyAvailable" },
{ text: "Qty", value: "qty" },
{ text: "Price", value: "price" },
{ text: "Total Price", value: "totalPrice" }
],
hadwareItems: [
{
name: "Frozen Yogurt",
qtyAvailable: 6,
price: 4.0
},
{
name: "Ice cream sandwich",
qtyAvailable: 9,
price: 4.3
},
{
name: "Eclair",
qtyAvailable: 0,
price: 6.0
},
{
name: "Cupcake",
qtyAvailable: 37,
price: 4.3
},
{
name: "Gingerbread",
qtyAvailable: 10,
price: 3.9
},
{
name: "Jelly bean",
qtyAvailable: 0,
price: 3.0
},
{
name: "Lollipop",
qtyAvailable: 2,
price: 20
},
{
name: "Honeycomb",
qtyAvailable: 32,
price: 6.5
},
{
name: "Donut",
qtyAvailable: 250,
price: 4.9
},
{
name: "KitKat",
qtyAvailable: 260,
price: 7
}
],
qty: 0,
totalPrice: 0
};
},
methods: {
addItem(hardwareItem) {
if (hardwareItem.qty < hardwareItem.qtyAvailable) {
hardwareItem.qty++;
} else {
alert("Can't select more than available Quantity");
}
},
removeItem(hardwareItem) {
hardwareItem.qty--;
if (hardwareItem.qty < 0) {
hardwareItem.qty = 0;
}
}
}
};
</script>
该示例也可在 Codepen 上获得:https://codepen.io/fabiozanchi/pen/LYVwaPy
谢谢!
【问题讨论】:
-
qty和totalPrice到底是什么意思?每一行还是一列? -
那是每一行。 totalPrice 是选择的数量(默认应该是 0 但现在是银行)* 价格。
-
@FabioZanchi,数量从 0 开始还是从“可用数量”开始统计?
-
数量从0开始,订购数量不能大于qtyAvailable
-
@FabioZanchi,让我知道以下答案可以解决您的问题
标签: javascript vue.js vuetify.js