File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 3131 </thead >
3232 <c:forEach items =" ${ requestScope. users } " var =" user" >
3333 <jsp:useBean id =" user" type =" ru.javawebinar.topjava.model.User" />
34- <tr >
34+ <tr id = " ${ user . id } " >
3535 <td ><c:out value =" ${ user. name } " /></td >
3636 <td ><a href =" mailto:${ user. email } " >${ user. email } </a ></td >
3737 <td >${ user. roles } </td >
38- <td ><input type =" checkbox" <c:if test =" ${ user. enabled } " >checked</c:if > id=" ${ user . id } " /></td >
38+ <td ><input type =" checkbox" <c:if test =" ${ user. enabled } " >checked</c:if >/></td >
3939 <td ><fmt:formatDate value =" ${ user. registered } " pattern =" dd-MMMM-yyyy" /></td >
4040 <td ><a ><span class =" fa fa-pencil" ></span ></a ></td >
41- <td ><a class =" delete" id = " ${ user . id } " ><span class =" fa fa-remove" ></span ></a ></td >
41+ <td ><a class =" delete" ><span class =" fa fa-remove" ></span ></a ></td >
4242 </tr >
4343 </c:forEach >
4444 </table >
Original file line number Diff line number Diff line change 1- function makeEditable ( ) {
1+ let form ;
2+
3+ function makeEditable ( datatableApi ) {
4+ ctx . datatableApi = datatableApi ;
5+ form = $ ( '#detailsForm' ) ;
26 $ ( ".delete" ) . click ( function ( ) {
3- deleteRow ( $ ( this ) . attr ( "id" ) ) ;
7+ if ( confirm ( 'Are you sure?' ) ) {
8+ deleteRow ( $ ( this ) . closest ( 'tr' ) . attr ( "id" ) ) ;
9+ }
410 } ) ;
511}
612
713function add ( ) {
8- $ ( "#detailsForm" ) . find ( ":input" ) . val ( "" ) ;
14+ form . find ( ":input" ) . val ( "" ) ;
915 $ ( "#editRow" ) . modal ( ) ;
1016}
1117
1218function deleteRow ( id ) {
1319 $ . ajax ( {
14- url : ajaxUrl + id ,
15- type : "DELETE" ,
16- success : function ( ) {
17- updateTable ( ) ;
18- }
20+ url : ctx . ajaxUrl + id ,
21+ type : "DELETE"
22+ } ) . done ( function ( ) {
23+ updateTable ( ) ;
1924 } ) ;
2025}
2126
2227function updateTable ( ) {
23- $ . get ( ajaxUrl , function ( data ) {
24- datatableApi . clear ( ) . rows . add ( data ) . draw ( ) ;
28+ $ . get ( ctx . ajaxUrl , function ( data ) {
29+ ctx . datatableApi . clear ( ) . rows . add ( data ) . draw ( ) ;
2530 } ) ;
2631}
2732
2833function save ( ) {
29- const form = $ ( "#detailsForm" ) ;
3034 $ . ajax ( {
3135 type : "POST" ,
32- url : ajaxUrl ,
33- data : form . serialize ( ) ,
34- success : function ( ) {
35- $ ( "#editRow" ) . modal ( "hide" ) ;
36- updateTable ( ) ;
37- }
36+ url : ctx . ajaxUrl ,
37+ data : form . serialize ( )
38+ } ) . done ( function ( ) {
39+ $ ( "#editRow" ) . modal ( "hide" ) ;
40+ updateTable ( ) ;
3841 } ) ;
3942}
Original file line number Diff line number Diff line change 1- const ajaxUrl = "admin/users/" ;
2- let datatableApi ;
1+ const userAjaxUrl = "admin/users/" ;
2+
3+ // https://stackoverflow.com/a/5064235/548473
4+ const ctx = {
5+ ajaxUrl : userAjaxUrl
6+ } ;
37
48// $(document).ready(function () {
59$ ( function ( ) {
6- datatableApi = $ ( "#datatable" ) . DataTable ( {
7- "paging" : false ,
8- "info" : true ,
9- "columns" : [
10- {
11- "data" : "name"
12- } ,
13- {
14- "data" : "email"
15- } ,
16- {
17- "data" : "roles"
18- } ,
19- {
20- "data" : "enabled"
21- } ,
22- {
23- "data" : "registered"
24- } ,
25- {
26- "defaultContent" : "Edit" ,
27- "orderable" : false
28- } ,
29- {
30- "defaultContent" : "Delete" ,
31- "orderable" : false
32- }
33- ] ,
34- "order" : [
35- [
36- 0 ,
37- "asc"
10+ makeEditable (
11+ $ ( "#datatable" ) . DataTable ( {
12+ "paging" : false ,
13+ "info" : true ,
14+ "columns" : [
15+ {
16+ "data" : "name"
17+ } ,
18+ {
19+ "data" : "email"
20+ } ,
21+ {
22+ "data" : "roles"
23+ } ,
24+ {
25+ "data" : "enabled"
26+ } ,
27+ {
28+ "data" : "registered"
29+ } ,
30+ {
31+ "defaultContent" : "Edit" ,
32+ "orderable" : false
33+ } ,
34+ {
35+ "defaultContent" : "Delete" ,
36+ "orderable" : false
37+ }
38+ ] ,
39+ "order" : [
40+ [
41+ 0 ,
42+ "asc"
43+ ]
3844 ]
39- ]
40- } ) ;
41- makeEditable ( ) ;
45+ } )
46+ ) ;
4247} ) ;
You can’t perform that action at this time.
0 commit comments