Jay Ram Jay Ram - 4 months ago 19
Javascript Question

ConverttoCSV is working but i want the view of the data to be changed in JavaScript Util-Factory in angularJS

var downloadCSV = function() {
var deferred = $q.defer();
var data = [
{"2013" : {}},
{"2014" : {}},
{"2015" : {}}

];
$q.all([
$http({
url: URLFactory.search(),
method: "POST",
data: formatParams(true,'2013')
}).then(function(d) {
data[0]["2013"] = d.data.response;
}),
$http({
url: URLFactory.search(),
method: "POST",
data: formatParams(true,'2014')
}).then(function(d) {
data[1]["2014"] = d.data.response;
}),
$http({
url: URLFactory.search(),
method: "POST",
data: formatParams(true,'2015')
}).then(function(d) {
data[2]["2015"] = d.data.response;
})
]).then(function(){
var r_data = formatData1(data);
var s_type_data = formatData2(data);
var m_cat_data = formatData3(data);
var r_data_csv = UtilsFactory.convertToCSV(r_data,"R 2013 - 2015", true);
var s_type_data_csv = UtilsFactory.convertToCSV(s_type_data,"S Type 2013 - 2015", true);
var m_cat_data_csv = UtilsFactory.convertToCSV(m_cat_data,"M Category 2013 - 2015", true);
var csv = region_data_csv +
spend_type_data_csv +
market_cat_data_csv;
deferred.resolve(csv);
});


return deferred.promise;

};
var formatData1 = function(data) {
var formatted = [];
var struct = {
"A" : {
"R" : "A"
},
"EE" : {
"R" : "EE"
},
"Gre" : {
"R" : "Gre"
}
}
for (var i = 0; i < data.length; i++) {
for (var j in data[i]) {
var r = data[i][j].RDATA;
var stype = data[i][j].STYPE.CURRENT;
for (var k = 0; k < r.length || k < stype.length; k++) {
struct[r[k].R][j + " (Mn " + currency_sym + ")"] [stype[k].STYPE] = parseFloat(r[k].VALUE + stype[k].VALUE).toFixed(2);
};

}
};

for (var i in struct) {
formatted.push(struct[i]);
};
return formatted;
};
var formatData2 = function(data) {

var formatted = [];
var struct = {
"Cl" : {
"S Type" : "Cl"
},
"Li" : {
"S Type" : "Li"
},
"Ma" : {
"S Type" : "Ma"
}

}
for (var i = 0; i < data.length; i++) {
for (var j in data[i]) {
var stype = data[i][j].STYPE.CURRENT;
for (var k = 0; k < stype.length; k++) {
struct[stype[k].STYPE][j + " (Mn " + currency_sym + ")"] = parseFloat(stype[k].VALUE).toFixed(2);
};
}
};

for (var i in struct) {
formatted.push(struct[i]);
};
return formatted;
};


Now i have 2 format data which displays r_data and s_type_data respectively in excel sheet with the help of convertToCSV.

The data is represented in tabular form like:

R 2013 - 2015

R 2013 2014 2015
A 2.5 NA NA
EE 1.5 NA NA
Gre 3 NA NA



S Type 2013-2015

S Type 2013 2014 2015
Cl 67 NA NA
Li 20 NA NA
Ma 19 NA NA


I want the data to appear as :

R S Type 2013 2014 2015
A Cl 0.5 NA NA
A Li 1.2 NA NA
A Ma 1.8 NA NA
Total 2.5


EE Cl
EE Li
EE Ma
Total 1.5


Can anyone tell me what should i change in struct statement? and also instead of 3 formatdata, can i accomplish this in one formatdata function ?

i tried putting stype data in formatdata1 but in console it says :

TypeError: Cannot set property 'Li' of undefined
at formatData1


enter code here

Answer
var deferred = $q.defer();
        var data = [{
            "2014": {}
        }, {
            "2015": {}
        }];
$http({
            url: URLFactory.searchxls(),
            method: "POST",
            data: formatParams(true)
        }).then(function(response) {
var all_data = formatData1(response.data.response.ALLDATA);
var all_data_csv = UtilsFactory.convertToCSV(all_data, " 2014 - 2015", true);
var csv = all_data_csv;

deferred.resolve(csv);
        });
        return deferred.promise;

    };

    var formatData1 = function(data) {

 var formatted = [];
        var R = {
            "R": {},
            "S_type": {},
       "2014": {},
            "2015": {}

        };
for (var i = 0; i < data.length; i++) {
            for (var j = 0; j < data[i].DATA.length; j++) {
            var R = angular.copy(R);
            R.R = data[i].col1name;
            R.S_type = data[i].col2name;

                    for (var k = 0; k < data[i].DATA[j].Values.length; k++) {
                     R['Sum of ' + data[i].DATA[j].Values[k]['col_YEAR']]= parseFloat(data[i].DATA[j].Values[k].VALUE).toFixed(2);    

                    }

                                formatted.push(R);
            }

}

    return formatted;
};
Comments