样式数据并使用Angularjs将其导出为CSV

时间:2015-12-29 06:51:41

标签: javascript angularjs csv

$scope.exportData = function ($searchProductDetails) {
    alasql('SELECT * INTO CSV("searchProductDetails.CSV",?) FROM ?',[$searchProductDetails]);
        };

searchProductDetails = [
   {
     "@search.score": 1.634418,
     "brandName": "Aashirvaad",
     "productId": "16361",
     "highLevelCategory": "Food",
     "lowLevelCategory": "Flour / Atta",
     "subCategory": "Wheat Flour / Atta",
     "productName": "Aashirvaad Sharbati Atta",
     "productImageUrl": "Cooking_Aashirvaad-Sharbati-Atta.jpg",
     "P1PKBrandsId": 10009
    },
    {
     "@search.score": 1.5750113,
     "brandName": "Pillsbury",
     "productId": "22017",
     "highLevelCategory": "Food",
     "lowLevelCategory": "Flour / Atta",
     "subCategory": "Wheat Flour / Atta",
     "productName": "Pillsbury Chakki Fresh Atta",
     "productImageUrl": "Cooking_Pillsbury-Chakki-Fresh-Atta.jpg",
     "P1PKBrandsId": 10823
    }
]

我不想导出@search.scoreP1PKBrandsIdproductImageUrl,但现在我可以导出所有内容,我只需要以CSV格式导出。

2 个答案:

答案 0 :(得分:1)

您可以使用REMOVE COLUMNS修饰符。 AlaSQL将为您删除所有不必要的列。试试这段代码:

$scope.exportData = function ($searchProductDetails) {
    alasql('SELECT * REMOVE COLUMNS [@search.score], P1PKBrandsId, \
         productImageUrl INTO CSV("searchProductDetails.CSV",{headers:true}) FROM ?',
         [$searchProductDetails]);
};

答案 1 :(得分:0)

尝试以下代码:

var productData = angular.copy(searchProductDetails);  
var finalJson =[];  
angular.forEach(productData, function(obj) {
            finalJson.push({"brandName": obj.brandName, 
                "productId": obj.productId,
                "highLevelCategory": obj.highLevelCategory,
                "lowLevelCategory": obj.lowLevelCategory,
                "subCategory": obj.subCategory,
                "productName": obj.productName,
            });
        });