1
+ <!DOCTYPE html>
2
+ < html lang ="en ">
3
+ < head >
4
+ < meta charset ="UTF-8 ">
5
+ < meta name ="viewport " content ="width=device-width, initial-scale=1 ">
6
+ < title > Test AJAX with jQuery</ title >
7
+ < script src ="https://code.jquery.com/jquery-3.6.0.min.js "> </ script >
8
+ </ head >
9
+ < body >
10
+ < div >
11
+ < button id ="btnTousLivres "> Tous les livres</ button >
12
+ < button id ="btnDerniersLivres "> Derniers livres</ button >
13
+ </ div >
14
+ < div id ="books "> </ div >
15
+ < div id ="Templates " style ="display:none ">
16
+ < ul id ="tplListeLivres ">
17
+ < li class ="item "> < a href ="" class ="link "> titre</ a > </ li >
18
+ </ ul >
19
+ </ div >
20
+ < script >
21
+ function chargeLivresParCode ( url ) {
22
+ $ . ajax ( {
23
+ url : url ,
24
+ type : 'get' ,
25
+ dataType : 'json' ,
26
+ success : ( tabLivres ) => {
27
+ // console.log(tabLivres);
28
+ let liste = $ ( '<ul>' ) ;
29
+ for ( let i = 0 ; i < tabLivres . length ; i ++ ) {
30
+ livre = tabLivres [ i ] ;
31
+ liste . append ( $ ( '<li>' ) . append ( $ ( '<a>' ) . attr ( 'href' , livre . url ) . text ( livre . name ) ) ) ;
32
+ }
33
+ $ ( '#books' ) . empty ( ) . append ( liste ) ;
34
+ }
35
+ } ) ;
36
+ }
37
+
38
+ function chargeLivresTemplates ( url ) {
39
+ $ . ajax ( {
40
+ url : url ,
41
+ type : 'get' ,
42
+ dataType : 'json' ,
43
+ success : ( tabLivres ) => {
44
+ // console.log(tabLivres);
45
+ let liste = $ ( '#tplListeLivres' ) . clone ( ) . attr ( 'id' , '' ) ;
46
+ tabLivres . forEach ( ( livre ) => {
47
+ // console.log(livre);
48
+ let item = $ ( '.item' , liste ) . clone ( ) . removeClass ( 'item' ) ;
49
+ // $('.link', item).attr('href', livre.url);
50
+ // $('.link', item).text(livre.name);
51
+ $ ( '.link' , item ) . attr ( 'href' , livre . url ) . text ( livre . name ) ;
52
+ liste . append ( item ) ;
53
+ } ) ;
54
+ $ ( '.item' , liste ) . remove ( ) ;
55
+ $ ( '#books' ) . empty ( ) . append ( liste ) ;
56
+ }
57
+ } ) ;
58
+ }
59
+
60
+ $ ( document ) . ready ( function ( ) {
61
+ $ ( '#btnTousLivres' ) . click ( function ( ) {
62
+ // chargeLivresParCode('https://delphi-books.com/api/b/all.json');
63
+ chargeLivresTemplates ( 'https://delphi-books.com/api/b/all.json' ) ;
64
+ } ) ;
65
+
66
+ $ ( '#btnDerniersLivres' ) . click ( function ( ) {
67
+ // chargeLivresParCode('https://delphi-books.com/api/b/lastyear.json');
68
+ chargeLivresTemplates ( 'https://delphi-books.com/api/b/lastyear.json' ) ;
69
+ } ) ;
70
+ } ) ;
71
+ </ script >
72
+ </ body >
73
+ </ html >
0 commit comments