[英]How to efficiently use jQuery to multiply columns of dynamically rendered table
I'm working on a pretty big table/form that is dynamically created depending on stock availability.我正在处理一个非常大的表格/表格,该表格/表格是根据库存情况动态创建的。 The table has 5 fields: Code, Prod.Name, Amount, Price and SubTotal.
该表有 5 个字段:Code、Prod.Name、Amount、Price 和 SubTotal。 The user is supposed to set a number in the Amount field and then there's a jQuery script that multiplies that amount for the price to display the SubTotal.
用户应该在金额字段中设置一个数字,然后有一个 jQuery 脚本将该金额乘以价格以显示小计。 In the table, a product looks like this:
在表中,产品如下所示:
<tr>
<td>Princess 01</td>
<td>Conjunto corpiño y cola less <strong>Talle 85</strong></td>
<td><input type="text" id="princess-lingerie-id-963" name="[princess-lingerie][id_963]" value=""></td>
<td>$<input type="text" id="price_princess-lingerie-id-963" value="99" readonly="readonly"/></td>
<td id="subTotal_princess-lingerie-id-963" name="subTotal"></td>
</tr>
My problem is that I have many different products that belong to different product categories, so while this belongs to the "princess-lingerie" (don't laugh) category, a product from the "cosmetica" category looks like this:我的问题是我有许多不同的产品属于不同的产品类别,所以虽然这属于“公主内衣”(不要笑)类别,但“化妆品”类别的产品看起来像这样:
<tr>
<td>D02/3</td>
<td>Magic Dual aroma Frutos Rojos</td>
<td><input type="text" id="cosmetica-stock-id-1008" name="[cosmetica-stock][id_1008]" value=""></td>
<td>$<input type="text" id="price_cosmetica-stock-id-1008" value="26" readonly="readonly"/></td>
I have actually already created the little bit of logic which would work for only one field, but I don't know how to extend it to the whole table.实际上,我已经创建了一些仅适用于一个字段的逻辑,但我不知道如何将其扩展到整个表。 Could anyone give me a hand with it?
谁能帮我一把? ...I'm a little bit overwhelmed and don't know where to start right now.
...我有点不知所措,现在不知道从哪里开始。
$(document).ready(function(){
$("#princess-lingerie-id-963").keyup(function() {
var howMany = parseInt($("#princess-lingerie-id-963").val());
var subTotal = parseInt($("#price_princess-lingerie-id-963").val()) * howMany;
//assign subTotal to the td
$("#subTotal_princess-lingerie-id-963").html(subTotal);
});
});
Shad is right to suggest adding useful classes. Shad 建议添加有用的类是正确的。 To handle the calculations in a sustainable way across a multi-row table, you'll also need a way to generically identify which row you're calculating on.
要在多行表中以可持续的方式处理计算,您还需要一种方法来通用地识别您正在计算的行。 I'm sure this could be greatly improved, but here's a basic example:
我相信这可以大大改善,但这里有一个基本的例子:
The jsfiddle demo is here . jsfiddle 演示在这里。
// there are two cells where the 'id' has this pattern, but only one is selectable (the other is read-only)
// still, it would be better to give it a class if at all possible to avoid confusion
// and possible tampering by users
$("input[id*=-id-]").keyup(function() {
var howMany = parseInt($(this).parent().next().find('input').val(), 10);
var subTotal = parseInt($(this).val(), 10) * howMany;
//assign subTotal to the td
$(this).parent().next().next().html(subTotal);
});
EDIT:编辑:
Updated to include radix and condition for blank value:更新为包括空白值的基数和条件:
http://jsfiddle.net/RzBeM/2/ http://jsfiddle.net/RzBeM/2/
$("input[id*=-id-]").keyup(function() {
var howMany = parseInt($(this).parent().next().find('input').val(), 10);
var subTotal = parseInt($(this).val(), 10) * howMany;
if (isNaN(subTotal)) subTotal = 0;
//assign subTotal to the td
$(this).parent().next().next().html(subTotal);
});
First, I would start giving your inputs useful classes, like 'qty' and 'price', and then the final cell can have a class of 'subtotal'.首先,我将开始为您的输入提供有用的类,例如“数量”和“价格”,然后最后的单元格可以有一个“小计”的 class。 That makes the next step possible:
这使得下一步成为可能:
jQuery('#TABLEID tr').each(function(i,E){
var subT=parseInt(jQuery(E).find('.qty').val(),10) * parseFloat(jQuery(E).find('.price').val());
jQuery(E).find('.subtotal').text('$' + subT.toFixed(2));
});
I use jQuery to iterate over each tr
in the table, and run the necessary calculations.我使用 jQuery 遍历表中的每个
tr
,并运行必要的计算。
The final solution could use a further tweaked version:最终解决方案可以使用进一步调整的版本:
jQuery(function(){
jQuery('.qty').keyup(function(){
var E=jQuery(this).parents('tr').filter(':first');
var subT=parseInt(E.find('.qty').val(),10) * parseFloat(E.find('.price').val());
E.find('.subtotal').text('$' + subT.toFixed(2));
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.