-
Notifications
You must be signed in to change notification settings - Fork 0
/
index2.html
115 lines (101 loc) · 3.64 KB
/
index2.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
<!DOCTYPE html>
<html>
<head>
<title>CRUD Operations</title>
</head>
<body>
<h1>CRUD Operations</h1>
<form id="crudForm">
<label for="name">Name:</label>
<input type="text" id="name" required />
<label for="age">Age:</label>
<input type="number" id="age" required />
<label for="gender">Gender:</label>
<select id="gender" required>
<option value="male">Male</option>
<option value="female">Female</option>
<option value="other">Other</option>
</select>
<button type="submit" id="saveBtn">Save</button>
<button type="button" id="updateBtn" style="display: none">Update</button>
<button type="button" id="cancelBtn" style="display: none">Cancel</button>
</form>
<table id="dataTable" border="1">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>Gender</th>
<th>Actions</th>
</tr>
</thead>
<tbody id="dataBody">
<!-- Data will be added here dynamically using JavaScript -->
</tbody>
</table>
<script>
// Mock database (replace with your server-side API)
let data = [];
// Function to render data in the table
function renderData() {
const tableBody = document.getElementById("dataBody");
tableBody.innerHTML = "";
data.forEach((record, index) => {
const row = document.createElement("tr");
row.innerHTML = `
<td>${index + 1}</td>
<td>${record.name}</td>
<td>${record.age}</td>
<td>${record.gender}</td>
<td>
<button onclick="editRecord(${index})">Edit</button>
<button onclick="deleteRecord(${index})">Delete</button>
</td>
`;
tableBody.appendChild(row);
});
}
// Function to handle form submission
document.getElementById("crudForm").addEventListener("submit", function (event) {
event.preventDefault();
const name = document.getElementById("name").value;
const age = document.getElementById("age").value;
const gender = document.getElementById("gender").value;
// Create or update the record in the mock database
const record = { name, age, gender };
if (this.dataset.mode === "edit") {
const index = parseInt(this.dataset.index);
data[index] = record;
} else {
data.push(record);
}
// Clear the form and re-render the table
this.reset();
renderData();
});
// Function to handle edit record button
function editRecord(index) {
const record = data[index];
document.getElementById("name").value = record.name;
document.getElementById("age").value = record.age;
document.getElementById("gender").value = record.gender;
// Set the form mode to edit and update the dataset attributes
const form = document.getElementById("crudForm");
form.dataset.mode = "edit";
form.dataset.index = index;
// Show/hide buttons based on form mode
document.getElementById("saveBtn").style.display = "none";
document.getElementById("updateBtn").style.display = "inline";
document.getElementById("cancelBtn").style.display = "inline";
}
// Function to handle delete record button
function deleteRecord(index) {
data.splice(index, 1);
renderData();
}
// Initial rendering of data in the table
renderData();
</script>
</body>
</html>