javascript - Calculate sum of values in jquery datatable footer - Stack Overflow

I want to display total amount in jquery datatable footer.Here is my datatable:Here's my jquery d

I want to display total amount in jquery datatable footer. Here is my datatable:

Here's my jquery datatable code:

for (var i = 0; i < length; i++ ) {
    var patient = data.data[i];
    console.log(patient);

    var formattedDate = function() {
        if (patient.Date === null) return "";
        var pattern = /Date\(([^)]+)\)/;
        var results = pattern.exec(patient.Date);
        var dt = new Date(parseFloat(results[1]));
        return (dt.getMonth() + 1 + "/" + dt.getDate() + "/" + dt.getFullYear());
    }

    $('#myReportTable').dataTable().fnAddData([
        patient.Name,
        patient.Address,
        //patient.Date,
        formattedDate,
        patient.Description,
        patient.Amount
    ]);
}

$('#myReportTable').DataTable({
footerCallback: function (tfoot, data, start, end, display) {
        var api = this.api();
            $(api.column(4).footer()).html(
            "Total: " + api.column(4).data().reduce(function (a, b) {
                return a + b;
            }, 0)
        );
    }
});

Tried this code, but it is showing an error:

I'm new to this, please help.

I want to display total amount in jquery datatable footer. Here is my datatable:

Here's my jquery datatable code:

for (var i = 0; i < length; i++ ) {
    var patient = data.data[i];
    console.log(patient);

    var formattedDate = function() {
        if (patient.Date === null) return "";
        var pattern = /Date\(([^)]+)\)/;
        var results = pattern.exec(patient.Date);
        var dt = new Date(parseFloat(results[1]));
        return (dt.getMonth() + 1 + "/" + dt.getDate() + "/" + dt.getFullYear());
    }

    $('#myReportTable').dataTable().fnAddData([
        patient.Name,
        patient.Address,
        //patient.Date,
        formattedDate,
        patient.Description,
        patient.Amount
    ]);
}

$('#myReportTable').DataTable({
footerCallback: function (tfoot, data, start, end, display) {
        var api = this.api();
            $(api.column(4).footer()).html(
            "Total: " + api.column(4).data().reduce(function (a, b) {
                return a + b;
            }, 0)
        );
    }
});

Tried this code, but it is showing an error:

I'm new to this, please help.

Share Improve this question edited Jan 11, 2019 at 7:32 Shreyas Pednekar asked Jan 11, 2019 at 7:11 Shreyas PednekarShreyas Pednekar 1,3058 gold badges33 silver badges54 bronze badges 3
  • Please provide example data. – O.O Commented Jan 11, 2019 at 7:27
  • Try var totalAmount = data.data.reduce((acc,cur) => acc + cur); – holydragon Commented Jan 11, 2019 at 7:27
  • @OlayinkaO my data is ing from database table – Shreyas Pednekar Commented Jan 11, 2019 at 7:31
Add a ment  | 

2 Answers 2

Reset to default 1

You can use datatables Sum Api

link - https://datatables/plug-ins/api/sum()

Also you could use the drawcallback function along with the sum api to calculate the some each time a record is added.

basically something like this;

$('#myReportTable').DataTable( {
    drawCallback: function () {
      var api = this.api();
      $( api.table().footer() ).html(
        api.column(3).data().sum()
      );
    }
  } );

3rd column indicates your Amount.

You are initializing DataTable() as two time in your code. Just bine that code so it will not throw an error as you mentioned (cannot reinitialize datatable...).

$(document).ready(function () {
        $('#example').DataTable({
            "footerCallback": function (row, data, start, end, display) {
                total = this.api()
                    .column(4)
                    .data()
                    .reduce(function (a, b) {
                        return parseInt(a) + parseInt(b);
                    }, 0);
                alert(total);
            }
        }).fnAddData([
                patient.Name,
                patient.Address,
                //patient.Date,
                formattedDate,
                patient.Description,
                patient.Amount
            ]);
    });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745546093a4632344.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信