asafdav / ng-csv

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

Is there a way the stop the file download if the array is blank? #165

Open ShaneQuinn opened 8 years ago

ShaneQuinn commented 8 years ago

Can there be a switch added to not initiate a download if the array comes back empty? Or is there a way to call this from a controller so the download call can be initiated after the presence of data can be verified?

pittmaca commented 7 years ago

This, or the ability to short circuit the click event would be a great feature

AguilaneP commented 7 years ago

check for $scope.data(); is undefined in the function $scope.buildCSV in ng-Csv.js

Here is the example:

              /**
                 * Creates the CSV and updates the scope
                 * @returns {*}
                 */
                $scope.buildCSV = function () {
                    var deferred = $q.defer();

                    $element.addClass($attrs.ngCsvLoadingClass || 'ng-csv-loading');
                    var objectData = $scope.data();
                    if (objectData) {
                        CSV.stringify(objectData, getBuildCsvOptions()).then(function (csv) {
                            $scope.csv = csv;
                            $element.removeClass($attrs.ngCsvLoadingClass || 'ng-csv-loading');
                            deferred.resolve(csv);
                        });
                    }
                    $scope.$apply(); // Old angular support

                    return deferred.promise;
                };
monokurobo commented 7 years ago

hi. @AguilaneP , I tried many ways to impl in controller, but failed. Could you help informing how to check in controller?