Search code examples
angularjscsvumbraco7alasql

Alasql how to export data to csv


Okay, so keyResource pulls data from my C# controller as a List of comma separated values. When the button is clicked, the console does log this data but when I set the alasql query to keyResource or to data it says datasource 0 is undefined.

angular.module("umbraco")
.controller("ExportAllController", function($scope, $http, keyResource){
    $scope.exportAll = function ($scope) {
        keyResource.exportAll().then(function (data) {
            console.log(data);
            alasql('SELECT * INTO CSV("AllDictionaryItems.csv",{headers:true}) FROM ?', []);
        });
    };
});

This is what I currently have ^

These are what I have tried:

angular.module("umbraco")
    .controller("ExportAllController", function($scope, $http, keyResource){
        $scope.exportAll = function ($scope) {
            keyResource.exportAll().then(function (data) {
                console.log(data);
                alasql('SELECT * INTO CSV("AllDictionaryItems.csv",{headers:true}) FROM ?', [$scope.exportAll]);
            });
        };
    });

angular.module("umbraco")
.controller("ExportAllController", function($scope, $http, keyResource){
    $scope.exportAll = function ($scope) {
        keyResource.exportAll().then(function (data) {
            console.log(data);
            alasql('SELECT * INTO CSV("AllDictionaryItems.csv",{headers:true}) FROM ?', [keyResource]);
        });
    };
});

angular.module("umbraco")
    .controller("ExportAllController", function($scope, $http, keyResource){
        $scope.exportAll = function ($scope) {
            keyResource.exportAll().then(function (data) {
                console.log(data);
                alasql('SELECT * INTO CSV("AllDictionaryItems.csv",{headers:true}) FROM ?', [data]);
            });
        };
    });

I'm not sure how to do this because when the console is logging the data, I don't know why it's not working when I'm exporting to CSV?


Solution

  • Your current method will not work because you are not passing any data for it to export.

    The reason, I think, your last attempt did not work because your data structure is wrong

    Have a look at below jsfiddle I create, which shows how to create csv from array of string or array of json object.

    http://jsfiddle.net/alantsai/h2wbbkm6/

    see if you try construct your data into one of the structure, and you should be able to export csv file