How to export JavaScript array info to csv (on client side)?

前端 未结 29 2114
没有蜡笔的小新
没有蜡笔的小新 2020-11-21 21:55

I know there are lot of questions of this nature but I need to do this using JavaScript. I am using Dojo 1.8 and have all the attribute info in array, which loo

29条回答
  •  轮回少年
    2020-11-21 22:30

    In case anyone needs this for knockout js, it works ok with basically the proposed solution:

    html:

    Download
    

    view model:

    // for the download link
    this.filename = ko.computed(function () {
        return ko.unwrap(this.id) + '.csv';
    }, this);
    this.csvContent = ko.computed(function () {
        if (!this.csvLink) {
            var data = ko.unwrap(this.data),
                ret = 'data:text/csv;charset=utf-8,';
    
            ret += data.map(function (row) {
                return row.join(',');
            }).join('\n');
    
            return encodeURI(ret);
        }
    }, this);
    

提交回复
热议问题