我正在实现一个 Bootgrid 表,使用 Ajax 从 Mysql 表中获取数据,一切正常,但现在我试图对最后一列求和并在最后一行或页脚上打印结果。有谁知道我应该调用哪种方法或者我怎样才能做到这一点?
问问题
803 次
1 回答
1
我有类似的经历,我能想到的最好的方法是使用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));
});
希望这可以帮助。
于 2017-12-30T19:38:50.933 回答