function readOnly(count){ }
Don't have an account?
Search for an answer or ask a question of the zone or Customer Support.
You need to sign in to do that
Sign in to start searching questions
Signup for a Developer Edition
Sign in to start a discussion
HTML Markup:
<template>
<div>
<h1>Product List</h1>
<ul>
<template for:each={products} for:item="product">
<li key={product.id}>
{product.name} - {product.price}
<button onclick={addToCart} data-id={product.id}>Add to Cart</button>
</li>
</template>
</ul>
<h1>Cart Items</h1>
<ul>
<template for:each={cartItems} for:item="item">
<li key={item.id}>{item.name} - {item.price}</li>
</template>
</ul>
</div>
</template>
JavaScript Code:
import { LightningElement, track } from 'lwc';
export default class CartItems extends LightningElement {
@track products = [
{ id: 1, name: 'Product A', price: '$10' },
{ id: 2, name: 'Product B', price: '$20' },
{ id: 3, name: 'Product C', price: '$30' },
];
@track cartItems = [];
addToCart(event) {
const id = event.target.dataset.id;
const product = this.products.find(item => item.id == id);
this.cartItems.push(product);
}
}