-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
143 lines (120 loc) · 5.8 KB
/
script.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
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
document.addEventListener('DOMContentLoaded', () => {
const addPatientBtn = document.getElementById('add-patient-btn');
const patientForm = document.getElementById('patient-form');
const form = document.getElementById('form');
const patientsTableBody = document.querySelector('#patients-table tbody');
const editPatientForm = document.getElementById('edit-patient-form');
const editForm = document.getElementById('edit-form');
let editingIndex = null;
addPatientBtn.addEventListener('click', () => {
patientForm.classList.toggle('hidden');
form.reset();
});
form.addEventListener('submit', (event) => {
event.preventDefault();
const name = document.getElementById('name').value;
const apellido = document.getElementById('apellido').value;
const age = document.getElementById('age').value;
const dni = document.getElementById('dni').value;
const celular = document.getElementById('celular').value;
const email = document.getElementById('email').value;
const condition = document.getElementById('condition').value;
const newRow = document.createElement('tr');
newRow.innerHTML = `
<td>${name}</td>
<td>${apellido}</td>
<td>${age}</td>
<td>${dni}</td>
<td>${celular}</td>
<td>${email}</td>
<td>${condition}</td>
<td>
<button class="edit-btn">Editar</button>
<button class="delete-btn">Eliminar</button>
</td>
`;
patientsTableBody.appendChild(newRow);
// Añadir eventos a los nuevos botones
newRow.querySelector('.delete-btn').addEventListener('click', () => {
newRow.remove();
});
newRow.querySelector('.edit-btn').addEventListener('click', () => {
const row = newRow;
const name = row.children[0].textContent;
const apellido = row.children[1].textContent;
const age = row.children[2].textContent;
const dni = row.children[3].textContent;
const celular = row.children[4].textContent;
const email = row.children[5].textContent;
const condition = row.children[6].textContent;
document.getElementById('edit-name').value = name;
document.getElementById('edit-apellido').value = apellido;
document.getElementById('edit-age').value = age;
document.getElementById('edit-dni').value = dni;
document.getElementById('edit-celular').value = celular;
document.getElementById('edit-email').value = email;
document.getElementById('edit-condition').value = condition;
editingIndex = Array.from(patientsTableBody.children).indexOf(row);
editPatientForm.classList.remove('hidden');
patientForm.classList.add('hidden');
});
form.reset();
patientForm.classList.add('hidden');
});
editForm.addEventListener('submit', (event) => {
event.preventDefault();
const name = document.getElementById('edit-name').value;
const apellido = document.getElementById('edit-apellido').value;
const age = document.getElementById('edit-age').value;
const dni = document.getElementById('edit-dni').value;
const celular = document.getElementById('edit-celular').value;
const email = document.getElementById('edit-email').value;
const condition = document.getElementById('edit-condition').value;
if (editingIndex !== null) {
const row = patientsTableBody.children[editingIndex];
row.children[0].textContent = name;
row.children[1].textContent = apellido;
row.children[2].textContent = age;
row.children[3].textContent = dni;
row.children[4].textContent = celular;
row.children[5].textContent = email;
row.children[6].textContent = condition;
editingIndex = null;
}
editForm.reset();
editPatientForm.classList.add('hidden');
});
document.getElementById('cancel-edit').addEventListener('click', () => {
editForm.reset();
editPatientForm.classList.add('hidden');
editingIndex = null;
});
// Añadir eventos de eliminación y edición a los botones existentes (si los hubiera)
patientsTableBody.querySelectorAll('.delete-btn').forEach(button => {
button.addEventListener('click', () => {
button.closest('tr').remove();
});
});
patientsTableBody.querySelectorAll('.edit-btn').forEach(button => {
button.addEventListener('click', () => {
const row = button.closest('tr');
const name = row.children[0].textContent;
const apellido = row.children[1].textContent;
const age = row.children[2].textContent;
const dni = row.children[3].textContent;
const celular = row.children[4].textContent;
const email = row.children[5].textContent;
const condition = row.children[6].textContent;
document.getElementById('edit-name').value = name;
document.getElementById('edit-apellido').value = apellido;
document.getElementById('edit-age').value = age;
document.getElementById('edit-dni').value = dni;
document.getElementById('edit-celular').value = celular;
document.getElementById('edit-email').value = email;
document.getElementById('edit-condition').value = condition;
editingIndex = Array.from(patientsTableBody.children).indexOf(row);
editPatientForm.classList.remove('hidden');
patientForm.classList.add('hidden');
});
});
});