将 Jquery 数据表中的两列相乘并求和

multiply two columns in Jquery datatables and sum it

我有一个数据表,显示正常。我可以总结一个特定的列。我想将两列相乘并对乘法结果求和。下面是数据表 Jquery 代码。

我想将第 5 列和第 7 列相乘,然后对相乘求和。

    <script type="text/javascript">
    $(document).ready(function () {

        $("#btnGet").click(function () {

            $('.spinner').css('display', 'block');  //if clicked ok spinner shown
            $(function () {
                $.ajax({
                    type: "POST",
                    url: "/Reports/ItemsReports",


                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        $("#tblAllItems").DataTable(
                            {
                                dom: 'Blfrtip',

                                bLengthChange: true,
                                lengthMenu: [[10, 20, -1], [10, 20, "All"]],
                                bFilter: true,
                                bSort: true,
                                bPaginate: true,
                                buttons: [
                                    'csv',
                                    {
                                        extend: 'excel',
                                        messageTop: 'All Items Report',
                                        footer: true
                                    },
                                    {
                                        extend: 'pdf',
                                        messageTop: 'All Items Report ',
                                        footer: true
                                    },
                                    'print'],

                                data: response,
                                columns: [{ 'data': 'Item_ID' },
                                    { 'data': 'Item_category' },
                                    { 'data': 'Item_Name' },
                                    { 'data': 'Item_Description' },
                               {
                                    'data': 'Cost_Price', render: $.fn.dataTable.render.number(',', '.', 2,) // Format to return Currency with comma and 2d.p
                               },
                               {
                                   'data': 'Sales_Price', render: $.fn.dataTable.render.number(',', '.', 2,) // Format to return Currency with comma and 2d.p
                               },

                                { 'data': 'Quantity' },
                              
                                { 'data': 'Reorder_Level' },

                                { 'data': 'Barcode1' },
                                { 'data': 'Barcode2' },
                                { 'data': 'Barcode3' },
                                { 'data': 'Barcode4' },
                                { 'data': 'Item_Discontinued' },
                                    { 'data': 'Supplier' },                                    
                                    { 'data': 'Date_Added' },
                                    { 'data': 'Added_By' },
                                
                                ],

                                // Getting the Sum of the Account Balance
                                "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;
                                    };

                                    // Total over all pages
                                    total = api
                                        .column(5)
                                        .data()
                                        .reduce(function (a, b) {
                                            return a + b;
                                        }, 0);

                                    // Total over this page
                                    pageTotal = api
                                        .column(5, { page: 'current' })
                                        .data()
                                        .reduce(function (a, b) {
                                            return a + b;
                                        }, 0);

                                    // Update footer
                                    var numFormat = $.fn.dataTable.render.number(',', '.', 2,).display; // This is to format the output to 2d.p
                                    $(api.column(5).footer()).html(numFormat(pageTotal) + ' ( ' + numFormat(total) + ' OverAllTotal)'
                                    );
                                },

                                destroy: true // To allow for Table Reinitialization


                            });
                        $('.spinner').css('display', 'none');
                    },
                    failure: function (response) {
                        alert(response.d);
                        $('.spinner').css('display', 'none');
                    },
                    error: function (response) {
                        alert(response.d);
                        $('.spinner').css('display', 'none');
                    }
                });
            });

        });

    });

</script>

我想将第 5 列和第 7 列相乘,然后对相乘求和。

我找到了使用数组的方法。

// This is to be used if no need for the totalcostpricecolumn
var costPrice = api.column(4).data().toArray();
var qty = api.column(6).data().toArray();

var total = 0;

for (i = 0; i < costPrice.length; i++) {
    total += costPrice[i] * qty[i];
}
$(api.column(7).footer()).html(numFormat(total)); // Display the totalcostPrice