jquery - Bootgrid 求和列并在页脚中显示结果

标签 jquery mysql sum jquery-bootgrid

我正在实现一个 Bootgrid 表,使用 Ajax 从 Mysql 表中获取数据,一切正常,但现在我正在尝试对最后一列求和并在最后一行或页脚打印结果。有谁知道我应该调用哪种方法或者我怎样才能做到这一点?

最佳答案

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

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));

});

希望这对您有所帮助。

关于jquery - Bootgrid 求和列并在页脚中显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43380631/

相关文章:

php - Laravel 身份验证问题

java - Spring Boot : java. sql.SQLException:用户访问被拒绝

sql-server - SUM() 比 SQL 中的 SELECT() 花费的时间更少。怎么运行的?

javascript - jQuery.dataTable() 如何隐藏行?

jquery - 如何使用 Moment JS 计算范围内给定工作日的数量?

javascript - jQuery 动画边距顶部

javascript - 仅在 IE10 中打开页面时自动弹出日期选择器

MySQL 统计所有超出限制的数据

c# - Collection<T>.Sum(a => a.ushortProperty) 到 ulong

sql - 在 postgresql 上使用 IF 语句选择