-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.html
161 lines (143 loc) · 5.13 KB
/
data.html
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="hlo.css">
<title>Data</title>
</head>
<body>
<h1 class="title">GDSC Core Team Member :)</h1>
<div class="n">
Name: <input type="text" id="n"><br>
</div>
<div class="r">
Role: <input type="text" id="r">
</div>
<div class="e">
Email: <input type="email" id="e">
</div>
<div class="p">
Phone Number: <input type="tel" id="p" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}">
</div>
<div class="add">
<button onclick="add()">Add Member</button>
</div>
<div class="container">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Role</th>
<th>Email</th>
<th>Phone Number</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>Amruta</td>
<td>Web</td>
<td>[email protected]</td>
<td>1234567890</td>
<td><button onclick="edit(this)" class="btn">Edit</button></td>
<td><button onclick="remove(this)" class="btn">Remove</button></td>
</tr>
</tbody>
</table>
</div>
<script>
let table = document.querySelector('.table');
function add() {
let newRow = table.insertRow();
var nam = document.getElementById('n').value;
var rol = document.getElementById('r').value;
var em = document.getElementById('e').value;
var ph = document.getElementById('p').value;
var cell1 = newRow.insertCell(0);
var cell2 = newRow.insertCell(1);
var cell3 = newRow.insertCell(2);
var cell4 = newRow.insertCell(3);
var cell5 = newRow.insertCell(4);
var cell6 = newRow.insertCell(5);
cell1.textContent = nam;
cell2.textContent = rol;
cell3.textContent = em;
cell4.textContent = ph;
cell5.innerHTML = `<button onclick="edit(this)" class="btn">Edit</button>`;
cell6.innerHTML = `<button onclick="remove(this)" class="btn">Remove</button>`;
sendDataToServer(nam, rol, em, ph);
}
async function sendDataToServer(name, role, email, phoneNumber) {
try {
const response = await fetch('/api/items', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ name, role, email, phoneNumber }) // Adjust properties as needed
});
if (response.ok) {
console.log('Item saved successfully');
} else {
console.error('Failed to save item');
}
} catch (error) {
console.error('An error occurred:', error);
}
}
function edit(button) {
var row = button.parentNode.parentNode;
var nameCell = row.cells[0];
var roleCell = row.cells[1];
var emailCell = row.cells[2];
var phoneNumberCell = row.cells[3];
var nameInput = document.createElement('input');
nameInput.type = 'text';
nameInput.value = nameCell.textContent;
nameCell.innerHTML = '';
nameCell.appendChild(nameInput);
var roleInput = document.createElement('input');
roleInput.type = 'text';
roleInput.value = roleCell.textContent;
roleCell.innerHTML = '';
roleCell.appendChild(roleInput);
var emailInput = document.createElement('input');
emailInput.type = 'email';
emailInput.value = emailCell.textContent;
emailCell.innerHTML = '';
emailCell.appendChild(emailInput);
var phoneNumberInput = document.createElement('input');
phoneNumberInput.type = 'tel';
phoneNumberInput.value = phoneNumberCell.textContent;
phoneNumberCell.innerHTML = '';
phoneNumberCell.appendChild(phoneNumberInput);
var editButton = row.cells[4].querySelector('button');
editButton.textContent = 'Save';
editButton.onclick = function() {
save(row);
};
}
function save(row) {
var nameInput = row.cells[0].querySelector('input');
var roleInput = row.cells[1].querySelector('input');
var emailInput = row.cells[2].querySelector('input');
var phoneNumberInput = row.cells[3].querySelector('input');
row.cells[0].innerHTML = nameInput.value;
row.cells[1].innerHTML = roleInput.value;
row.cells[2].innerHTML = emailInput.value;
row.cells[3].innerHTML = phoneNumberInput.value;
var editButton = row.cells[4].querySelector('button');
editButton.textContent = 'Edit';
editButton.onclick = function() {
edit(this);
};
}
function remove(button) {
var row = button.parentNode.parentNode;
row.remove();
}
</script>
</body>
</html>