Ich benutze dynamicform wbraganca, um Bestellvorgang durchzuführen. In dynamicform habe ich Artikel, Menge, Preis und Gesamtbetrag. Wenn jemand den Artikel oder die Menge bearbeitet, berechnet er die Summe und Summe der Summe für jeden Zeilenartikel. Was ich fragen möchte, wie man die Gesamtreihen in der dynamischen Form kennt, also kann ich Schleife, um die Summe zu summieren. hier ist mein CodeYii2 - Wie Schleife innerhalb von dynamicform wbraganca mit Javascript
<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>
und mein Javascript wie diese
<?php
$script = <<< JS
jQuery(".dynamicform_wrapper").on("afterInsert", function(e, item) {
jQuery(".dynamicform_wrapper .add-item").each(function(index) {
calculateTotal(index+1);
});
});
jQuery(".dynamicform_wrapper").on("afterDelete", function() {
jQuery(".dynamicform_wrapper .remove-item").each(function(index) {
calculateTotal(index+1);
});
});
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(""));
total = total + subtotal;
}
$('#purchaseorder-total').val(total);
}
JS;
$this->registerJs($script, $this::POS_END);
?>
das Problem, wenn Update zu tun, es ist alles nicht berechnen kann.