jQuery Datatables - footerCallback sum列,表总数的问题

时间:2018-02-14 14:33:58

标签: datatables

我已经创建了一个函数,我可以调用我希望总结的列,使用下面的内容。但是第9列的总(表格总数)条目始终为零。页面总数似乎有效。第9列的页面总数和表格总数也有效。

"footerCallback": function ( row, data, start, end, display ) {
            var api = this.api(), data;
            // Remove the formatting to get integer data for summation
            var intVal = function ( i ) {
                return typeof i === 'string' ?
                    i.replace(/[\£,]/g, '')*1 :
                    typeof i === 'number' ?
                        i : 0;
            };
            var column_sum = function (col) {
                // Total over all pages
                total = api
                    .column(col)
                    .data()
                    .reduce( function (a, b) {
                        return intVal(a) + intVal(b);
                    }, 0 );
                // Total over this page
                pageTotal = api
                    .column(col, { page: 'current'} )
                    .data()
                    .reduce( function (a, b) {
                        return intVal(a) + intVal(b);
                    }, 0 );
                return accounting.formatMoney(pageTotal) +' ('+ accounting.formatMoney(total) +' total)'
            };
            // Update footer
            $( api.column(6).footer()).html(
                column_sum(6)
            );
            $( api.column(9).footer()).html(
                column_sum(9)
            );
        }

修改

我添加了一些清理过的数据。目前页面总数适用于第一列和第二列。

所有页面总数不起作用。即每当我按列标题过滤时,我应该看到每页的总数

<table width="100%" class="table table-striped table-bordered table-hover dataTable no-footer dtr-inline" id="circuit_list"
 role="grid" style="width: 100%;">
    <thead>
        <tr>
            <th>Info</th>
            <th>Type</th>
            <th>Cost PM</th>
            <th>Term</th>
            <th>Remaining Term</th>
            <th>Remaining Cost</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                <a href="/circuits/edit/238/1/all_cl" class="btn btn-primary btn-circle">
                    <i class="fa fa-list"></i>
                </a>
            </td>
            <td>Fibre</td>
            <td>£950.00</td>
            <td>12</td>
            <td>0</td>
            <td>£0.00</td>
        </tr>
        <tr>
            <td>
                <a href="/circuits/edit/238/2/all_cl" class="btn btn-primary btn-circle">
                    <i class="fa fa-list"></i>
                </a>
            </td>
            <td>Fibre</td>
            <td>£950.00</td>
            <td>12</td>
            <td>0</td>
            <td>£0.00</td>
        </tr>
        <tr>
            <td>
                <a href="/circuits/edit/333/101/all_cl" class="btn btn-primary btn-circle">
                    <i class="fa fa-list"></i>
                </a>
            </td>
            <td>MPLS</td>
            <td>£1791.33</td>
            <td>12</td>
            <td>11</td>
            <td>£19,704.63</td>
        </tr>
        <tr>
            <td>
                <a href="/circuits/edit/334/101/all_cl" class="btn btn-primary btn-circle">
                    <i class="fa fa-list"></i>
                </a>
            </td>
            <td>MPLS</td>
            <td>£100.00</td>
            <td>12</td>
            <td>11</td>
            <td>£1,100.00</td>
        </tr>
        <tr>
            <td>
                <a href="/circuits/edit/235/1/all_cl" class="btn btn-primary btn-circle">
                    <i class="fa fa-list"></i>
                </a>
            </td>
            <td>MPLS</td>
            <td>£593.33</td>
            <td>36</td>
            <td>15</td>
            <td>£8,899.95</td>
        </tr>
        <tr>
            <td>
                <a href="/circuits/edit/317/82/all_cl" class="btn btn-primary btn-circle">
                    <i class="fa fa-list"></i>
                </a>
            </td>
            <td>Fibre</td>
            <td>£103.00</td>
            <td>3</td>
            <td>0</td>
            <td>£0.00</td>
        </tr>
        <tr>
            <td>
                <a href="/circuits/edit/229/2/all_cl" class="btn btn-primary btn-circle">
                    <i class="fa fa-list"></i>
                </a>
            </td>
            <td>MPLS</td>
            <td>£373.33</td>
            <td>36</td>
            <td>11</td>
            <td>£4,106.63</td>
        </tr>
        <tr>
            <td>
                <a href="/circuits/edit/233/1/all_cl" class="btn btn-primary btn-circle">
                    <i class="fa fa-list"></i>
                </a>
            </td>
            <td>DSL</td>
            <td>£1837.66</td>
            <td>60</td>
            <td>6</td>
            <td>£11,025.96</td>
        </tr>
        <tr>
            <td>
                <a href="/circuits/edit/234/1/all_cl" class="btn btn-primary btn-circle">
                    <i class="fa fa-list"></i>
                </a>
            </td>
            <td>DSL</td>
            <td>£373.34</td>
            <td>36</td>
            <td>15</td>
            <td>£5,600.10</td>
        </tr>
        <tr>
            <td>
                <a href="/circuits/edit/243/5/all_cl" class="btn btn-primary btn-circle">
                    <i class="fa fa-list"></i>
                </a>
            </td>
            <td>DSL</td>
            <td>£373.34</td>
            <td>36</td>
            <td>15</td>
            <td>£5,600.10</td>
        </tr>
        <tr>
            <td>
                <a href="/circuits/edit/244/4/all_cl" class="btn btn-primary btn-circle">
                    <i class="fa fa-list"></i>
                </a>
            </td>
            <td>MPLS</td>
            <td>£373.34</td>
            <td>36</td>
            <td>12</td>
            <td>£4,480.08</td>
        </tr>
        <tr>
            <td>
                <a href="/circuits/edit/324/83/all_cl" class="btn btn-primary btn-circle">
                    <i class="fa fa-list"></i>
                </a>
            </td>
            <td>4G</td>
            <td>£103.00</td>
            <td>3</td>
            <td>0</td>
            <td>£0.00</td>
        </tr>
        <tr>
            <td>
                <a href="/circuits/edit/2/6/all_cl" class="btn btn-primary btn-circle">
                    <i class="fa fa-list"></i>
                </a>
            </td>
            <td>4G</td>
            <td>£41.50</td>
            <td>12</td>
            <td>0</td>
            <td>0.00</td>
        </tr>
        <tr>
            <td>
                <a href="/circuits/edit/57/18/all_cl" class="btn btn-primary btn-circle">
                    <i class="fa fa-list"></i>
                </a>
            </td>
            <td>4G</td>
            <td>£45.00</td>
            <td>12</td>
            <td>0</td>
            <td>£0.00</td>
        </tr>
        <tr>
            <td>
                <a href="/circuits/edit/113/35/all_cl" class="btn btn-primary btn-circle">
                    <i class="fa fa-list"></i>
                </a>
            </td>
            <td>Fibre</td>
            <td>£45.00</td>
            <td>12</td>
            <td>0</td>
            <td>£0.00</td>
        </tr>
        <tr>
            <td>
                <a href="/circuits/edit/218/71/all_cl" class="btn btn-primary btn-circle">
                    <i class="fa fa-list"></i>
                </a>
            </td>
            <td>4G</td>
            <td>£57.00</td>
            <td>12</td>
            <td>0</td>
            <td>£0.00</td>
        </tr>
        <tr>
            <td>
                <a href="/circuits/edit/264/71/all_cl" class="btn btn-primary btn-circle">
                    <i class="fa fa-list"></i>
                </a>
            </td>
            <td>MPLS</td>
            <td>£45.00</td>
            <td>12</td>
            <td>0</td>
            <td>£0.00</td>
        </tr>
        <tr>
            <td>
                <a href="/circuits/edit/269/61/all_cl" class="btn btn-primary btn-circle">
                    <i class="fa fa-list"></i>
                </a>
            </td>
            <td>DSL</td>
            <td>£45.00</td>
            <td>12</td>
            <td>0</td>
            <td>£0.00</td>
        </tr>
        <tr>
            <td>
                <a href="/circuits/edit/300/85/all_cl" class="btn btn-primary btn-circle">
                    <i class="fa fa-list"></i>
                </a>
            </td>
            <td>4G</td>
            <td>£30.00</td>
            <td>12</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>
                <a href="/circuits/edit/307/76/all_cl" class="btn btn-primary btn-circle">
                    <i class="fa fa-list"></i>
                </a>
            </td>
            <td>4G</td>
            <td>£45.00</td>
            <td>12</td>
            <td>6</td>
            <td>270.00</td>
        </tr>
    </tbody>
</table>

1 个答案:

答案 0 :(得分:3)

最有可能在第9列的一个或多个单元格中,其值无法转换为数字,并且您获得 intVal 函数返回的 NaN

intVal 有两个问题:

  1. i.replace(/[\£,]/g, '')*1可以返回 NaN
  2. 对于 NaN
  3. typeof i === 'number'可以为真

    从看来,intVal正试图检查这一点,但逻辑是错误的。 将 intVal 更改为:

    var intVal = function ( i ) {
    
       if(typeof i === 'string') {
             i = i.replace(/[\£,]/g, '')*1;
       }
       // check if you got a valid number.
       if (Number.isNaN(i)) {
             return 0;
       }
       return i;
    };
    

    如果需要过滤总计,请使用:

    total = api.column(col, {"filter": "applied"}) 
    

    total = api.column(col, {"search": "applied"})