J'ai modifié le code précédent pendant que tu testais ...
je récupère le data et fais un each dessus pour peupler une liste avec le name ...
Version imprimable
J'ai modifié le code précédent pendant que tu testais ...
je récupère le data et fais un each dessus pour peupler une liste avec le name ...
Merci indéfiniment de votre aide....:zoubi: cela fonctionne a merveillle
Mais le seul hic est que je voudrais transmettre la variable que je rempli dans success de ajax dans mon select....la variable est array...
Code:
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 var $categorySelect = $('#categorySelect'); var $categoryForm = $('#categoryForm'); var $listPoints = $('#list-interest-points'); var array = {}; // load categoryName(select) and InterestPoints(list) $.ajax({ type: "GET", url: '/index_points_category/', dataType: 'json', success: function(data) { $.each(data, function(key,value) { var categoryName = key; var arrayPoints = value; array.categoryName = arrayPoints; }); } }); var currentJson; hash_sort_cat= {"Gare":[{"address":"","city":"","latitude":null,"longitude":null,"name":"test","postal_code":""},{"address":null,"city":null,"latitude":null,"longitude":null,"name":"test","postal_code":null}],"Aeroport":[{"address":null,"city":null,"latitude":null,"longitude":null,"name":"test_aero","postal_code":null},{"address":null,"city":null,"latitude":null,"longitude":null,"name":"essai","postal_code":null}],"Hopital":[{"address":null,"city":null,"latitude":null,"longitude":null,"name":"test_hop1","postal_code":null},{"address":null,"city":null,"latitude":null,"longitude":null,"name":"test_hop2","postal_code":null}]} $.each(hash_sort_cat, function(key,val){ $categorySelect.append($("<option/>").val(key).text(key).data('points',val)); }) $categorySelect.on('change', function(){ currentJson = $(this).find(':selected').data('points'); $listPoints.empty(); $.each(currentJson, function(idx,val){ $listPoints.append($('<a href="#" class="list-group-item">').text(val.name)) ; }); })
ben intègre simplement le code dans le success en utilisant data comme json ...
Code:
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 var $categorySelect = $('#categorySelect'); var $categoryForm = $('#categoryForm'); var $listPoints = $('#list-interest-points'); var currentJson; // load categoryName(select) and InterestPoints(list) $.ajax({ type: "GET", url: '/index_points_category/', dataType: 'json', success: function(data) { $categorySelect.empty(); $.each(hash_sort_cat, function(key,val){ $categorySelect.append($("<option/>").val(key).text(key).data('points',val)); }) }); } }); $categorySelect.on('change', function(){ currentJson = $(this).find(':selected').data('points'); }) $categorySelect.on('change', function(){ $listPoints.empty(); $.each(currentJson, function(idx,val){ $listPoints.append($('<a class="list-group-item">').text(val.name)) ; }) });
Impossible je ne souhaiterais n'avoir qu'une seule requéte...remplir mon array et ensuite traiter mon array en dehors de ma function ajax...
Je sais que Ajax est asynchrone....
tu ne pourras peupler tes liste qu'une fois l'ajax accompli ... donc dans le success
cf ci dessus
Aucune autre solution ?
Merci de votre aide, toute au long de la journée ^^
Passer une bonne soirée.
tu peux sur le success renseigner le current json ...
puis l'utiliser plus tard
Mais je ne vois pas bien la chronologie de ton truc ...
J'imagine un truc du genre :
sur le success tu renseignes le currenJson ...
et tu affiches un bouton
au click du bouton la liste se peuple
ça c'est possible
Ok moi je voudrais récupérer mon data le passer dans un array pour m'en servir plus tard...
Merci quand même pour votre aide, vous m'avez fais découvrir des trucs que je ne connaissais pas et ça c'est cool ^^
Cordialement
je ne comprends pas ce que tu veux mettre dans ta liste ???Citation:
.mais il faut virer interest.name
des liens href à priori ? tu veux mettre quoi comme texte si tu ne mets pas le name ?
Bonjour,
Au final mon problème est résolu...voici le code final :
Merci pour votre aide .Code:
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 //ajax interest points var $categorySelect = $('#categorySelect'); var $categoryForm = $('#categoryForm'); var $listPoints = $('#list-interest-points'); var array = {}; $.ajax({ type: "GET", url: '/index_points_category/', dataType: 'json', success: function(data) { alert("ok"); CreateSelectAndList(data) } }); function CreateSelectAndList(data){ var currentJson; $.each(data, function(key,val){ $categorySelect.append($("<option/>").val(key).text(key).data('points',val)); }) $categorySelect.on('change', function(){ currentJson = $(this).find(':selected').data('points'); $listPoints.empty(); $.each(currentJson, function(key,val){ $listPoints.append($('<a href="#" class="list-group-item">').text(val.name)) ; }); }); }
Cordialement