To create a basic application in JavaScript that takes a name and phone number, stores them in a "database," and allows you to add, delete, and search entries, we'll use a simple setup with HTML, CSS, and JavaScript. For the purpose of simplification, we'll simulate a database using an array in the JavaScript code.
Here's a complete example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Contact List</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
input, button { margin: 5px; }
table { margin-top: 20px; border-collapse: collapse; width: 50%; }
th, td { border: 1px solid #ccc; padding: 8px; text-align: left; }
</style>
</head>
<body>
<h1>Contact List</h1>
<input type="text" id="nameInput" placeholder="Name">
<input type="text" id="phoneInput" placeholder="Phone Number">
<button onclick="addContact()">Add</button>
<input type="text" id="searchInput" placeholder="Search by Name">
<button onclick="searchContact()">Search</button>
<h2>Contact Database</h2>
<table id="contactTable">
<thead>
<tr>
<th>Name</th>
<th>Phone Number</th>
<th>Actions</th>
</tr>
</thead>
<tbody id="contactTableBody">
<!-- Contact entries will be inserted here -->
</tbody>
</table>
<script>
let contacts = [];
function addContact() {
const name = document.getElementById('nameInput').value;
const phone = document.getElementById('phoneInput').value;
if (name && phone) {
contacts.push({ name, phone });
document.getElementById('nameInput').value = '';
document.getElementById('phoneInput').value = '';
displayContacts();
}
}
function deleteContact(index) {
contacts.splice(index, 1);
displayContacts();
}
function displayContacts() {
const tableBody = document.getElementById('contactTableBody');
tableBody.innerHTML = '';
contacts.forEach((contact, index) => {
const row = `<tr>
<td>${contact.name}</td>
<td>${contact.phone}</td>
<td><button onclick="deleteContact(${index})">Delete</button></td>
</tr>`;
tableBody.innerHTML += row;
});
}
function searchContact() {
const searchValue = document.getElementById('searchInput').value.toLowerCase();
const filteredContacts = contacts.filter(contact =>
contact.name.toLowerCase().includes(searchValue)
);
const tableBody = document.getElementById('contactTableBody');
tableBody.innerHTML = '';
filteredContacts.forEach((contact, index) => {
const row = `<tr>
<td>${contact.name}</td>
<td>${contact.phone}</td>
<td><button onclick="deleteContact(${index})">Delete</button></td>
</tr>`;
tableBody.innerHTML += row;
});
}
</script>
</body>
</html>
Explanation:
-
HTML Structure: The HTML provides inputs for entering names and phone numbers, a table to show the contacts, and buttons for adding, deleting, and searching.
-
JavaScript Logic:
- An array
contacts is maintained to simulate a contact database.
addContact: Adds a new name and phone to the list.
deleteContact: Removes a contact by its index.
displayContacts: Updates and renders the contact list in the table.
searchContact: Filters the displayed contacts by name, based on what's entered in the search input.
-
CSS: Basic styling is applied to make the interface more readable.
This setup does not involve a server-side database. For extending this to a real-world application, you'd need to integrate some backend technology such as Node.js with a database like MongoDB or MySQL.