2017-04-04 5 views
0

私はyii2の動的フォームwbragancaを使用していますが、動的フォームではkartik/select2を使用しています。ここに私のビューコードです:yii2 dropDownListすべてのインデックスから常に値が1つ遅いとき

<div class="col-sm-8 col-md-3">      
    <?= $form->field($detail, "[{$i}]item_id")->widget(Select2::className(), [ 
     'data' => ArrayHelper::map(Item::find()->all(), 'id', 'name'), 
     'language' => 'en', 
     'options' => ['placeholder' => 'Select a item ...', 'onchange' => 'getItemPrice($(this))'], 
     'pluginOptions' => [ 
      'allowClear' => true,        
     ], 
    ]); 
?> 
</div> 
<div class="col-sm-4 col-md-2"> 
    <?= $form->field($detail, "[{$i}]qty")->widget(MaskedInput::className(), 
    [ 
     'clientOptions' => [ 
      'alias' => 'numeric', 
      'groupSeparator' => ',', 
      'digits' => 0, 
      'autoGroup' => true, 
      'removeMaskOnSubmit' => true, 
      'rightAlign' => false,         
     ], 
     'options' => [ 
      'class' => 'form-control', 
      'onchange' => 'calculateSubtotal($(this))',      
     ]        
    ]) ?> 
    </div> 
    <div class="col-sm-4 col-md-2"> 
     <?= $form->field($detail, "[{$i}]price")->widget(MaskedInput::className(), 
     [ 
      'clientOptions' => [ 
       'alias' => 'numeric', 
       'groupSeparator' => ',', 
       'digits' => 0, 
       'autoGroup' => true, 
       'removeMaskOnSubmit' => true, 
       'rightAlign' => false,        
      ], 
      'options' => [ 
       'class' => 'form-control', 
       'onchange' => 'calculateSubtotal($(this))',         
      ] 
    ]) ?> 
    </div> 
     <div class="col-sm-4 col-md-2"> 
      <?= $form->field($detail, "[{$i}]total")->widget(MaskedInput::className(), 
       [ 
        'clientOptions' => [ 
         'alias' => 'numeric', 
         'groupSeparator' => ',', 
         'digits' => 0, 
         'autoGroup' => true, 
         'removeMaskOnSubmit' => true, 
         'rightAlign' => false, 
        ] 
       ]) ?> 
    </div> 

、これは、私は最初のアイテム(価格150,000数量1)、関数計算の合計は常に結果0を与える選択したときに

function getItemPrice(item){ 
    var index = item.attr("id").replace(/[^0-9.]/g, ""); 
    var item_id = $('#purchaseorderdetail-'+ index + "-item_id").val(); 
    $.get('../item/get-price', {id : item_id}, function(data){ 
     $('#purchaseorderdetail-' + index + '-price').val(data); 
     $('#purchaseorderdetail-' + index + '-qty').val(1); 
     $('#purchaseorderdetail-' + index + '-total').val(data); 
    }); 
    calculateTotal(Number(index)+1); 
} 

function calculateSubtotal(item){ 
    var index = item.attr("id").replace(/[^0-9.]/g, ""); 
    var qty = $('#purchaseorderdetail-' + index + '-qty').val(); 
    qty = qty == "" ? 0 : Number(qty.split(",").join("")); 
    var price = $('#purchaseorderdetail-' + index + '-price').val(); 
    price = price == "" ? 0 : Number(price.split(",").join("")); 
    $('#purchaseorderdetail-' + index + '-total').val(qty * price); 

    calculateTotal(Number(index)+1); 
} 

function calculateTotal(index){  
    var total = 0; 
    for(i=0; i< index; i++){ 
     var subtotal = $('#purchaseorderdetail-' + i + '-total').val();   
     subtotal = subtotal == "" ? 0 : Number(subtotal.split(",").join("")); 
     alert(subtotal); 
     total = total + subtotal; 
    } 
    $('#purchaseorder-total').val(total); 
} 

私のjavascriptのコードで、私は2番目の項目(価格370,000と数量1)を追加すると、結果合計= 150,000となります。 2番目のアイテムを価格55,000の別のアイテムに変更すると、合計520,000の結果が得られます。

何が恋しいですか?ご意見をお聞かせください。 ありがとう

答えて

1

calculateTotal(Number(index)+1);のコードcalculateTotal(Number(index)+1);を中括弧$.getに移動してこのアンカーを見つけました。 今度は2番目のアイテムを削除するときは、calculateTotalをやり直す方法は?

0

すでに必要なものが見つかりました。 再計算するためにこれらのコードを追加するだけです

jQuery(".dynamicform_wrapper").on("afterDelete", function(e) { 
    jQuery(".dynamicform_wrapper .remove-item").each(function(i) {     
     calculateTotal(i+1); 
    }); 
}); 
関連する問題