Generate Inventory UI in HTML + JS (one file)
- User
Generate Inventory UI in HTML + JS (one file)
- Assistant
Generate Inventory UI in HTML + JS (one file)
Certainly! Below is a complete, self-contained HTML file that implements a simple Inventory UI using HTML, CSS, and JavaScript. This interface allows users to:
You can copy the following code into a file with a .html extension (e.g., inventory.html) and open it in your web browser to see it in action.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Inventory Management UI</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
}
.container {
width: 90%;
max-width: 800px;
margin: 40px auto;
background-color: #fff;
padding: 20px 30px 40px 30px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h1 {
text-align: center;
color: #333;
}
form {
margin-bottom: 30px;
}
input[type="text"], input[type="number"] {
padding: 10px;
margin-right: 10px;
width: 200px;
border: 1px solid #ccc;
border-radius: 4px;
}
input[type="number"] {
width: 100px;
}
button {
padding: 10px 15px;
background-color: #28a745;
border: none;
color: white;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #218838;
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 12px 15px;
border-bottom: 1px solid #ddd;
text-align: center;
}
th {
background-color: #f8f9fa;
}
.action-btn {
padding: 5px 10px;
margin: 0 2px;
border: none;
border-radius: 4px;
cursor: pointer;
}
.edit-btn {
background-color: #007bff;
color: white;
}
.edit-btn:hover {
background-color: #0069d9;
}
.delete-btn {
background-color: #dc3545;
color: white;
}
.delete-btn:hover {
background-color: #c82333;
}
/* Responsive */
@media (max-width: 600px) {
input[type="text"], input[type="number"] {
width: 100%;
margin-bottom: 10px;
}
button {
width: 100%;
}
.action-btn {
margin: 5px 0;
width: 100%;
}
}
</style>
</head>
<body>
<div class="container">
<h1>Inventory Management</h1>
<form id="addItemForm">
<input type="text" id="itemName" placeholder="Item Name" required>
<input type="number" id="itemQuantity" placeholder="Quantity" min="1" required>
<button type="submit">Add Item</button>
</form>
<table id="inventoryTable">
<thead>
<tr>
<th>#</th>
<th>Item Name</th>
<th>Quantity</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<!-- Inventory items will be dynamically inserted here -->
</tbody>
</table>
</div>
<script>
// Inventory Array to hold items
let inventory = [];
// References to DOM elements
const addItemForm = document.getElementById('addItemForm');
const itemNameInput = document.getElementById('itemName');
const itemQuantityInput = document.getElementById('itemQuantity');
const inventoryTableBody = document.querySelector('#inventoryTable tbody');
// Function to render the inventory table
function renderInventory() {
// Clear existing rows
inventoryTableBody.innerHTML = '';
// Populate table with current inventory
inventory.forEach((item, index) => {
const row = document.createElement('tr');
// Index
const indexCell = document.createElement('td');
indexCell.textContent = index + 1;
row.appendChild(indexCell);
// Item Name
const nameCell = document.createElement('td');
nameCell.textContent = item.name;
row.appendChild(nameCell);
// Quantity
const quantityCell = document.createElement('td');
quantityCell.textContent = item.quantity;
row.appendChild(quantityCell);
// Actions
const actionsCell = document.createElement('td');
// Edit Button
const editButton = document.createElement('button');
editButton.textContent = 'Edit';
editButton.classList.add('action-btn', 'edit-btn');
editButton.addEventListener('click', () => editItem(index));
actionsCell.appendChild(editButton);
// Delete Button
const deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.classList.add('action-btn', 'delete-btn');
deleteButton.addEventListener('click', () => deleteItem(index));
actionsCell.appendChild(deleteButton);
row.appendChild(actionsCell);
inventoryTableBody.appendChild(row);
});
}
// Function to add a new item
function addItem(event) {
event.preventDefault(); // Prevent form submission
const name = itemNameInput.value.trim();
const quantity = parseInt(itemQuantityInput.value.trim());
if (name === '' || isNaN(quantity) || quantity < 1) {
alert('Please enter a valid item name and quantity.');
return;
}
// Check if item already exists
const existingItemIndex = inventory.findIndex(item => item.name.toLowerCase() === name.toLowerCase());
if (existingItemIndex !== -1) {
// Update quantity if item exists
inventory[existingItemIndex].quantity += quantity;
} else {
// Add new item
inventory.push({ name, quantity });
}
// Reset form
addItemForm.reset();
// Re-render inventory
renderInventory();
}
// Function to edit an item
function editItem(index) {
const item = inventory[index];
const newQuantity = prompt(`Enter new quantity for "${item.name}":`, item.quantity);
if (newQuantity === null) return; // Cancelled
const quantityNumber = parseInt(newQuantity.trim());
if (isNaN(quantityNumber) || quantityNumber < 1) {
alert('Please enter a valid quantity.');
return;
}
inventory[index].quantity = quantityNumber;
renderInventory();
}
// Function to delete an item
function deleteItem(index) {
const item = inventory[index];
const confirmDelete = confirm(`Are you sure you want to delete "${item.name}" from the inventory?`);
if (confirmDelete) {
inventory.splice(index, 1);
renderInventory();
}
}
// Event Listener for adding items
addItemForm.addEventListener('submit', addItem);
// Initial rendering
renderInventory();
</script>
</body>
</html>
HTML Structure:
CSS Styling:
JavaScript Functionality:
name and quantity properties.renderInventory): Dynamically updates the inventory table based on the current inventory array.addItem):
editItem):
deleteItem):
User Experience Enhancements:
Add a New Item:
Edit an Existing Item:
Delete an Item:
Feel free to customize the UI further based on your requirements. Here are some ideas:
localStorage or other storage mechanisms.This simple Inventory Management UI provides a solid foundation for managing items on the client side. You can extend and modify it to cater to more complex requirements as needed.
Not yet included in a published release.