2016-08-23 3 views
1

を機能をループしていない、私は私の機能に私の機能で立ち往生している私のページのすべての値を置き換える期待しますelements.Iに私の目詰まり繰り返し同じ値を呼び出し、それぞれjQueryの私はjQueryプラグインを開発する新たなんだ正しく

(function ($) { 

    $.fn.siPrifixx = function (value, options) { 


     // This is the easiest way to have default options. 
     var settings = $.extend({ 
      // These are the defaults. 
      maxDigits: 8, 
      seperator: true, 
      decimal: 1, 
      popUp: true, 
      index: "tool tip message" 
     }, options); 

     console.log(settings.index); 

     $(this).addClass('tooltip', 'test'); 
     $(this).tooltipster({ 
      theme: 'tooltipster-default', 
      functionInit: function() { 
       return value 
      } 
     }) 

     // $('.tooltip').prop(settings.index, value); 

     var number = value; 

     if (typeof value === 'string') { 
      var parts = value.split(","); 
      number = (parseInt(parts.join(""))); 
     } 




      if (typeof number !== 'undefined' && !isNaN(number)) { 

       // if the number is alreadey comma seperated convert to number 
       var n = settings.decimal 
       // 2 decimal places => 100, 3 => 1000, etc 
       var decPlace = Math.pow(10, n); 

       // Enumerate number abbreviations 
       var abbrev = ["K", "M", "B", "T"]; 
       // Go through the array backwards, so we do the largest first 
       for (var i = abbrev.length - 1; i >= 0; i--) { 

        // Convert array index to "1000", "1000000", etc 
        var size = Math.pow(10, (i + 1) * 3); 

        // If the number is bigger or equal do the abbreviation 
        if (size <= number) { 
         // Here, we multiply by decPlaces, round, and then divide by decPlaces. 
         // This gives us nice rounding to a particular decimal place. 
         number = Math.round(number * decPlace/size)/decPlace; 

         // Handle special case where we round up to the next abbreviation 
         if ((number == 1000) && (i < abbrev.length - 1)) { 
          number = 1; 
          i++; 
         } 

         // Add the letter for the abbreviation 
         number += abbrev[i]; 

         // We are done... stop 

         break; 
        } 


       } 

       $(this).html(number) 
       console.log(number) 
       // return number; 
      } else { 
       $(this).html(number) 
       console.log(number) 
       // return value; 
      } 

    }; 

}(jQuery)); 

私はこのようなループで関数を呼び出しています。

$.each($(plugin.element).find('.widget-data'), function(index, value) 
{ 
    var index = $(this).data('index'); 
    var value = data.stats[index]; 
    $('.widget-data').siPrifixx(value,{ 
      decimal:2, 
      index:index 
    }); 

私のコードで何が間違っていますか?

+0

を 'data.stats'は何ですか? – vijayP

+0

それは私のajax応答データセットです –

答えて

1

$('.widget-data').siPrifixxを呼び出すと、まだwidget-dataクラスのすべての要素に対処しています。既にそのセットを反復しているので、すべての反復ですべての要素をターゲットにするべきではありません。代わりに$(this).siPrifixx(...);

+0

こんにちはDavid Hedlundさん、ありがとうございました –

1

次のコードで試用できます呼び出し:

$(plugin.element).find('.widget-data').each(function(index) 
{ 
    var index_current = $(this).data('index'); 
    var value = data.stats[index_current]; 
    $(this).siPrifixx(value,{ 
      decimal:2, 
      index:index_current 
    }); 
}); 
+0

私のデータに問題はありません、data.stats [index];私は$( '。widget-data')を変更します。siPrifixxを$(this).siPrifixxに変更します。 –

+0

あなたの貢献に感謝します! –

+0

ようこそ@nizar ... ':)'! – vijayP

関連する問題