Bonjour,
Je lis un fichier json, quand j'utilise (var json = mes donneés), et j'applique une fonction pour organiser par alphabet, tout va bien, mais quand j'utilise getJson, je perds l'ordre par alphabet:
Fonction
Code json
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 function sortByName(idList){ var $peopleli = $('#'+idList).children(); $peopleli.sort(function(a,b){ var an = a.id, bn = b.id; return ((an > bn) ? 1 : -1); }); var xy = $peopleli.appendTo("#"+idList); return xy; } // end function sortByName()
jQuery:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 var data = [ { top_en: "Vehicles", top_tran: "Veh", }, { top_en: "Estate", top_tran: "Est", }, { top_en: "High-Tech", top_tran: "Hig", } ];
Merci à vous
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 $('.searchArea').on('click', function(){ var getID = $(this).attr('id'); var jsonFile, something; switch(getID){ case 'categories': jsonFile = 'CHEMIN-VERS-JSON/categories.json'; break; } // end switch var getLang = detectLang()[0]; var itemsList = []; var itemsListKey = []; // datas pour lire le fichier json // et data pour lire la variable $.getJSON(jsonFile, function( datas ) { $.each(datas, function(ind,val){ /* lire a partir du fichier json */ var showItems = val['cat_'+getLang]; // top_fr / top_en / top_ar var showKeys = val['cat_key']; /* lire a partir de la variable json */ //var showItems = val['top_en']; //var showKeys = val['top_tran']; itemsList.push(showItems); itemsListKey.push(showKeys); }); // end each var buildCats = ''; $.each(itemsList, function(key, value){ buildCats += '<li id="'+itemsListKey[key]+'">'+value+'</li>'; }); console.log(buildCats); $('#list-'+getID).html(buildCats); $('#list-'+getID).html(sortByName('list-'+getID)); }); // end getJSON if($("#list-"+getID).is(":hidden")){ console.log('Opened'); $('.arrowDrop-'+getID).html('<img src="https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-arrow-up-b-16.png">'); } else { console.log('Closed'); $('.arrowDrop-'+getID).html('<img src="https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-arrow-down-b-16.png">'); } $('#list-'+getID).slideToggle(); }); // end click
Partager