我需要计算 DataTable 页脚中的时间,但我所拥有的只是 this
"footerCallback": function(row, data, start, end, display) {
var api = this.api(),
data;
var intVal = function(i) {
return typeof i === 'string' ? i.replace(/[\$,]/g, '') * 1 : typeof i === 'number' ? i : 0;
};
totalhrs = api.column(3).data().reduce(function(a, b) {
return intVal(a) + intVal(b);
}, 0);
totalhrs_page = api.column(3, {
page: 'current'
}).data().reduce(function(a, b) {
return intVal(a) + intVal(b);
}, 0);
// Update footer
$('#totalHours').html("<b>" + "TOTAL Hours: " + totalhrs_page + "/" + totalhrs + "</b>");
}
正如您在演示中看到的,我得到了“花费时间”列中所有行的总和,我需要的是计算时间。当小数位变为 >= 60 时,它应该将整数+1。假设结果是 23.82,那么应该是 24.22。现在小数点到一百时只能+1。
有更简单的方法吗?
谢谢!
最佳答案
您可以在reduce函数中将其转换为分钟
totalhrs = api.column(3).data().reduce(function(a, b) {
b = b.split(".");
var hours = b[0]||0;
var min = b[1]||0;
return a + intVal(hours*60)+intVal(min);
}, 0);
totalhrs = Math.floor(totalhrs / 60)+"." + totalhrs % 60;
关于javascript - 如何通过 DataTable 页脚计算时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42063910/