Bootgrid sum列并在页脚中显示结果

问题描述 投票:2回答:1

我正在实现一个Bootgrid表,使用Ajax从Mysql表中获取数据,一切正常,但现在我试图在最后一行或页脚上总结最后一列和打印结果。有谁知道我应该打电话给哪种方法或者我怎么做到这一点?

jquery mysql sum jquery-bootgrid
1个回答
1
投票

我有类似的经历,我能想到的最好的方法是使用loaded事件处理程序,然后手动进行计算,这里是一个例子,假设你有两个列qte和价格,你想得到总计(qte *价格):

var bootGrid = ('#grid');

bootGrid.bootgrid({
    ajax: true,
    url: 'json'
    ,multiSort:true
    // other options...
    ,labels: {
            infos: '<h3>Total: <b><span id="totalAmount"></span></b></h3><p>Showing {{ctx.start}} to {{ctx.end}} of {{ctx.total}} entries</p>',
    } //labels
}).on("loaded.rs.jquery.bootgrid", function (){
    // dynamically find columns positions
    var indexQte = -1;
    var indexPrice = -1;
    $(bootGrid).find('th').each(function(e){
        if ($(this).attr('data-column-id') == 'qte'){
            indexQte = e;
        } else if ($(this).attr('data-column-id') == 'price'){
            indexPrice = e;         
        }
    });
    var totalAmount = 0.0;
    $(bootGrid).find('tbody tr').each(function() {
        var qte = 0.0;
        var price = 0.0;
        // loop through rows
        $(this).find('td').each(function(i){
            if (i == indexQte){
                qte = parseFloat($(this).text());
            } else if (i == indexPrice){
                price = parseFloat($(this).text());
            }
        });
        totalAmount += qte * price;
    });
    $('#totalAmount').text(totalAmount.toFixed(2));

});

希望这可以帮助。

© www.soinside.com 2019 - 2024. All rights reserved.