【问题标题】:How to add a subtotal column in Datatables如何在数据表中添加小计列
【发布时间】:2021-11-09 04:10:46
【问题描述】:

My last question 与列的总和(垂直)有关,现在我需要对列的值求和(水平)

我尝试过的:

我尝试迭代所有列 table.columns().data() 并对值求和,但我不知道如何在最后一列中“注入”求和结果。

想要的结果:

小计是以下结果:3+4+5+6+7+8+9+10+11+12

LIVE JS BIN

【问题讨论】:

    标签: datatables


    【解决方案1】:

    这是带有旧代码的 JS 代码。你也可以从here看到它

    <script>
    $(document).ready(function() {
    
    
        var DT1 = $('#example').DataTable({
    
            columnDefs: [{
                orderable: false,
                className: 'select-checkbox',
                targets: 0,
            }],
            select: {
                style: 'os',
                selector: 'td:first-child'
            },
            order: [
                [1, 'asc']
            ],
            "language": {
                "info": "Showing _START_ to _END_ of _TOTAL_ Projects",
                "infoEmpty": "Showing 0 to 0 of 0 Projects",
                "infoFiltered": "(filtered from _MAX_ total Projects)"
            },
            "pagingType": "numbers",
            dom: 'rtip',
            initComplete: function(settings, json) {
                // calculate the sum when table is first created:
                doSum();
            }
        });
    
        $('#example').on('draw.dt', function() {
            // re-calculate the sum whenever the table is re-displayed:
            doSum();
        });
        $(".selectAll").on("click", function(e) {
            if ($(this).is(":checked")) {
                DT1.rows().select();
            } else {
                DT1.rows().deselect();
            }
        });
    
        // This provides the sum of all records:
        function doSum() {
            // get the DataTables API object:
            var table = $('#example').DataTable();
            // set up the initial (unsummed) data array for the footer row:
            var totals = ['', 'Total:', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
            // iterate all rows - use table.rows( {search: 'applied'} ).data()
            // if you want to sum only filtered (visible) rows:
            totals = table.rows().data()
                // sum the amounts:
                .reduce(function(sum, record) {
                    for (let i = 2; i <= 12; i++) {
                        sum[i] = sum[i] + numberFromString(record[i]);
                    }
                    return sum;
                }, totals);
            // place the sum in the relevant footer cell:
            for (let i = 1; i <= 12; i++) {
                var column = table.column(i);
                $(column.footer()).html(formatNumber(totals[i]));
            }
    
    
            $('#example > tbody  > tr').each(function(index, tr) {
                var rowSum = 0;
                for (let j = 0; j < 12; j++) {
                    if (j >= 2) {
                        var tdCellValue = numberFromString(tr.cells[j].innerText);
                        rowSum = rowSum + tdCellValue;
                    }
                    if (j >= 11) {
                        tr.cells[12].innerText = rowSum;
                    }
                }
            });
    
            $('#example > tfoot  > tr').each(function(index, tr) {
                var rowSum = 0;
                for (let j = 0; j < 12; j++) {
                    if (j >= 2) {
                        var tdCellValue = numberFromString(tr.cells[j].innerText);
                        rowSum = rowSum + tdCellValue;
                    }
                    if (j >= 11) {
                        tr.cells[12].innerText = rowSum;
                    }
                }
            });
        }
    
        function numberFromString(s) {
            return typeof s === 'string' ?
                s.replace(/[\$,]/g, '') * 1 :
                typeof s === 'number' ?
                s : 0;
        }
    
        function formatNumber(n) {
            return n.toLocaleString(); // or whatever you prefer here
        }
    });
    </script>
    

    【讨论】:

    • 谢谢解答,怎么多了一个0000000002
    • @Cheknov 你能再检查一次吗? row1 sum value,1004156 =(100444 +1002222+ 100466+ 100122+ 100122+ 100122+ 100122+ 10096+ 100222)= 1004156 row2 sum值,1000000 =(100000 +100000 +100000 +100000 +100000 +100000 +100000 + 100000 +100000 +100000 ) 第 3 行总和值,2004156 = (200013 + 200546 +200444 +200222 +200466 +200789 +200122 +200333 +200999 +200222 )
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 1970-01-01
    • 2015-11-06
    • 2015-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多