How to calculate the total sum in HTML table

2019-09-13 07:14发布

I have updated the google sheet tables in Web Apps and in this web app with date picker and other filter option. My table consists of some numbers (Row-Chargeback ) I am trying to generate the grand total (Sum) in the top left of my web app.

This should show only the number of selected days

enter image description here

for detailed script refer the sheet sample Sheet

1条回答
三岁会撩人
2楼-- · 2019-09-13 07:49

You can use sum() api from datatable to get total of amount. You need to add below cdn.

<script type="text/javascript" language="javascript" src="//cdn.datatables.net/plug-ins/1.10.15/api/sum().js"></script>

Add HTML element before table tag, to display the sum amount.

<p id="sum"></p>

And then add the below callback function during the table datatable initialization.

drawCallback: function () {
              var api = this.api();
              $("#sum").html(
                "Total Amount: "+api.column( 4, {page:'current'} ).data().sum().toLocaleString()
              );
            }

Note: Since you are using multiple plug-ins, it is beneficial in terms of performance to combine the plug-ins into a single CDN Link wherever possible, rather than making multiple requests to the DataTables CDN.

查看更多
登录 后发表回答