Vue.js Fourth day learning notes _javascript Tips

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.