我目前正在尝试计算动态变化形式的总和。我目前拥有的是Symfony2表单集合,其中我将产品数量乘以unitcost = ProductCost。到目前为止,这很有用。现在我需要添加表单的所有ProductCost,并将结果图输入另一个字段(recipecost)。
我认为我需要的是一个Foreach循环,但不知道从哪里开始。
$(document).on('change', '.products, .amounts, .unit', function(event) {
var amount = $(this).parent().parent().find('.amounts').val();
var productId = $(this).parent().parent().find('.products').val();
var unit = $(this).parent().parent().find('.unit').val();
var productCostField = $(this).parent().parent().find('.product-costs');
var recipeCost = $(this).parent().parent().find('.recipecost');
console.log("Amount: " + amount + " - ProductID: " + productId + " - unit: " + unit);
if (amount == "" || productId == "" || unit == "") {
// Don't make the Ajax call if missing one of the values
return false;
}
// triggered every time an input field is changed
$.post(
Routing.generate('calculate_cost'),
{
amount: amount,
unit: unit,
product: productId
},
function(data) {
data = JSON.parse(data);
if (!data.success) {
// An error was thrown in the controller
alert(data.message);
}
else {
// Update the corresponding productCost field using the data from the controller
console.log("Product cost: " + data.productCost);
productCostField.val(data.productCost);
var arr = document.getElementsByClassName('product-costs');
var tot=0;
for(var i=0;i<arr.length;i++){
if(parseInt(arr[i].value))
tot += parseInt(arr[i].value);
}
recipecostField.value = tot;
}
}
);
});
public function getProductCostAction(Request $request) {
$amount = $request->request->get('amount', null);
$productId = $request->request->get('product', null);
$unit = $request->request->get('unit', null);
if (empty($amount) || empty($productId)) {
return new \Symfony\Component\HttpFoundation\Response(json_encode(array('success' => false, 'message' => 'Bad input')));
}
$em = $this->getDoctrine()->getManager();
$product = $em->getRepository('BCInventoryBundle:Product')->find($productId);
$u = $em->getRepository('BCInventoryBundle:Measures')->find($unit);
$mass = new Mass($amount, $u->getUnit());
$fam = $mass->toUnit('g');
if (empty($product)) {
return new \Symfony\Component\HttpFoundation\Response(json_encode(array('success' => false, 'message' => 'Invalid product')));
}
$productCost = $product->getCostunit() * $fam;
return new \Symfony\Component\HttpFoundation\Response(json_encode(array('success' => true, 'productCost' => $productCost)));
}
How I would like it to work Image http://i61.tinypic.com/2rx8jll.png
说实话我不知道从哪里开始,我花了最后几个小时试图弄清楚我是应该用PHP还是用Ajax做的,我目前有类似的功能运行& #39; s最后按下了提交按钮。看起来像这样:
public function fixRecipecost() {
$this->recipecost = 0;
foreach ($this->product AS $pc) {
$this->recipecost += $pc->getProductcost();
$this->setRecipecost($this->recipecost);
}
}
任何帮助非常感谢。我需要明确的是,ProductCost字段的数量可以是1或3000,因此只需手动指定每个字段就不是一个选项。
我刚刚尝试将其添加到我的JS结尾:
var arr = document.getElementsByClassName('product-costs');
var tot=0;
for(var i=0;i<arr.length;i++){
if(parseInt(arr[i].value))
tot += parseInt(arr[i].value);
}
recipecostField.value = tot;
}
}
但是没有效果。尽管如此,我的日志中没有错误。
答案 0 :(得分:1)
我同意user26409021,你可以在每次AJAX调用返回时计算总和。这样的事情应该有效(假设您的&#34;食谱成本&#34;字段的ID设置为&#34;食谱成本&#34;):
$(document).on('change', '.products, .amounts, .unit', function(event) {
updateProductCost(this);
});
function updateProductCost(field) {
var amount = $(field).parent().parent().find('.amounts').val();
var productId = $(field).parent().parent().find('.products').val();
var unit = $(field).parent().parent().find('.unit').val();
var productCostField = $(field).parent().parent().find('.product-costs');
var recipeCost = $(field).parent().parent().find('.recipecost');
console.log("Amount: " + amount + " - ProductID: " + productId + " - unit: " + unit);
if (amount == "" || productId == "" || unit == "") {
// Don't make the Ajax call if missing one of the values
return false;
}
// triggered every time an input field is changed
$.post(
Routing.generate('calculate_cost'),
{
amount: amount,
unit: unit,
product: productId
},
function(data) {
data = JSON.parse(data);
if (!data.success) {
// An error was thrown in the controller
alert(data.message);
}
else {
// Update the corresponding productCost field using the data from the controller
console.log("Product cost: " + data.productCost);
productCostField.val(data.productCost);
calculateRecipeCost();
}
});
}
function calculateRecipeCost() {
var totalCost = 0;
$('.product-costs').each(function(index, value) {
if ($.isNumeric($(this).val())) {
totalCost = totalCost + parseFloat($(this).val());
}
});
$('#recipe-cost').val(totalCost);
}