asafdav / ng-csv

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

Can we use json key as header instead of defining individual header on csv-header. How do we pass dynamic header according to data assigned to ng-csv ? #52

Closed trpansh closed 10 years ago

trpansh commented 10 years ago

Can we use header from the data "downloadProjects" on csv-header instead of "['Field A', 'Field B', 'Field C', 'Field D', 'Field E']" and how do we do it?

asafdav commented 10 years ago

Hi, I'm not sure what you mean, what is downloadProjects ? Can you please post an example of the wanted usecase ?

trpansh commented 10 years ago

<button type="button" ng-csv="downloadProjects" csv-header="['Field A', 'Field B', 'Field C']" filename="test.csv">Export</button>

downloadProjects is a data to be exported to CSV which is in the format as follows [{ "agency": "Road boards", "budget": 0.0, "category": "GOODS", "contractor": "", "status": "Closed", "title": "DR23-4/070-71 /139" }] and I was wondering whether we could use key as csv-header dynamically.

asafdav commented 10 years ago

Sorry for my late response, You can just send a function to csv-header that returns Object.keys(downloadProjects)

andrewboni commented 9 years ago

I'd also like something like this- where the key of an object becomes the column header. By loading the csv-header separately with something like Object.keys() like you mentioned, there is no order guarantee:

// array like object with random key ordering
var an_obj = { 100: 'a', 2: 'b', 7: 'c' };
console.log(Object.keys(an_obj)); // console: ['2', '7', '100']