当我尝试将ng-table中的过滤器数据下载到CSV时,我将单行拆分为两行。
输出如下
Row1 包含三列
Item 1 | 12222-12228-14567-124568-18680-20940-18717-ABCDED-sdf_dsfsdf | X-Large
取而代之的是
第一行
Item 1 | 12222-12228-14567-124568-18680-20940-18717-ABCDED-sdf_dsfsdf |
第二行
X-Large
这是我的代码
var data = [{ "name": "Item 1", "color": "12222-12228-14567-124568-18680-20940-18717-ABCDED-aSFDasdf_sdfsdf_dsfsdf", "size": "X-Large" },
{ "name": "Item 2", "color": "Green", "size": "X-Large" },
{ "name": "Item 3", "color": "Green", "size": "X-Large" }];
$scope.downloadData = function (data){
var csv = $scope.JsonToCsv(data);
}
$scope.JsonToCsv = function(objArray) {
var array = typeof objArray != 'object' ? JSON.parse(objArray) :objArray;
var str = '';
var line = '';
var head = array[0];
for (var index in array[0]) {
var value = index + "";
line += '"' + value.replace(/"/g, '""') + '",';
}
line = line.slice(0, -1);
str += line + '\r\n';
for (var i = 0; i < array.length; i++) {
var line = '';
for (var index in array[i]) {
line += array[i][index] + ',';
}
line = line.slice(0, -1);
str += line + '\r\n';
}
return str;
}
任何人都可以通过查找我的代码中出现的问题来帮助我吗?
答案 0 :(得分:0)
我有一个工作代码,我根据你的需要改变了。希望这对你也有用
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
var str = '';
var line = "";
for (var index in array[0]) {
line += index + ',';
}
line = line.slice(0, -1);
str += line + '\r\n';
for (var i = 0; i < array.length; i++) {
var line = "";
for (var index in array[i]) {
line += '"' + array[i][index] + '",';
}
line.slice(0, line.length - 1);
str += line + '\r\n';
}
if (str == '') {
alert("oops");
return;
}
console.log(str);
return str;
谢谢:)