【问题标题】:How to display sub-total in every division如何在每个部门中显示小计
【发布时间】:2016-03-28 01:00:47
【问题描述】:

我有一个计算总计和小计的函数。我的问题是我想显示每个部门的小计,但它会是相同的输出。我希望标题 1 的子总数为 37.00,标题 2 为 99.00。这是屏幕截图。

这是我的代码。

$('.pricing_level').on('change', function(e){
    var pricing_level_id = e.target.value;
    var customer_id = $('.customer ').val();

    $('tbody.subcontent tr').each(function(){
        var item_id =  $(this).closest('tr').find(".details_itemid").val();
        var category_id = $(this).closest('tr').find(".details_category_id").val();
        var subcategory_id = $(this).closest('tr').find(".details_subcategory_id").val();
        var that = this;

        $.get('/dev/api/itempricinglevel?item_id=' + item_id + '&pricinglevel_id=' + pricing_level_id, function(itempricinglevel){
            var std = parseFloat(itempricinglevel.std);
            var min = parseFloat(itempricinglevel.min);
            var max = parseFloat(itempricinglevel.max);

            $(that).closest('tr').find('.details_unitprice').val(std);

            //Here's the code to display the sub-total in every division
            var d_qty = $(that).closest('tr').find(".details_qty").val();
            var d_netprice  = $(that).closest('tr').find(".details_netunitprice").val();
            var total = d_qty * d_netprice;
            var totalAmt = total.toFixed(2);

            $(that).closest('tr').find(".details_total").val(totalAmt);
            grandtotal();
            var thisloc = $(that).parent().parent().parent().parent().find('.total');
            var inloc = $(that).parent().parent().parent().parent().find('.stotal');
            var eachloc =  $(that).parent().parent().parent().find('.details_total');
            subtotal(thisloc, eachloc, inloc);
            //------------------------------------------------------------ 
        })

    })
})

这是我的小计函数

function subtotal(thisloc, eachloc, inloc){ 
    var headertotal = 0;
    eachloc.each(function(){ 
        var price = $(this).val();
        headertotal += parseFloat(price);
        $(thisloc).text((headertotal).toFixed(2));
        $(inloc).val((headertotal).toFixed(2));
    });
}

这是小计的 HTML 代码。

$('tbody.content').prepend('<div class="groupborder" style="border-color:#333333;"><tr><td> \
            <table class="tables_details" style="width:100%;">\
              <th id="groups" data-id="'+headcount+'" style="background-color: #333333; width: 1250px;" > \
                <label style="display:inline-block;float:left; margin-left:900px; width:60px; font-weight: bold; color:#FFFFFF;">Sub-total: </label> \
                <label class="head" style="display: block; margin-left: -930px; float: left; font-weight: bold; color:#FFFFFF;">HEADER '+headcount+'</label> \
                <input type="text" name="header_name[]" class="edit-input" style="height: 20px; display: none; margin-left: -930px; float: left; padding-left:5px;" value="HEADER '+headcount+'"/> \
                <input name="header_id[]" type="hidden" value="'+headcount+'"> \
                <a href="" class="rm-header fa fa-times-circle fa-lg" style="float:right; margin-right:5px; margin-top:5px"></a> \
                <label style="display:inline-block; float:right; margin-right:18px; font-weight: bold; color:#FFFFFF;" class="total">0.00</label> \
                <input name="header_subtotal[]" class="stotal" type="hidden" value="0.00"> </th> \
              <tbody class="subcontent tbody"></tbody>\
            </table>\
         </td>\
       </tr></div>');

【问题讨论】:

  • 可以分享subtotal的功能和观点吗
  • 函数小计(thisloc, eachloc, inloc){ var headertotal = 0; eachloc.each(function(){ var price = $(this).val(); headertotal += parseFloat(price); $(thisloc).text((headertotal).toFixed(2)); $(inloc)。 val((headertotal).toFixed(2)); }); }
  • 你能用 HTML 更新你的问题吗?
  • 我认为每次调用 subtotal 时你都会覆盖它们。
  • 为每个小计分配一个唯一的ID,然后为每个tr定义一个与小计ID匹配的数据属性,并在您的foreach中为tr获取该数据属性并将其作为ID传递到您的小计函数。

标签: jquery


【解决方案1】:

api里面是。

var dataid = $(that).closest('tr').find('.details_total').data('total_id');
subtotal2(dataid);

而且你必须制作另一个小计函数。

function subtotal2(dataid){ 
    var headertotal = 0;
    $('.details_total_'+dataid+'').each(function(){ 
        var price = $(this).val();
        headertotal += parseFloat(price);
        $('#total_'+dataid+'').text((headertotal).toFixed(2));
        $('#total_'+dataid+'').val((headertotal).toFixed(2));

    });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多