gpt4 book ai didi

javascript - VUE - [food toppings counter] 当点击 "+ add"按钮时,如何增加 v-for 列表中的元素 -topping- 的值 -of toppings-

转载 作者:行者123 更新时间:2023-12-03 06:47:45 27 4
gpt4 key购买 nike

我是 js 和 vue 的初学者,我正在建立一个网站来自动化我的比萨店交付订单。

我有一个 v-for 浇头列表,客户可以从列表中最多选择 4 个浇头,他可以选择 2、3 或 4 次相同的浇头。

我为列表中的每个项目创建了一个计数器,它将选定的顶部名称存储在一个限制为 4 的数组中。这有效。

如何选择和增加在界面中选择的浇头数量?

下面我有我的代码和一些视觉引用

https://codepen.io/mordzin/pen/VwePBzq

var App = new Vue({
el: '#app',
data: {
flavorCounter: 0,
cardapio: [],
order: [],
selectedItem: null
},
mounted() {
// const axios = require('axios');
axios.get('https://v2-api.sheety.co/4a5e4bb41d15a6ea344152fafca024db/zunepizza/cardapio')
.then((response) => {

this.cardapio = response.data.cardapio
})
.catch((error) => {

})
.finally(() => {

})
},

methods:{

addFlavor(){

if (this.flavorCounter <= 3) {
flavor = event.target.getAttribute('flavor')
this.order.push(flavor);
this.flavorCounter ++

console.log(this.order)
console.log(this.flavorCounter)
} else {
console.log('Maximo de sabores')
}
},

removeFlavor(){

if (this.flavorCounter >= 1) {
flavor = event.target.getAttribute('flavor')

this.order.splice(index, 1);
this.flavorCounter --

console.log(this.order)
console.log(this.flavorCounter)
} else {
}
},

}


});


<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,minimum-scale=1">
<title>Zune Pizza</title>
<link rel="stylesheet" href="zunepizza.css">
<link async href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link async href="https://fonts.googleapis.com/css?family=Roboto&display=swap" rel="stylesheet">


<!-- <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.12/handlebars.min.js"></script> -->
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
</head>

<body>
<div id="app">
<template>
<div id="cardapio">
<ul v-for="cardapio in cardapio">
<li>
<div class="item-info">
<h3 class="item-title">{{cardapio.name}}</h3>
<div class="tag">
<h5>{{cardapio.type}}</h5>
</div>
<p class="item-title">{{cardapio.desc}}</p>
</div>
<div class="item-img">
<div class="flavorCounter" :id="cardapio.name">
<button
@click="addFlavor()"
:flavor="cardapio.name"
class="addFlavor controller-btn"> + </button>

<input :id='cardapio.name' type="number" value="0" class="flavorQuantity">

<button
@click='removecliFlavor()'
:flavor='cardapio.name'
class="addFlavor controller-btn"> - </button>
</div>
<img src="img/cardapio/baska.jpg" alt="">
</div>
</li>
</ul>
</div>
</template>

</div>

<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script src="zunepizza.js" type="text/javascript" charset="utf-8"></script>

</body>

</html>

zune pizza interface topping counter

我需要像下图这样的东西

ifood interface topping counter

最佳答案

你可以这样做:https://codepen.io/jasik/pen/NWxdLRg

<div id="cardapio">
<ul v-for="cardapio in cardapios">
<li>
<div class="item-info">
<h3 class="item-title">{{cardapio.name}}</h3>
<div class="tag">
<h5>{{cardapio.type}}</h5>
</div>
<p class="item-title">{{cardapio.desc}}</p>
</div>
<div class="item-img">
<div class="flavorCounter" :id="cardapio.name">
<button @click="cardapio.counter++" :disabled="cardapio.counter === 4" :flavor="cardapio.name" class="addFlavor controller-btn"> + </button>

<input :id='cardapio.name' type="number" v-model="cardapio.counter" class="flavorQuantity">

<button @click='cardapio.counter--' :disabled="cardapio.counter === 0" :flavor='cardapio.name' class="addFlavor controller-btn"> - </button>
</div>
<img src="img/cardapio/baska.jpg" alt="">
</div>
</li>
</ul>
</div>


data: {
cardapios: []
},
created() {
axios
.get(
"https://v2-api.sheety.co/4a5e4bb41d15a6ea344152fafca024db/zunepizza/cardapio"
)
.then((response) => {
let edited = response.data.cardapio;
edited.forEach((e) => {
e.counter = 0;
});
this.cardapios = edited;
})
.catch((error) => {})
.finally(() => {});
},
methods: {}

希望你会发现它有帮助。

关于javascript - VUE - [food toppings counter] 当点击 "+ add"按钮时,如何增加 v-for 列表中的元素 -topping- 的值 -of toppings-,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62475290/

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