从CSV导入到HandsOnTable并从HandsOnTable导出到CSV [英] Import from CSV into HandsOnTable and Export to CSV from HandsOnTable

查看:296
本文介绍了从CSV导入到HandsOnTable并从HandsOnTable导出到CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 http://handsontable.com/作为显示项目价格的小部件之一,我在他们的API或常见问题解答中找不到从CSV功能导出和导入的功能. 有没有人实现或知道它?

I am using http://handsontable.com/ as one of the widgets for showing prices for my project, I could not find export and import from CSV feature in their API or FAQ. Has anyone implemented or know about it ?

推荐答案

是的,该注释将您链接至有关操作方法的说明,并

Yup, that comment links you to the explanation on how to do it, and here is my implementation of it for anyone that wants to just reuse code. There are a few enhancements beyond the basic CSV exporting like the escaping of spaces and special characters, as well as apostrophes. It also sets the column headers if they exist so remove that line if you don't have column headers.

假设您有一个id = export-csv的按钮,相关代码:

The relevant code assuming you have a button with id=export-csv:

function parseRow(infoArray, index, csvContent) {
    var sizeData = _.size(hot1.getData());
    if (index < sizeData - 1) {
        dataString = "";
        _.each(infoArray, function(col, i) {
            dataString += _.contains(col, ",") ? "\"" + col + "\"" : col;
            dataString += i < _.size(infoArray) - 1 ? "," : "";
        })

        csvContent += index < sizeData - 2 ? dataString + "\n" : dataString;
    }
    return csvContent;
}

/**
 * Export to CSV button
 */
var exportCsv = $("#export-csv")[0];
if (exportCsv) {
    Handsontable.Dom.addEvent(exportCsv, "mouseup", function(e) {
        exportCsv.blur(); // jquery ui hackfix
        var csvContent = "data:text/csv;charset=utf-8,";
        csvContent = parseRow(colHeaders, 0, csvContent);  // comment this out to remove column headers
        _.each(hot1.getData(), function(infoArray, index) {
            csvContent = parseRow(infoArray, index, csvContent);
        });
        var encodedUri = encodeURI(csvContent);
        var link = document.createElement("a");
        link.setAttribute("href", encodedUri);
        link.setAttribute("download", $("h1").text() + ".csv");
        link.click();
    })
}

希望有帮助!

这篇关于从CSV导入到HandsOnTable并从HandsOnTable导出到CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆