asafdav / ng-csv

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

How to use "csv-column-order" attribute? #123

Closed rahul-desai3 closed 8 years ago

rahul-desai3 commented 8 years ago

An example would be highly appreciated. (I didn't find it in your current examples.)

Thanks, Rahul

shaohaolin commented 8 years ago

Hi, You can define your html like this: <div ng-csv="testDelim" csv-column-order="order" filename="custom.csv"></div> In your JS file: $scope.testDelim = [ {a:1, b:2, c:3}, {a:4, b:5, c:6} ]; $scope.order = [ 'b', 'a', 'c' ]; It output: 2,1,3 5,4,6

rahul-desai3 commented 8 years ago

@shaohaolin It doesnt seem to work as expected. Here is the relevant part of my code:

HTML:

<button 
    class="btn btn-default" 
    ng-csv="csv.csvArray" 
    field-separator="," 
    decimal-separator="."
    csv-column-order="order"
    csv-header="['URL', 'TITLE', 'META DESCRIPTION']"
    filename="{{ csv.filename }}.csv" >
        <span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span>
</button>

JS:

$scope.order = ['link', 'snippet', 'title'];
// all other variables have valid data
// I am running this on Angular.js v1.4.5
rahul-desai3 commented 8 years ago

@shaohaolin I also tried it in jsFiddle and it wouldn't let me download the CSV. http://jsfiddle.net/HB7LU/18206/

shaohaolin commented 8 years ago

@rahul-desai3 You forgot to DI. angular .module('myApp',['ngSanitize', 'ngCsv'])

rahul-desai3 commented 8 years ago

@shaohaolin I added that. It still didn't help :-|

shaohaolin commented 8 years ago

@rahul-desai3 Something wrong when you import the ng-csv file. Please update the latest. Here is jsFiddle link that uses ng-csv cdnjs. http://jsfiddle.net/HB7LU/18208/

rahul-desai3 commented 8 years ago

Updating it to the latest version works! Thanks @shaohaolin :)

shaohaolin commented 8 years ago

@rahul-desai3 You are welcome! Enjoy hacking!