JS Lab 2
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
const dbName = "CustomerDB";
|
||||
let db;
|
||||
|
||||
const request = indexedDB.open(dbName, 1);
|
||||
|
||||
request.onerror = (event) => {
|
||||
console.error("Database error:", event.target.error);
|
||||
};
|
||||
|
||||
request.onupgradeneeded = (event) => {
|
||||
db = event.target.result;
|
||||
const objectStore = db.createObjectStore("customers", { keyPath: "customer_id" });
|
||||
objectStore.createIndex("email", "email", { unique: true });
|
||||
console.log("IndexedDB setup complete");
|
||||
};
|
||||
|
||||
request.onsuccess = (event) => {
|
||||
db = event.target.result;
|
||||
console.log("Database connected successfully");
|
||||
};
|
||||
|
||||
function saveToIndexedDB(customer) {
|
||||
if (!db) { return };
|
||||
const transaction = db.transaction(["customers"], "readwrite");
|
||||
const objectStore = transaction.objectStore("customers");
|
||||
const request = objectStore.put(customer);
|
||||
|
||||
request.onsuccess = () => {
|
||||
console.log("Customer saved to local IndexedDB:", customer);
|
||||
};
|
||||
|
||||
request.onerror = (event) => {
|
||||
console.error("Error saving to IndexedDB:", event.target.error);
|
||||
};
|
||||
}
|
||||
|
||||
function deleteFromIndexedDB(id) {
|
||||
if (!db) { return };
|
||||
const transaction = db.transaction(["customers"], "readwrite");
|
||||
const objectStore = transaction.objectStore("customers");
|
||||
const request = objectStore.delete(Number(id));
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,41 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>Customer Management Portal</title>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<link rel="stylesheet" href="style.css" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<fieldset class="customer-fieldset">
|
||||
<legend class="customer-legend">Customer Management</legend>
|
||||
<div class="form-row">
|
||||
<label for="customer_id">Customer ID:</label>
|
||||
<input type="text" id="customer_id" name="customer_id" />
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="customer_name">Customer Name:</label>
|
||||
<input type="text" id="customer_name" name="customer_name" />
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="customer_email">Customer Email:</label>
|
||||
<input type="email" id="customer_email" name="customer_email" />
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="customer_phone">Customer Phone:</label>
|
||||
<input type="tel" id="customer_phone" name="customer_phone" />
|
||||
</div>
|
||||
<div class="button-row">
|
||||
<button id="new" class="action-btn btn-new">New</button>
|
||||
<button id="add_customer" class="action-btn btn-add">Add Customer</button>
|
||||
<button id="update_customer" class="action-btn btn-update">Update Customer</button>
|
||||
<button id="delete_customer" class="action-btn btn-delete">Delete Customer</button>
|
||||
<button id="find_customer" class="action-btn btn-find">Find Customer</button>
|
||||
</div>
|
||||
</fieldset>
|
||||
<div id="message" class="message"></div>
|
||||
|
||||
<script src="app.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,115 @@
|
||||
|
||||
body {
|
||||
background: #f7f9fa;
|
||||
font-family: 'Segoe UI', Arial, sans-serif;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.customer-fieldset {
|
||||
background: #fff;
|
||||
border: 1px solid #d1d5db;
|
||||
border-radius: 10px;
|
||||
box-shadow: 0 2px 8px rgba(0,0,0,0.07);
|
||||
padding: 2rem 2.5rem 1.5rem 2.5rem;
|
||||
max-width: 400px;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.customer-legend {
|
||||
font-size: 1.3rem;
|
||||
font-weight: 600;
|
||||
color: #2d3748;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.form-row {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.form-row label {
|
||||
margin-bottom: 0.3rem;
|
||||
color: #4a5568;
|
||||
font-size: 1rem;
|
||||
}
|
||||
|
||||
.form-row input {
|
||||
padding: 0.5rem;
|
||||
border: 1px solid #cbd5e1;
|
||||
border-radius: 5px;
|
||||
font-size: 1rem;
|
||||
background: #f9fafb;
|
||||
transition: border 0.2s;
|
||||
}
|
||||
|
||||
.form-row input:focus {
|
||||
border-color: #3182ce;
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.button-row {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
margin-top: 0.5rem;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.action-btn {
|
||||
color: #fff;
|
||||
border: none;
|
||||
border-radius: 5px;
|
||||
padding: 0.5rem 1rem;
|
||||
font-size: 1rem;
|
||||
cursor: pointer;
|
||||
transition: background 0.2s;
|
||||
}
|
||||
|
||||
/* Action-specific button colors */
|
||||
.btn-new {
|
||||
background: #6b7280;
|
||||
}
|
||||
.btn-new:hover {
|
||||
background: #4b5563;
|
||||
}
|
||||
|
||||
.btn-add {
|
||||
background: #22c55e;
|
||||
}
|
||||
.btn-add:hover {
|
||||
background: #16a34a;
|
||||
}
|
||||
|
||||
.btn-update {
|
||||
background: #f59e42;
|
||||
}
|
||||
.btn-update:hover {
|
||||
background: #d97706;
|
||||
}
|
||||
|
||||
.btn-delete {
|
||||
background: #ef4444;
|
||||
}
|
||||
.btn-delete:hover {
|
||||
background: #b91c1c;
|
||||
}
|
||||
|
||||
.btn-find {
|
||||
background: #3182ce;
|
||||
}
|
||||
.btn-find:hover {
|
||||
background: #2563eb;
|
||||
}
|
||||
|
||||
.message {
|
||||
margin-top: 1.2rem;
|
||||
font-size: 1.05rem;
|
||||
color: #2563eb;
|
||||
min-height: 1.2em;
|
||||
text-align: center;
|
||||
}
|
||||
Reference in New Issue
Block a user