-1

I have an application where I make a http request from angularjs to a Java Application to fetch the data from DB.

I am getting the json as reponse in json array format.

[ {
"pumpId": 33,
"pVVoltage": "214",
"pVCurrent": "0",
"epochTime": 1482690480000
},
  {
"pumpId": 33,
"pVVoltage": "214",
"pVCurrent": "0",
"epochTime": 1482690480000
}
]

Task: On clicking Download button, angularjs controller can convert json object array to csv and give option to download this file to user.

HTML:

<button type="button" class="btn btn-primary btn-sm" ng-click="get_report(report_from_time, report_to_time)">Download</button>

get_report() have the json data.

PumpService.getReport($scope.id, $scope.timestamp_report_from_time, $scope.timestamp_report_to_time).success(function (data) {
                $scope.reportData = data;
            }).error(function (error) {
                console.error('ERROR OCCURED:', +error);
            })

What I need:

  1. Way to convert the json array into csv file in angularjs controller.

  2. On clicking Download button, this converted csv file can be downloaded into local system. Any guidance would be highly appreciated.

MonsterJava
  • 423
  • 7
  • 23
  • 1
    So whats the actual question, at the current state your post is too broad. – JDurstberger Jan 18 '17 at 12:06
  • I need to export the converted csv file. Simply means, I have the json data, I need to convert this json data into csv and export (download). – MonsterJava Jan 18 '17 at 12:08
  • I hope you do not expect someone to post the complete answer for you. I suggest you first find out how to do the conversion and how to create downloadable files. If you then run into a specific problem come back and create a new question. – JDurstberger Jan 18 '17 at 12:11
  • thanks a lot. @Altoyyr you just opened my eyes. – MonsterJava Jan 18 '17 at 12:15
  • Possible duplicate of [Export to xls using angularjs](http://stackoverflow.com/questions/21680768/export-to-xls-using-angularjs) – Kursad Gulseven Jan 20 '17 at 19:59

1 Answers1

0

Try this http://ngmodules.org/modules/ng-csv

For working example - https://asafdav.github.io/ng-csv/example/

Ankur Akvaliya
  • 2,989
  • 4
  • 28
  • 53