Skip to content

Commit 50ed0cf

Browse files
committed
JS get table
1 parent 5056e58 commit 50ed0cf

File tree

3 files changed

+52
-39
lines changed

3 files changed

+52
-39
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,35 @@ void start(HttpServletResponse reponse) throws IOException {
2929
}*/
3030

3131
// Crear nou empleat
32-
@PostMapping( "/EmpleatList")
32+
@PostMapping( "/EmployeeList")
3333
@ResponseStatus(HttpStatus.CREATED) // 201
3434
public void addEmployee(@RequestBody Employee employee) {
3535
repositori.addEmployee(employee);
3636

3737
}
3838

3939
// Retorna una llista JSon d'empleats
40-
@GetMapping("/EmpleatList")
40+
@GetMapping("/EmployeeList")
4141
public List<Employee> allEmployees() {
4242
return repositori.getAllEmployees();
4343
}
4444

4545
// Busca un empleat per Id
46-
@GetMapping("/EmpleatList/{id}")
46+
@GetMapping("/EmployeeList/{id}")
4747
public Employee getFirstEmployee(@PathVariable("id") int id) {
4848
return repositori.getEmployeeById(id);
4949
}
5050

5151
// Actualitzar Empleat
52-
@PutMapping("/EmpleatList/{id}")
52+
@PutMapping("/EmployeeList/{id}")
5353
@ResponseStatus(HttpStatus.ACCEPTED) // 202
5454
public Employee updateEmployee(@RequestBody Employee employee, @PathVariable("id") int id) {
5555
repositori.updateEmployee(employee,id);
5656
return repositori.getEmployeeById(id);
5757
}
5858

5959
// Eliminar Empleat
60-
@DeleteMapping("/EmpleatList/{id}")
60+
@DeleteMapping("/EmployeeList/{id}")
6161
@ResponseStatus(HttpStatus.ACCEPTED) // 202
6262
public String deleteEmployee(@PathVariable("id") int id) {
6363
repositori.deleteEmployee(id);

src/main/resources/static/index.js

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,39 @@
11

2-
$(document).ready(function () {
3-
$('#newEmployee').submit(function (evento) {
4-
$.ajax({
5-
url: '/EmpleatList',
6-
type: 'POST',
7-
data: JSON.stringify({
8-
"name": $('#name').val(),
9-
"job": $('#job').val()
10-
}),
11-
processData: false,
12-
contentType: "application/json"
13-
})
14-
evento.preventDefault();
15-
});
16-
});
2+
$(document).ready(function() {
3+
$('#newEmployee').submit(function(evento) {
4+
evento.preventDefault();
5+
$.ajax({
6+
url: '/EmployeeList',
7+
type: 'POST',
8+
data: JSON.stringify({
9+
"name": $('#name').val(),
10+
"job": $('#job').val()
11+
}),
12+
processData: false,
13+
contentType: "application/json"
14+
})
15+
updateTable()
16+
});
17+
18+
$(function() {
19+
updateTable();
20+
});
21+
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+
});
37+
});
38+
};
39+
});

src/main/resources/templates/index.html

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,19 @@ <H4>Empleats</H4><button type="submit">Nou Empleat</button>
1717
</div>
1818
<div>
1919
<table id="employeeTable" border="1">
20-
<tr>
21-
<th>#</th>
22-
<th>Nom</th>
23-
<th>Càrreg</th>
24-
<th>Salari</th>
25-
<th>Accions</th>
26-
</tr>
27-
<tbody>
20+
<thead>
2821
<tr>
29-
<th>0</th>
30-
<td>Gerard</td>
31-
<td>Programador Junior</td>
32-
<td>puiglatorre@gmail.com</td>
33-
<td>
34-
<button type="submit">Modificar</button>
35-
<button type="submit">Eliminar</button>
36-
</td>
22+
<th>#</th>
23+
<th>Nom</th>
24+
<th>Càrreg</th>
25+
<th>Salari</th>
26+
<th>Accions</th>
3727
</tr>
38-
28+
</thead>
29+
<tbody>
3930
</tbody>
4031
</table>
4132
</div>
42-
</div>
4333
<div class="container"></div>
4434
<div>
4535
<H4>Crear Empleat</H4>
@@ -65,7 +55,7 @@ <H4>Crear Empleat</H4>
6555
<div>
6656
<br><br>
6757
<input type=submit name="submit" value="Afegir">
68-
<input type=reset value="Cancellar">
58+
<input type=reset value="Cancellar">
6959
</div>
7060
</form>
7161
</div>

0 commit comments

Comments
 (0)