Share a section of code that exports JSON array data in CSV format:
Html:
<button class= "btn btn-danger" @click = ' exportdata ' > Export </button>
Js:
Export Data exportdata:function () {Let TableHeader = [{colname: ' type ', Coltext: ' Types ',}, {colname: ' Name ', Coltext: ' Product Name ',}, {colname: ' number ', Coltext: ' Quantity ',}, {colname: ' Price ', Coltext: '
Unit price ',}]; Let tablebody = [{type: ' food ', Name: ' Wang-want snow Cake ', Number: ' 100 boxes ', Price: ' 25 yuan/bag '}, {type: ' food ', NA Me: ' Double-sinks ham ', Number: ' 50 boxes ', Price: ' 5 yuan/root '}, {type: ' food ', Name: ' Caterpillar Bread ', Number: ' 10 boxes ', Price: ' 3 Yuan/bag '}, {type: ' food ', Name: ' Alps Lollipop ', Number: ' 10 box ', Price: ' 0.5 yuan/a '}, {type: ' food ', nam E: ' Mengniu yogurt ', Number: ' 20 boxes ', Price: ' 12.9 yuan/bottle '}, {type: ' Fruit ', Name: ' Banana ', Number: ' 10 kg ', Price: ' 2.5 Yuan/kg '}, {type: ' Fruit ', Name: ' Grapes ', Number: ' 20 kg ', Price: ' 4 yuan/catty '}, {type: ' Fruit ', Name: ' Durian ', Number: ' 10 kg ', Price: ' 24 yuan/catty '}, {type: ' Fruit ', Name: ' Plum ', Number: ' 20 Jin ', Price: ' 4 yuan/kg '}
]; Var CSV = ' \ufeff ';
var keys = [];
Tableheader.foreach (function (item) {csv = = ' + Item.coltext + ' ", ';
Keys.push (Item.colname);
});
CSV = Csv.replace (/\,$/, ' \ n ');
Tablebody.foreach (function (item) {var _item = {};
Keys.foreach (function (key) {csv = ' "' + Item[key] + '", ';
});
CSV = Csv.replace (/\,$/, ' \ n ');
});
CSV = csv.replace (/"null"/g, ' "");
var blob = new blob ([CSV], {type: ' Text/csv,charset=utf-8 '}); var csvurl = window.
Url.createobjecturl (BLOB);
var a = document.createelement (' a ');
var now = new Date ();
function To2 (num) {return num > 9 num: ' 0 ' + num; A.download = ' Purchase information export ' + now.getfullyear () + To2 ((Now.getmonth () + 1)) + To2 (Now.getdate ()) + To2 (now.gethours ()) + to
2 (Now.getminutes ()) + To2 (Now.getseconds ()) + '. csv ';
A.href = Csvurl;
Document.body.appendChild (a);
A.click ();
Document.body.removeChild (a);
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.