asafdav / ng-csv

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

Using a promise for header info #58

Open danauerbach opened 10 years ago

danauerbach commented 10 years ago

Hi,

I've just gotten ngCSV to work with my $resource API calls, and it's great!

Just wondering if there is a way to provide header information based on a promise as well? I would like to construct the header array based on the returned data so I don't have to hard-code the header details in my front-end.

Any help greatly appreciated!

And thanks for ng-csv!

dan

asafdav commented 9 years ago

Hi, can you return the header as the first row of the data ?

danauerbach commented 9 years ago

Well, I'm getting a json array of objects, from which I can construct the header array. Problem is it comes with the data, not before.

Thx, Dan

On Oct 4, 2014, at 10:41 AM, Asaf David notifications@github.com wrote:

Hi, can you return the header as the first row of the data ?

— Reply to this email directly or view it on GitHub.

asafdav commented 9 years ago

I see,

Well I think you can use a promise, ng-csv accepts a promise, so in your case you can "resolve" it only after you constructed your header.

Should be something like that:

$scope.getArray = function($q) {
var someResource = $resource('/any/url');
return someResource.get()
    .$promise.then(function(data) {
       $scope.header = something(data);
       return data; 
    });
}
danauerbach commented 9 years ago

Ah, ok, will give it a shot. I assume in your code above, the template would have:

csv-header="header"

Do I have this right?

thx!

asafdav commented 9 years ago

Yup

On Sun, Oct 5, 2014 at 2:07 AM, danauerbach notifications@github.com wrote:

Ah, ok, will give it a shot. I assume in your code above, the template would have:

csv-header="header"

Do I have this right?

thx!

— Reply to this email directly or view it on GitHub https://github.com/asafdav/ng-csv/issues/58#issuecomment-57921318.

asafdav commented 9 years ago

any luck ?

danauerbach commented 9 years ago

Sorry I haven't gotten back to you.

I took an initial stab at it, without success. I set before $scope.header = [] before making async call to server, and set it to actual header array just before resolving data promise, but the new header array isn't being used. Seems like it is already set at that point.

Leaving $scope.header undeclared until promise return results in a error.

I intend to do some more testing today or tomorrow. Will let you know how it goes.

Thanks, dan

On Oct 7, 2014, at 9:12 AM, Asaf David notifications@github.com wrote:

any luck ?

— Reply to this email directly or view it on GitHub.

asafdav commented 9 years ago

OK thanks for the update.

Looking forward

MohammedBenj commented 7 years ago

hello

I have the same issue, the header is based on $promise response, it is possible to call a $promise in header csv-header?

When using the example csv-header="header", the header still always empty.