为calculateRowProductData函数新增功能:修改sub_total计算逻辑并更新字段
修改后的函数代码
function calculateRowProductData(quantity) { // 假设lens和cutting_charge是按rowindex存储的全局数组,若实际来源不同请自行调整 const lensPrice = lens[rowindex]; const cuttingCharge = cutting_charge[rowindex]; const rowTotalBase = (lensPrice + cuttingCharge); if(product_type[pos] == 'standard') unitConversion(); else row_product_price = product_price[rowindex]; const $currentRow = $('table.order-list tbody tr:nth-child(' + (rowindex + 1) + ')'); // 更新lens和cutting_charge的显示与输入字段 $currentRow.find('.lens').text(lensPrice.toFixed({{$general_setting->decimal}})); $currentRow.find('.lens-value').val(lensPrice.toFixed({{$general_setting->decimal}})); $currentRow.find('.cutting-charge').text(cuttingCharge.toFixed({{$general_setting->decimal}})); $currentRow.find('.cutting-charge-value').val(cuttingCharge.toFixed({{$general_setting->decimal}})); $currentRow.find('.discount').text((product_discount[rowindex] * quantity).toFixed({{$general_setting->decimal}})); $currentRow.find('.discount-value').val((product_discount[rowindex] * quantity).toFixed({{$general_setting->decimal}})); $currentRow.find('.tax-rate').val(tax_rate[rowindex].toFixed({{$general_setting->decimal}})); if (tax_method[rowindex] == 1) { // 基于新的rowTotalBase计算税和小计,若税的计算逻辑需调整请根据实际需求修改 const net_unit_price = rowTotalBase - product_discount[rowindex]; const tax = net_unit_price * quantity * (tax_rate[rowindex] / 100); const sub_total = rowTotalBase * quantity + tax; $currentRow.find('.net_unit_price').text(net_unit_price.toFixed({{$general_setting->decimal}})); $currentRow.find('.net_unit_price').val(net_unit_price.toFixed({{$general_setting->decimal}})); $currentRow.find('.tax').text(tax.toFixed({{$general_setting->decimal}})); $currentRow.find('.tax-value').val(tax.toFixed({{$general_setting->decimal}})); $currentRow.find('.sub-total').text(sub_total.toFixed({{$general_setting->decimal}})); $currentRow.find('.subtotal-value').val(sub_total.toFixed({{$general_setting->decimal}})); } else { const sub_total_unit = rowTotalBase - product_discount[rowindex]; const net_unit_price = (100 / (100 + tax_rate[rowindex])) * sub_total_unit; const tax = (sub_total_unit - net_unit_price) * quantity; const sub_total = rowTotalBase * quantity; $currentRow.find('.net_unit_price').text(net_unit_price.toFixed({{$general_setting->decimal}})); $currentRow.find('.net_unit_price').val(net_unit_price.toFixed({{$general_setting->decimal}})); $currentRow.find('.tax').text(tax.toFixed({{$general_setting->decimal}})); $currentRow.find('.tax-value').val(tax.toFixed({{$general_setting->decimal}})); $currentRow.find('.sub-total').text(sub_total.toFixed({{$general_setting->decimal}})); $currentRow.find('.subtotal-value').val(sub_total.toFixed({{$general_setting->decimal}})); } calculateTotal(); }
关键修改说明
- 新增字段更新: 加入了lens和cutting_charge的DOM同步逻辑,将对应值更新到页面的显示文本和输入框中(若实际DOM类名与示例不同,请替换为项目中实际使用的类名)
- 调整小计计算逻辑: 将
sub_total的计算方式替换为(lens + cutting_charge) * quantity,同时适配两种计税方式的场景 - 代码优化: 缓存当前行的jQuery对象,避免重复DOM查询,提升执行效率
- 语义化变量: 新增
rowTotalBase变量,让计算逻辑更直观易懂
内容的提问来源于stack exchange,提问作者Mohamed rifky
相关产品推荐
相关产品推荐

