Skip to content

Commit 2ee0653

Browse files
committed
JS Delete fuction implemented
1 parent 50ed0cf commit 2ee0653

File tree

3 files changed

+53
-22
lines changed

3 files changed

+53
-22
lines changed

src/main/java/com/itacademy/CrudEmpleats/controller/ControllerEmployees.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.itacademy.CrudEmpleats.controller;
22

3-
import java.io.IOException;
43
import java.util.List;
54

6-
import javax.servlet.http.HttpServletResponse;
75

86
import org.springframework.beans.factory.annotation.Autowired;
97
import org.springframework.http.HttpStatus;

src/main/resources/static/index.js

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,59 @@ $(document).ready(function() {
55
$.ajax({
66
url: '/EmployeeList',
77
type: 'POST',
8+
contentType: 'application/Json',
89
data: JSON.stringify({
910
"name": $('#name').val(),
1011
"job": $('#job').val()
1112
}),
12-
processData: false,
13-
contentType: "application/json"
14-
})
15-
updateTable()
13+
success: function(data, textStatus, jQxhr) {
14+
updateTable();
15+
$('#newEmployee').trigger('reset');
16+
},
17+
error: function(xhr, textStatus, errorThrown) {
18+
console.log(textStatus);
19+
}
20+
});
21+
1622
});
1723

1824
$(function() {
1925
updateTable();
2026
});
27+
});
28+
29+
function updateTable() {
30+
$.get("/EmployeeList", function(data) {
31+
$('#employeeTable > tbody').empty();
32+
var table_data;
33+
$.each(data, function(i, employee) {
34+
table_data += "<tr>" +
35+
"<td ><form>" + employee.id + "</td>" +
36+
"<td >" + employee.name + "</td>" +
37+
"<td>" + employee.job + "</td>" +
38+
"<td>" + employee.salary + "</td>" +
39+
"<td>" +
40+
"<button type=submit>Modificar</button>" +
41+
"<button type=submit onclick='deleteEmploye(" + employee.id + ")'>Eliminar</button>" +
42+
"<form></td>"
43+
});
44+
$('#employeeTable > tbody').append(table_data);
45+
});
46+
};
2147

22-
function updateTable() {
23-
$.get("/EmployeeList", function(data) {
24-
$('#employeeTable > tbody').empty()
25-
$.each(data, function(i, employee) {
26-
var tblRow = "<tr>" +
27-
"<td>" + employee.id + "</td>" +
28-
"<td>" + employee.name + "</td>" +
29-
"<td>" + employee.job + "</td>" +
30-
"<td>" + employee.salary + "</td>" +
31-
"<td>" +
32-
"<button type=submit>Modificar</button>" +
33-
"<button type=submit>Eliminar</button>" +
34-
"</td>"
35-
$(tblRow).appendTo("#employeeTable tbody");
36-
});
48+
function deleteEmploye(id) {
49+
if (confirm("Desitja eliminar a l'Empleat?")) {
50+
$.ajax({
51+
type: "DELETE",
52+
url: "/EmployeeList/" + id,
53+
success: function(data, textStatus, jQxhr) {
54+
updateTable();
55+
},
56+
error: function(xhr, textStatus, errorThrown) {
57+
console.log(textStatus);
58+
}
3759
});
60+
3861
};
39-
});
62+
63+
};

src/main/resources/templates/index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ <H4>Empleats</H4><button type="submit">Nou Empleat</button>
2727
</tr>
2828
</thead>
2929
<tbody>
30+
<form> <tr>
31+
<td id=employeeId>IdExempleAtHtml</td>
32+
<td id=employeeName>NameExempleAtHtml </td>
33+
<td> JobExempleAtHtml</td>
34+
<td> SalaryExempleAtHtml </td>
35+
<td>
36+
<button type=submit>Modificar</button>
37+
<button type=submit id=deleteEmployee>Eliminar</button>
38+
</form></td>
3039
</tbody>
3140
</table>
3241
</div>

0 commit comments

Comments
 (0)