如何计算数据表最终总金额

时间:2018-12-07 14:09:15

标签: php jquery

我正在为我的daliy销售编写报告。当我尝试计算最终总输出显示为undefined时 我使用未定义的警报消息输出显示检查了总数 我不知道那是什么错误。我到目前为止尝试过的内容附在下面

var total = 0;
function get_all() {
    var from_date = $('#from_date').val();
    var to_date   = $('#to_date').val();

    $('#tbl-projects').dataTable().fnDestroy();

    $.ajax({
        url      : "../php/report/all_sales.php",
        type     : "POST",
        dataType : 'JSON',
        data     : { from_date : from_date, to_date : to_date },
        async    : false,
        success  : function (data) {         
            $('#tbl-projects').dataTable({
                dom         : 'Bfrtip',
                buttons     : [
                    'excel', 'pdf', 'print'
                ],
                "aaData"    : data,
                "scrollX"   : true,
                "aoColumns" : [
                    {"sTitle": "Invoice No", "mData": "id"},
                    {"sTitle": "Date",       "mData": "date"},
                    {"sTitle": "Total",      "mData": "total"},
                    {"sTitle": "Pay",        "mData": "pay"},
                    {"sTitle": "Due",        "mData": "due"},
                ]
            });

            total += Number(total);
            alert(total);
        },
        error    : function (xhr) {
            console.log('Request Status: ' + xhr.status  );
            console.log('Status Text: ' + xhr.statusText );
            console.log(xhr.responseText);
            var text = $($.parseHTML(xhr.responseText)).filter('.trace-message').text();
            console.log(text)
        }
    });
}

1 个答案:

答案 0 :(得分:1)

Total变量是在函数外部定义的,而不是在函数内部定义total或只是将其设为全局变量。

求和线total += Number(total);必须在循环内从total中提取data ajax response

因此您的功能可以如下所示:

       function get_all() {
            var total=0;//local varialble
            var from_date = $('#from_date').val();
            var to_date = $('#to_date').val();
            $('#tbl-projects').dataTable().fnDestroy();

            $.ajax({
                url:"../php/report/all_sales.php",

                type: "POST",
                dataType: 'JSON',
                data:{from_date:from_date, to_date:to_date},
                async:false,
                success: function (data) {

                    $('#tbl-projects').dataTable({
                        dom: 'Bfrtip',
                        buttons: [
                            , 'excel', 'pdf', 'print'
                        ],

                        "aaData": data
                        ,
                        "scrollX": true,
                        "aoColumns": [
                            {"sTitle": "Invoice No", "mData": "id"},
                            {"sTitle": "Date", "mData": "date"},
                            {"sTitle": "Total", "mData": "total"},
                            {"sTitle": "Pay", "mData": "pay"},
                            {"sTitle": "Due", "mData": "due"},

                        ]


                    });

                   data.forEach(function(recordInLoop) {
                        total += Number(recordInLoop.total);
                   });

                },
                error: function (xhr) {
                    console.log('Request Status: ' + xhr.status  );
                    console.log('Status Text: ' + xhr.statusText );
                    console.log(xhr.responseText);
                    var text = $($.parseHTML(xhr.responseText)).filter('.trace-message').text();
                    console.log(text)
                }
            });


        }