首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Tabulator.js计算时间

如何使用Tabulator.js计算时间
EN

Stack Overflow用户
提问于 2019-02-08 18:47:07
回答 1查看 343关注 0票数 1

我目前正在使用Tabulator.js来获取为我的项目生成的表。在我的项目中,我从firebase接收信息,并将其加载到由TABULATOR.js ( http://tabulator.info )设计的表中。

我知道如何在我的表格中计算正常值。但我想计算一下时间。例如,在第1行我得到00:45 (45分钟),第2行我得到1:10,结果行应该给我1:55。

我对web codding并不是很熟悉,这就是为什么我需要你的帮助。

我的代码:

代码语言:javascript
复制
var table = new Tabulator("#editor", {
        downloadConfig:{
            columnGroups:false, 
            rowGroups:false,
        },
        height:360, 
        data: arrayFlight,
        pagination:"local",
        resizableColumns: false,
        paginationSize:12,
        columnVertAlign:"center",
        columnHoriAlign:"center",
        columns:[ //I show you only the requested column
            {
            title:"SINGLE PILOT FLIGHT TIME",
                columns:[
                {title:"SE", field:"seTime", align:"center", formatter:"datetime", formatterParams:{inputFormat:"hh:mm:ss", outputFormat:"hh:mm:ss", invalidPlaceholder:"-"}, sorter:false, headerSort:false, width:95, bottomCalc:"sum", bottomCalcParams:{precision:2}},
                {title:"ME", field:"meTime", align:"center", formatter:"datetime", formatterParams:{inputFormat:"hh:mm:ss", outputFormat:"hh:mm:ss", invalidPlaceholder:"-"}, sorter:false, headerSort:false, width:95},
                ],
            },  
        ],

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-13 16:07:04

为此,您需要使用自定义列计算,如下所示:

代码语言:javascript
复制
//define custom calculation function
var timeSum = function(values, data, calcParams){
    //values - array of column values
    //data - all table data
    //calcParams - params passed from the column definition object

    var time = 0;

    values.forEach(function(value){
        if(value){
            //split time value into minutes and sum
            var values = value.split(":");

            time += parseInt(values[0]) * 60;
            time += parseInt(values[1]);
        }
    });

    //format sum as hour:minute
    return Math.floor(time/60) + ":" + time%60;
};

然后,您可以在列定义中指定它:

代码语言:javascript
复制
{title:"SE", field:"seTime", bottomCalc:timeSum}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54590750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档