Created
February 10, 2014 16:17
-
-
Save ada-lovecraft/8918810 to your computer and use it in GitHub Desktop.
json to csv downloadl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CSVRow | |
constructor: (bucket) -> | |
@day = moment(bucket.time).format('L') | |
@time = moment(bucket.time).format('LT') | |
@totalAmount = "$#{bucket.totalAmount}" | |
@averageAmount = "$#{bucket.averageAmount}" | |
@transactions = bucket.transactions | |
$scope.generateCSV = -> | |
$scope.csvArray = [] | |
if $scope.useDateRange | |
$scope.csvFilename = $scope.dateRange.start.format('MM-DD-YYYY')+' - ' + $scope.dateRange.end.format('MM-DD-YYYY') + '.csv' | |
else | |
$scope.csvFilename = moment($scope.selectedDay.date).format('MM-DD-YYYY')+'.csv' | |
if $scope.selectedRange | |
for bucket in $scope.selectedRange.buckets | |
$scope.csvArray.push new CSVRow(bucket) | |
csvContent = "data:text/csv;charset=utf-8,"; | |
csvContent += "Day, Time, Total $, Average $, # Transactions\r\n" | |
_.each $scope.csvArray, (obj, index) -> | |
dataString = "#{obj.day},#{obj.time},#{obj.totalAmount},#{obj.averageAmount},#{obj.transactions}" | |
csvContent += dataString | |
csvContent += '\n' if index < $scope.csvArray.length | |
$timeout -> | |
$window.encodedUri = encodeURI(csvContent); | |
$window.open(encodedUri); | |
, 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment