I am trying to do something similar to a shopping cart, where initially there is a series of products that I load with an array of objects.
products = [
{
id: 1,
name: "Product1",
},
{
id: 2,
name: "Product2",
},
{
id: 3,
name: "Product",
},
];
I show the list of products with svelte like this:
<div class="col-md-12 row grid">
{#each products as product}
<div class="card mt-2 g-col-3">
<div class="row">
<div class="col-md-8">
<div class="card-body">
<h5>{product.name}</h5>
<button
on:click={addProduct(product.id)}
class="btn btn-primary">Add Product
</button>
{ "0 products"}
</div>
</div>
</div>
</div>
{/each}
</div>
And through the function addProduct() I update the array inventory with the product id and the number of units of that product
let inventory =[];
const addProduct = id =>{
let qty = 0;
if(inventory.find(element => element.id === id))
{
qty = inventory.find(element => element.id === id).qty
inventory=inventory.filter(element => element.id !== id)
inventory.push({id:id,qty:qty+1});
}
else{
inventory.push({id:id,qty:1});
}
}
Where I don't know how to set it up is in each product, where it now says { "0 products"}, update it dynamically as the user adds each of the products
Thanks a lot !