I'm a newcomer to the world of JavaScript and Vue.js, currently working on a project to automate the ordering process for my pizza delivery pizzeria.
On the website, I have a list of toppings that customers can choose from. They have the option to select up to 4 toppings from the list, and they can choose the same topping 2, 3, or 4 times.
To manage this, I implemented a counter for each item in the list. The counter keeps track of the selected topping names in an array with a limit of 4, and this approach has been successful.
My question is, how can I allow users to select a topping and increase the quantity on the interface?
Below you can find my code snippet and some visual references:
Vue.component('topping-selector', {
data() {
return {
selectedToppings: [],
toppingLimit: 4
};
},
methods: {
addTopping(topping) {
if (this.selectedToppings.length < this.toppingLimit) {
this.selectedToppings.push(topping);
} else {
console.log('You have reached the maximum number of toppings allowed.');
}
},
increaseToppingAmount(topping) {
const index = this.selectedToppings.indexOf(topping);
if (index !== -1) {
this.selectedToppings.splice(index, 1);
this.selectedToppings.push(topping);
}
}
}
});
Below is the HTML and template structure:
<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 src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
</head>
<body>
<div id="app">
<topping-selector></topping-selector>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</body>
</html>
Zune Pizza Topping Counter Display
Desired interface similar to the image: