简体   繁体   中英

Datatables Complex header + sum api not working

Im having trouble in combining Complex Header + sum() of datatables.

JS

$('#mainTable').DataTable({
    "initComplete": function () {
        this.api().columns('.sum').every(function () {
            var column = this;

            var sum = column
               .data()
               .reduce(function (a, b) { 
                   a = parseInt(a, 10);
                   if(isNaN(a)){ a = 0; }

                   b = parseInt(b, 10);
                   if(isNaN(b)){ b = 0; }

                   return a + b;
               });

            $(column.footer()).html(sum);
        });
    }
});

Check this pen for full code:

http://codepen.io/JefMari/pen/mEYXya

The problem isn't the header, it is the strong-tag around your numbers. Strip the numbers for the calculation, with a regex for example.

var stripRegex = /<.*?>/g;

$('#mainTable').DataTable({
    "initComplete": function (settings, json) {
        this.api().columns('.sum').every(function () {
            var column = this;            
            var sum = column
               .data()
               .reduce(function (a, b) { 
                   a = parseInt(a.replace(stripRegex, ''), 10);
                   console.log(a);
                   if(isNaN(a)){ a = 0; }

                   b = parseInt(b.replace(stripRegex, ''), 10);
                   if(isNaN(b)){ b = 0; }

                   return a + b;
               });

            $(column.footer()).html(sum);
        });
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM