2016-04-08 25 views
1

Komut dizimde each işlevini kullanmak istiyorum ancak bu işlev, bir hata döndürdüğü anlamına gelen bir nesnede kullanılır ... each kullanmak istediğiniz nesne newPlots'dur.jQuery, her bir nesnedeki bir nesneyi kullanır

Belki bir numara biliyor musunuz?

AJAX tarafından döndürülen içeriği kullanarak newPlots numaralı grafikleri eklemek istiyorum.

   var newPlots = { 
        "City" : { 
         x : 45.834444, 
         y : 1.261667, 
         text : {content : "City"}, 
         tooltip : {content : "City"} 
        } 
        , "City2" : { 
         size:60, 
         x : 47.323056, 
         y : 5.041944, 
         text : { 
          content : "City", 
          position : "left", 
          margin : 5 
         } 
        } 
       } 

Ve yapmak istediğim şey newPlots yılında each işlevini kullanarak geçerli: düz metin olarak, newPlots buna benziyor. Eğer newPlots uzatmak için denemek

$.ajax({ 
type: "post", 
url: '<?php echo $this->Url->build(array('controller'=>'Villes','action'=>'viewresult')); ?>', 
data: { 
    departements: dptcode 
}, 
dataType : "json", 
success: function (ret) { 
      // Update some plots and areas attributes ... 
      var updatedOptions = {'plots' : {}}; 
      // add some new plots .. 
      var newPlots = { 

       $.each(ret, function(index, element) { 
         "Limoge" : { 
          x : element.x, 
          y : element.y, 
          text : {content : "Limoge"}, 
          tooltip : {content : "Limoge"} 
         }, 
       }); 



      } 

      // and delete some others ... 
      var deletedPlots = []; 

      $(".mapcontainer").trigger('update', [updatedOptions, newPlots, deletedPlots, {animDuration : 1000}]); 




}, 
error : function(xhr, textStatus, errorThrown) { 
    alert('An error occurred! ' + errorThrown); 
} 
}); 
+0

... – FabriceDouceur

+2

Yani bascically, nesnelerden dizisini almak istiyorum ??? Kodunuz anlam ifade etmiyor ve beklenen davranışınızın ne olduğunu açıklamıyorsunuz, bunu anlamak zor… –

+0

Bunu değiştirin -> var newPlots = $ .map (ret, function (index, eleman) {dönüş "Limoge": { x: element.x, y: element.y, metin: {içeriği: "Limoge"}, ipucu: {içeriği: "Limoge"} } }); – Amir

cevap

1

yolu sözdizimsel doğru değil:

Burada kodumu bulabilirsiniz.

deneyin yapıyor

  var newPlots = newPlots || { }; 
      $.each(ret, function(index, element) { 
        newPlots[element.name] = { 
         x : element.x, 
         y : element.y, 
         text : {content : element.name}, 
         tooltip : {content : element.name} 
        }; 
      }); 
O çalışmıyor
+0

Teşekkürler! Çok iyi çalışıyor ! – FabriceDouceur

İlgili konular