1

I have Data like This

Data:

EMPID   MONTHNAME   ABSENTDAYS  DAYSWORKED  PRESENTDAYS PENALTY
10001   January     0           1           1           100
10001   January     0           1           1           100
10001   February    0           1           1           100
10001   February    0           1           1           100
10001   February    0           1           1           100
10001   March       0           1           1           100
10001   April       0           1           1           100
10001   April       0           1           1           100

This is my Code

Code

        var html;
        var totalWorkedDays = 0;
        var totalAbsentDays = 0;
        var totalPresentDays = 0;
        var totalPenalty= 0;
        $(data).each(function (index, th) {
            if (th.MONTHNAME == 'January') {
                count = count + 1;
                totalWorkedDays = totalWorkedDays + parseFloat(th.DAYSWORKED);
                totalAbsentDays = totalAbsentDays + parseFloat(th.ABSENTDAYS);
                totalPresentDays = totalPresentDays + parseFloat(th.ABSENTDAYS);
                totalPenalty= totalPenalty+ parseFloat(th.RATE);
                html = '<td class="text-center" id=jan_' + th.SeqID + '>' + th.MONTHNAME + '</td>'
                + '<td class="text-center">' + totalWorkedDays + '</td>'
                + '<td class="text-center">' + totalAbsentDays + '</td>'
                + '<td class="text-center">' + totalPresentDays + '</td>'
                + '<td class="text-center">' + totalPenalty.toFixed(2) + '</td>'
                + '</tr>';
            }
            if (th.MONTHNAME == 'February') {
                count = count + 1;
                totalWorkedDays = totalWorkedDays + parseFloat(th.DAYSWORKED);
                totalAbsentDays = totalAbsentDays + parseFloat(th.ABSENTDAYS);
                totalPresentDays = totalPresentDays + parseFloat(th.ABSENTDAYS);
                totalPenalty= totalPenalty+ parseFloat(th.RATE);
                html = '<td class="text-center" id=jan_' + th.SeqID + '>' + th.MONTHNAME + '</td>'
                + '<td class="text-center">' + totalWorkedDays + '</td>'
                + '<td class="text-center">' + totalAbsentDays + '</td>'
                + '<td class="text-center">' + totalPresentDays + '</td>'
                + '<td class="text-center">' + totalPenalty.toFixed(2) + '</td>'
                + '</tr>';
            }
        });
        fa_table.append(html);

Result:

Month     TotalAbsent    TotalWorked    TotalPresent    TotalPenalty
February  0              3              3               300.00

The month of January is not showing... i want to show all of the months dynamically with total below.

please help. Thanks in Advance..

Genebert
  • 70
  • 11
  • Hi Mami few suggestions 1. always use trim function while comparing two strings. 2. in your code "totalPresentDays = totalPresentDays + parseFloat(th.ABSENTDAYS);" i think it should be th.PRESENTDAYS – Uttam Gupta Sep 14 '18 at 04:39

1 Answers1

0

You're overwriting your html variable every time you put code in it, and you don't do anything with it until the each() iterator is finished. I would reexamine your if blocks, but the easiest fix is to move the last line of your code inside of that each() function (below the second if block).

tagurit
  • 494
  • 5
  • 13
  • when i move append inside each.. the column would not be sum up – Genebert Sep 14 '18 at 03:43
  • @MamiSasazaki I'm honestly really confused about what you're asking, but I would suggest summing these numbers when you query your data instead of in the JavaScript. See this if you can use SQL: https://stackoverflow.com/questions/20369868/sum-duplicate-row-values – tagurit Sep 14 '18 at 04:27