Saturday 6 May 2017

How to Parse a JSON response and group them date wise and head wise into a table in javascript?

This is the Sample response i am getting from the API.

{"success":true,"transaction":[{"_id":"58efd5717ddda769f26793fc","transId":"Exp/04-17/17","trpId":"Trav/dfsd/04-17/12","tripId":"58efd4dc7ddda769f26793f8","userId":"58ac19eaec1e7e4628be6f01","expenseHeadId":"588f279b85cd494eb7989c83","expenseHeadName":"Conveyance Expenses","transactionAmount":799,"isReimbursible":true,"paymentMode":"Cash","travelPurpose":"Official","hodId":"58aac3cb35c7194023da4777","clientId":"588f279b85cd494eb7989c80","fromCity":"Delhi","toCity":"Bhilai","fromCityId":"57f4aae6c0cd6b4cde54a514","toCityId":"583e0303e69a2e27ed7b416d","tripEndDate":"2017-04-27T18:30:00.000Z","policyDescription":"Not Applicable","expenseType":"general","__v":0,"hodApprovedTime":"2017-04-13T19:46:21.585Z","adminApprovedTime":"2017-04-13T19:47:55.910Z","accountsApprovedTime":"2017-04-13T20:07:30.097Z","paymentId":["58efda82f38d502c9e7fc5c1"],"isAccountsPaid":true,"isCancelled":false,"transactionStatus":"accounts-approved","isAccountsApproved":true,"isAdminApproved":true,"isHodApproved":true,"isDocUploaded":false,"createdTime":"2017-04-13T19:45:53.672Z"},{"_id":"58efd87af38d502c9e7fc5ba","transId":"Exp/04-17/18","trpId":"Trav/dfsd/04-17/12","tripId":"58efd4dc7ddda769f26793f8","userId":"58ac19eaec1e7e4628be6f01","expenseHeadId":"588f279b85cd494eb7989c85","expenseHeadName":"Travel","transactionAmount":1231,"isReimbursible":true,"paymentMode":"Draft","travelPurpose":"Official","hodId":"58aac3cb35c7194023da4777","clientId":"588f279b85cd494eb7989c80","fromCity":"Delhi","toCity":"Bhilai","fromCityId":"57f4aae6c0cd6b4cde54a514","toCityId":"583e0303e69a2e27ed7b416d","tripEndDate":"2017-04-27T18:30:00.000Z","policyDescription":"Not Applicable","expenseType":"general","__v":0,"hodApprovedTime":"2017-04-13T20:00:36.469Z","adminApprovedTime":"2017-04-13T20:00:53.898Z","accountsApprovedTime":"2017-04-13T20:03:39.801Z","paymentId":["58efd99bf38d502c9e7fc5bf"],"isAccountsPaid":true,"isCancelled":false,"transactionStatus":"accounts-approved","isAccountsApproved":true,"isAdminApproved":true,"isHodApproved":true,"isDocUploaded":false,"createdTime":"2017-04-13T19:58:50.678Z"}]}

What i a struggling in is how to display the data on the angular page as a table in which the row heads are the Dates and the Column Heads are the "expenseHeadName" in the response.

I have tried using a lot of different techniques nested loops and so on but so far nothing has worked satisfactorily.

For Example i would want table like this:

DATE        TRAVEL     FOOD   LODGING   TOTAL
15 Jun-2017    250       300     400       950
17 June 2017   300       200       0       500

My code till now (not sure if that will help)

angular.forEach(response, function (value, key) {

        var obj={};
       //var d=new Date(value.createdTime);


       // var d =value.createdTime;
        var d =new Date(value.createdTime);
         var d5 =d.setHours(0,0,0,0);
        var d6=new Date(d5);
         angular.forEach(response, function (value1, key1) {
             var d1=value1.createdTime;
             var d1 = new Date(d1);
            var d2 =d1.setHours(0,0,0,0);
             var d3=new Date(d2);
            if (d6.getTime() == d3.getTime()) 
            {
                obj['date']=d3;

                 angular.forEach(expenseHeadsArray, function (value, key) 
                        {
                            obj[value.expenseHeadName] = 0;

                        });
                obj['total']=0;
                rows.push(obj);

            }                 

         });
      // obj['date']=d.toDateString();

    });



    angular.forEach(rows, function (value_rows, key_rows) {
        angular.forEach(response, function (value_resp, key_resp) {
            var d1 = new Date(value_rows.date);
            var d2 = new Date(value_resp.createdTime);
            var d3 =d2.setHours(0,0,0,0);
            var d4=new Date(d3);
           // if (angular.equals(value_rows.date, value_resp.createdTime)) {
             if (d1.getTime() == d4.getTime()) {
                angular.forEach(expenseHeadsArray, function (value_expenseHead, key_expenseHead) {
                    if (angular.equals(value_resp.expenseHeadName, value_expenseHead.expenseHeadName)) {
                        var exhead=value_expenseHead.expenseHeadName;

                        rows[key_rows][exhead] += value_resp.transactionAmount;
                        rows[key_rows].total += value_resp.transactionAmount;
                }

                });
            }
        });
    });

Maybe i am missing out some easy link but any help would be appreciated.

I am using Angular .js on the frontend and Node.js on the backend



via Vaibhav Singh

No comments:

Post a Comment