合并月份总量

时间:2017-07-14 08:47:46

标签: jquery html angularjs json

我以表格形式获取数据。现在我需要合并同月的数据。显示如下表格。请帮我解决一下这个。提前致谢 例如:目前该表就像

Month | Total Amount
Jan   | 100
Jan   | 100
Mar   | 200
Apr   | 300
Apr   | 200

我希望输出像

 Month | Total Amount
    Jan   | 200
    Mar   | 200
    Apr   | 500

以下是代码:

    success: function (data) {
        if (data.length > 0) {

            var tr;
            var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun","Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

            for (var i = 0; i < data.length; i++) {

                var date = new Date(data[i].CompDt);

                var month = monthNames[date.getMonth()];
                var total = 0;
                var totalamt = data[i].TotalAmt;
                tr = $('<tr/>');
                tr.append("<td>" + month + "</td>");
                tr.append("<td>" + totalamt + "</td>");
                $('#GraphTable').append(tr);
            }

            var total = 0;
            for (var i = 0; i < data.length; i++) {
                total = total + data[i].TotalAmt;
            }
            $('#GraphTable').append('<tr style="font-weight: bold; background-color: white"><td colspan="2" align="right">Total: </td><td>' + total + '</td></tr>');
            tr = $('<tr style="font-weight: bold; background-color: white" />');
            tr.append("<td></td>");
            tr.append("<td></td>");


            $('#GraphTable').append(tr);

        }
        else {

            tr = $('<tr/>');
            tr.append("<th colspan='10' style='text-align:center'>No Data to display</th>");

            $('#GraphTable').append(tr);
        }

    } //end of success
});

3 个答案:

答案 0 :(得分:1)

成功更新以下代码。

success: function (data) {
    if (data.length > 0) {

        var tr;
        var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun","Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

        var monthsAmt = [0,0,0,0,0,0,0,0,0,0,0,0];

        for (var i = 0; i < data.length; i++) {
            var date = new Date(data[i].CompDt);
            var monthIdx = monthsAmt[date.getMonth()];
            monthsAmt[monthIdx] += data[i].TotalAmt;
        }

        for(var i =0;i<12;i++){
        if(monthsAmt[i])
            tr = $('<tr/>');
            tr.append("<td>" +monthNames[i] + "</td>");
            tr.append("<td>" + monthsAmt[i] + "</td>");
            $('#GraphTable').append(tr);
          }
        }
        var total = 0;
        for (var i = 0; i < data.length; i++) {
            total = total + data[i].TotalAmt;
        }
        $('#GraphTable').append('<tr style="font-weight: bold; background-color: white"><td colspan="2" align="right">Total: </td><td>' + total + '</td></tr>');
        tr = $('<tr style="font-weight: bold; background-color: white" />');
        tr.append("<td></td>");
        tr.append("<td></td>");


        $('#GraphTable').append(tr);

    }
    else {

        tr = $('<tr/>');
        tr.append("<th colspan='10' style='text-align:center'>No Data to display</th>");

        $('#GraphTable').append(tr);
      }
    } //end of success
});

答案 1 :(得分:0)

这可能有效:

var result=[];

        success: function (data) {
                if (data.length > 0) {

                    var tr;
                    var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun","Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

                    for (var i = 0; i < data.length; i++) {
                        var totalAmt=0;
                         if(data[i].CompDt==data[i+1].CompDt)
                         {  
                            totalAmt= data[i].totalamt+data[i+1].totalamt;
                            result.push({'Month':data[i].CompDt, 'TotalAmount':totalAmt})
                         }

            } //end of success
        });

答案 2 :(得分:0)

我找到了我的解决方案:

 var tr;
                    var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
                    var monthsAmt = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

                    for (var i = 0; i < data.length; i++) {
                        var date = new Date(data[i].CompDt);
                        var monthIdx = monthNames[date.getMonth()];

                        switch(monthIdx)
                        {
                            case "Jan": monthsAmt[0] += data[i].TotalAmt;
                                break;

                            case "Feb": monthsAmt[1] += data[i].TotalAmt;
                                break;

                            case "Mar": monthsAmt[2] += data[i].TotalAmt;
                                break;

                            case "Apr": monthsAmt[3] += data[i].TotalAmt;
                                break;

                            case "May": monthsAmt[4] += data[i].TotalAmt;
                                break;

                            case "Jun": monthsAmt[5] += data[i].TotalAmt;
                                break;

                            case "Jul": monthsAmt[6] += data[i].TotalAmt;
                                break;

                            case "Aug": monthsAmt[7] += data[i].TotalAmt;
                                break;

                            case "Sep": monthsAmt[8] += data[i].TotalAmt;
                                break;

                            case "Oct": monthsAmt[9] += data[i].TotalAmt;
                                break;

                            case "Nov": monthsAmt[10] += data[i].TotalAmt;
                                break;

                            case "Dec": monthsAmt[11] += data[i].TotalAmt;
                                break;
                        }

                    }
                    for(var i =0;i<12;i++){
                        if(monthsAmt[i])
                            {
                            tr = $('<tr/>');
                        tr.append("<td>" +monthNames[i] + "</td>");
                        tr.append("<td>" + monthsAmt[i] + "</td>");
                        $('#GraphTable').append(tr);
                    }
                }