【问题标题】:jQuery Datatables Footer sum of a hh:mm:ss data type columnjQuery Datatables 页脚总和 hh:mm:ss 数据类型列
【发布时间】:2016-09-21 18:48:02
【问题描述】:

我可以像这样得到一个 INT 列的页脚总数:

footerCallback: function ( row, data, start, end, display ) {
        var quantita = this.api(), data;

        // Total over all pages
        total_quantita = quantita
            .column( 5 )
            .data()
            .reduce( function (a, b) {
                return intVal(a) + intVal(b);
            }, 0 );

        // Total over this page
        pageTotal_quantita = quantita
            .column( 5, { page: 'current'} )
            .data()
            .reduce( function (a, b) {
                return intVal(a) + intVal(b);
            }, 0 );

        // Update footer Column "quantita"
        $( quantita.column( 5 ).footer() ).html(
        pageTotal_quantita +' ('+ total_quantita +' total)'
        );

我的问题是:我怎样才能像上面那样获得 hh:mm:ss 数据类型列的总数? 谢谢!

【问题讨论】:

  • 以同样的方式,也许使用MomentJS 的持续时间

标签: javascript jquery datatables


【解决方案1】:

我已经更新了你的代码,你就快到了,但我们用了片刻来创建这个:

var table = $('#example').DataTable({
    "footerCallback": function ( row, data, start, end, display ) {
        var api = this.api(), data;
        // Total over all pages
        total_ID = api.column(0).data().reduce( function (a, b) {
            return ~~a + ~~b;
        }, 0 );
        total_Duration = api.column(1).data().reduce( function (a, b) {
            return moment.duration(a).asMilliseconds() + moment.duration(b).asMilliseconds();
        }, 0 );
        // Total over this page
        pageTotal_ID = api.column(0, { page: 'current'} ).data().reduce( function (a, b) {
            return ~~a + ~~b;
        }, 0 );
        pageTotal_Duration = api.column(1, { page: 'current'} ).data().reduce( function (a, b) {
            return moment.duration(a).asMilliseconds() + moment.duration(b).asMilliseconds();
        }, 0 );
        // Update footer Column "quantita"
        $( api.column(0).footer()).html(
            pageTotal_ID + ' ('+ total_ID +' total)'
        );
        $( api.column(1).footer()).html(
            moment.utc(pageTotal_Duration).format("HH:mm:ss") + ' ('+ moment.utc(total_Duration).format("HH:mm:ss") + ' total)'
        );
    }   
});

它似乎适用于有限的例子我created。希望对您有所帮助。

【讨论】:

  • 不客气,请注意format 函数可能需要长时间调整,因为我spoofing 时刻认为这是一个约会。环顾四周,我相信您会找到另一种正确格式化持续时间的方法。
  • 确实,时间有问题。如果小时 > 24 则小时 = 1。大问题:(
【解决方案2】:

试图找到解决方案...我要做的第一件事是 sum() 列数据 (https://cdn.datatables.net/plug-ins/1.10.11/api/sum%28%29.js)

        var time_api = this.api();
        //total for all pages
        var total = time_api.column(3)
                .data()
                .sum();           

        //write in footer
        $(time_api.column(3)
            .footer())
            .html(total);

这给了我一个总和的输出,但它需要一些调整,而且我不知道如何转换它

变成这个:12:03:05(正确的和输出)。

我可能不得不为此创建一个新问题,如果得到答案,我会在这里更新。

请帮忙。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-22
    • 1970-01-01
    • 2017-06-18
    • 2018-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多