Jonas Eriksson
03/17/2023, 4:53 PMJonas Eriksson
03/17/2023, 4:54 PMJonas Eriksson
03/17/2023, 4:55 PM<div x-data="{ products: [
{ name: 'Motor Control Unit', price: 50, quantity: 0 },
{ name: 'Power Distribution Module', price: 100, quantity: 4 },
{ name: 'Ignition Coil Pack', price: 250, quantity: 0 }
]}">
<table>
<thead>
<tr>
<th>Quantity</th>
<th>Product Name</th>
<th>Price</th>
<th>Subtotal</th>
</tr>
</thead>
<tbody>
<template x-for="(product, index) in products" :key="index">
<tr>
<td><input type="number" min="0" x-model="product.quantity"></td>
<td x-text="product.name"></td>
<td x-text="'$'+product.price"></td>
<td x-text="'$'+(product.quantity * product.price)"></td>
</tr>
</template>
</tbody>
</table>
</div>
The code uses AlpineJS to create a data object named products that contains an array of objects representing each product, including its name, price, and quantity. The HTML table is dynamically generated using a x-for loop to iterate over each product object and generate a new row for each.
The first column contains an HTML input element with a type of "number" and a minimum value of 0. It is bound to the quantity property of the current product object using x-model.
The second column simply displays the product name using x-text.
The third column displays the product price with a dollar sign using x-text and concatenating the string with the price property of the current product object.
The fourth column displays the subtotal by multiplying the quantity and price properties of the current product object and then displaying the result with a dollar sign using x-text.
Note that the second product object in the products array has a pre-filled quantity of 4. This will automatically update the subtotal for that row when the page is loaded.
I hope this helps! Let me know if you have any questions.ryan
03/17/2023, 4:55 PMJonas Eriksson
03/17/2023, 4:56 PM<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AlpineJS and Tailwind CSS Product List</title>
<link href="<https://cdn.jsdelivr.net/npm/tailwindcss@2.2.16/dist/tailwind.min.css>" rel="stylesheet">
<script src="<https://cdn.jsdelivr.net/npm/alpinejs@2.8.2/dist/alpine.min.js>" defer></script>
</head>
<body>
<div x-data="productList()" class="container mx-auto py-10">
<table class="table-auto w-full">
<thead>
<tr>
<th class="border px-4 py-2">Product Quantity</th>
<th class="border px-4 py-2">Product Name</th>
<th class="border px-4 py-2">Product Price</th>
<th class="border px-4 py-2">Subtotal</th>
</tr>
</thead>
<tbody>
<template x-for="(product, index) in products" :key="index">
<tr>
<td class="border px-4 py-2">
<input x-model="product.quantity" @input="calculateSubtotal(product)" type="number" min="0" class="w-full">
</td>
<td class="border px-4 py-2" x-text="product.name"></td>
<td class="border px-4 py-2" x-text="product.price"></td>
<td class="border px-4 py-2" x-text="product.subtotal"></td>
</tr>
</template>
</tbody>
</table>
</div>
<script>
function productList() {
return {
products: [
{
quantity: 0,
name: 'Axial Flux Capacitor',
price: 50,
subtotal: 0
},
{
quantity: 4,
name: 'Rapid Torque Converter',
price: 100,
subtotal: 400
},
{
quantity: 0,
name: 'Vortex Harmonic Stabilizer',
price: 250,
subtotal: 0
}
],
calculateSubtotal(product) {
product.subtotal = product.quantity * product.price;
},
init() {
this.products.forEach(product => this.calculateSubtotal(product));
}
}
}
</script>
</body>
</html>Jonas Eriksson
03/17/2023, 4:56 PMcalculateSubtotal function is called whenever the input value changes, which updates the respective product's subtotal."Jonas Eriksson
03/17/2023, 5:02 PMDave Merrill
03/17/2023, 5:34 PMsknowlton
03/17/2023, 6:00 PMsknowlton
03/17/2023, 6:00 PMJonas Eriksson
03/17/2023, 6:38 PMDave Merrill
03/17/2023, 7:17 PMJonas Eriksson
03/23/2023, 8:30 PMJonas Eriksson
03/23/2023, 8:36 PMformatCurrency function to use Intl.NumberFormat without the fraction digits" - done. StackOverflow can probably shut down in a few weeks.
function initProductForm() {
// ... existing code
return {
// ... existing properties and functions
formatCurrency(amount) {
return new Intl.NumberFormat('nb-NO', {
style: 'currency',
currency: 'NOK',
minimumFractionDigits: 0,
maximumFractionDigits: 0
}).format(amount);
}
};
}