asafdav / ng-csv

Simple directive that turns arrays and objects into downloadable CSV files
MIT License
573 stars 215 forks source link

CSV from controller - how to specify file? #55

Closed axewelder closed 9 years ago

axewelder commented 9 years ago

I see in issue Exporting from controller #35 (closed), how to drive the action from the controller:

I use the example code show in the issue:

//inject CSV service in your controller $scope.getArray = [{a: 1, b:2}, {a:3, b:4}]; $scope.options = {}; $scope.options.header = ['Field A', 'Field B'];

$scope.send_email = function() { CSV.stringify($scope.getArray, $scope.options).then(function(result){ console.log(result); }); }

The results look correct in the console, but I don't see how I pass in the desired file name and have the file created, like I do in the method from the HTML.

asafdav commented 9 years ago

Using the above code you can create the CSV content but it won't trigger a download.

Due to browsers' policies, a file download should be triggered only by an user action (otherwise it will be treated as popup and will be blocked immediately).

If you want to create a file, please use the directive.

HTIRLANGI commented 8 years ago

Hi asafdav, when I use the directive, {{info.name}}{{info.displayName}} I want to control or trigger ng-csv based on a condition. If that condition fails/ is false, then ng-csv should not be triggered and file download should not happen. How can I control this while using nd-csv directive?

roccomuso commented 7 years ago

+1

enjoiful commented 6 years ago

This is possible. Create a hidden button in your html that has the ng-csv attribute. Then from your Angular1 controller, click the hidden button like so:

$timeout(function() { angular.element('#exportButton').triggerHandler('click'); }); https://stackoverflow.com/questions/22447374/how-to-trigger-ng-click-angularjs-programmatically