-
Notifications
You must be signed in to change notification settings - Fork 0
/
crud.js
89 lines (82 loc) · 3 KB
/
crud.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const preExistingData = [
{
name: "John Doe",
company: "ABC Inc.",
designation: "Manager",
phone: "1234567890",
email: "[email protected]"
},
{
name: "Jane Smith",
company: "XYZ Corp.",
designation: "Engineer",
phone: "9876543210",
email: "[email protected]"
},
{
name: "Angela Yu",
company: "ABC_INCORP",
designation: "SDE",
phone: "9009009870",
email: "[email protected]"
}
];
let editingIndex = -1;
function populateTableWithPreExistingData() {
const tableBody = document.querySelector("#contactTable tbody");
let html = "";
preExistingData.forEach((contact, index) => {
const serialNumber = index + 1;
html += `
<tr>
<td>${serialNumber}</td>
<td>${contact.name}</td>
<td>${contact.company}</td>
<td>${contact.designation}</td>
<td>${contact.phone}</td>
<td>${contact.email}</td>
<td class="edit-icon" onclick="editContact(${index})">Edit</td>
<td class="delete-icon" onclick="deleteContact(${index})">Delete</td>
</tr>
`;
});
tableBody.innerHTML = html;
}
function addOrEditContact(event) {
event.preventDefault();
const name = document.getElementById("name").value.trim();
const company =document.getElementById("company").value.trim();
const designation = document.getElementById("designation").value.trim();
const phone = document.getElementById("phone").value.replace(/-/g, '').trim();
const email = document.getElementById("email").value.trim();
const newContact = { name, company, designation, phone, email };
if (editingIndex === -1) {
preExistingData.push(newContact);
} else {
preExistingData[editingIndex] = newContact;
document.getElementById("submitContactBtn").innerText = "Add Contact";
}
populateTableWithPreExistingData();
document.getElementById("contactForm").reset();
}
function editContact(index) {
editingIndex = index;
const contact = preExistingData[index];
document.getElementById("name").value = contact.name;
document.getElementById("company").value = contact.company;
document.getElementById("designation").value = contact.designation;
document.getElementById("phone").value = contact.phone;
document.getElementById("email").value = contact.email;
document.getElementById("submitContactBtn").innerText = "Save";
}
function deleteContact(index) {
const confirmation = confirm("Do you want to Delete the Entire Row??");
if (confirmation) {
preExistingData.splice(index, 1);
populateTableWithPreExistingData();
}
}
document.getElementById("contactForm").addEventListener('submit', function(event) {
addOrEditContact(event);
});
populateTableWithPreExistingData();