11
2- $ ( document ) . ready ( function ( ) {
3- $ ( '#newEmployee' ) . submit ( function ( evento ) {
4- evento . preventDefault ( ) ;
2+ $ ( document ) . ready ( updateTable ( ) ) ;
3+
4+ //Read employee List & Update the table
5+ function updateTable ( ) {
6+ $ . get ( "/EmployeeList" , function ( data ) {
7+ $ ( '#employeeTable > tbody' ) . empty ( ) ;
8+ var table_data ;
9+ $ . each ( data , function ( i , employee ) {
10+ table_data += "<tr>" +
11+ "<td>" + employee . id + "</td>" +
12+ "<td>" + employee . name + "</td>" +
13+ "<td>" + employee . job + "</td>" +
14+ "<td>" + employee . salary + "</td>" +
15+ "<td>" +
16+ "<button type='button'onclick='editEmployee(" + employee . id + ")'>Modificar</button>" +
17+ "<button type='button' onclick='deleteEmployee(" + employee . id + ")'>Eliminar</button>" +
18+ "</td>"
19+ } ) ;
20+ $ ( '#employeeTable > tbody' ) . append ( table_data ) ;
21+ } ) ;
22+ } ;
23+
24+ //Create & update
25+ function sendEmployee ( ) {
26+ //create employee
27+ if ( $ ( '#action' ) . val ( ) == 'Afegir' ) {
528 $ . ajax ( {
629 url : '/EmployeeList' ,
730 type : 'POST' ,
@@ -10,54 +33,75 @@ $(document).ready(function() {
1033 "name" : $ ( '#name' ) . val ( ) ,
1134 "job" : $ ( '#job' ) . val ( )
1235 } ) ,
13- success : function ( data , textStatus , jQxhr ) {
36+ success : function ( data , textStatus , jQxhr ) {
1437 updateTable ( ) ;
15- $ ( '#newEmployee' ) . trigger ( 'reset' ) ;
1638 } ,
17- error : function ( xhr , textStatus , errorThrown ) {
39+ error : function ( xhr , textStatus , errorThrown ) {
1840 console . log ( textStatus ) ;
1941 }
2042 } ) ;
21-
22- } ) ;
23-
24- $ ( function ( ) {
25- updateTable ( ) ;
26- } ) ;
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+ //Update employee
44+ } if ( $ ( '#action' ) . val ( ) == 'Actualitzar' ) {
45+ $ . ajax ( {
46+ url : '/EmployeeList/' + $ ( '#idEmployee' ) . val ( ) ,
47+ type : 'PUT' ,
48+ contentType : 'application/Json' ,
49+ data : JSON . stringify ( {
50+ "id" : $ ( '#idEmployee' ) . val ( ) ,
51+ "name" : $ ( '#name' ) . val ( ) ,
52+ "job" : $ ( '#job' ) . val ( )
53+ } ) ,
54+ success : function ( data , textStatus , jQxhr ) {
55+ updateTable ( ) ;
56+ } ,
57+ error : function ( xhr , textStatus , errorThrown ) {
58+ console . log ( textStatus ) ;
59+ }
4360 } ) ;
44- $ ( '#employeeTable > tbody' ) . append ( table_data ) ;
45- } ) ;
61+ }
62+ $ ( '#EmployeeForm' ) . trigger ( 'reset' ) ;
63+ $ ( '#action' ) . val ( 'Afegir' )
4664} ;
4765
48- function deleteEmploye ( id ) {
66+ //Delete employee
67+ function deleteEmployee ( id ) {
4968 if ( confirm ( "Desitja eliminar a l'Empleat?" ) ) {
5069 $ . ajax ( {
5170 type : "DELETE" ,
5271 url : "/EmployeeList/" + id ,
53- success : function ( data , textStatus , jQxhr ) {
72+ success : function ( data , textStatus , jQxhr ) {
5473 updateTable ( ) ;
5574 } ,
56- error : function ( xhr , textStatus , errorThrown ) {
75+ error : function ( xhr , textStatus , errorThrown ) {
5776 console . log ( textStatus ) ;
5877 }
5978 } ) ;
60-
6179 } ;
80+ } ;
6281
82+ //Read one employee info and put in the modifier form.
83+ function editEmployee ( id ) {
84+ $ . get ( "/EmployeeList/" + id , function ( data ) {
85+ $ ( '#idEmployee' ) . val ( data . id ) ;
86+ $ ( '#name' ) . val ( data . name ) ;
87+ $ ( '#job' ) . val ( getJobCode ( data . job ) ) ;
88+ } ) ;
89+ $ ( '#action' ) . val ( 'Actualitzar' ) ;
6390} ;
91+
92+ //Take the enum codes for Employee Jobs.
93+ function getJobCode ( job ) {
94+ if ( job == 'Director de projectes' ) {
95+ return 'Director_Projectes' ;
96+ } if ( job == 'Programador Senior' ) {
97+ return 'Programador_Senior' ;
98+ } if ( job == 'Programador_Mid' ) {
99+ return 'Programador_Mid' ;
100+ } if ( job == 'Programador Junior' ) {
101+ return 'Programador_Junior' ;
102+ } if ( job == 'Administratiu' ) {
103+ return 'Administratiu' ;
104+ } else {
105+ return "Default" ;
106+ }
107+ }
0 commit comments