将表导出为CSV

时间:2017-10-19 08:47:59

标签: javascript csv blob export-to-csv

我试图将表格内容导出并保存为CSV,并且它正常工作,但我想在导出保存之前隐藏/删除指定div。 在将表格导出/保存为CSV之前,我想隐藏/删除span.delete-list-domains。

jQuery(document).ready(function ($) {
function exportTableToCSV($table, filename) {
    //$("span.delete-list-domains").hide();
    var $rows = $table.find('tr:has(td)'),
    tmpColDelim = String.fromCharCode(11),
    tmpRowDelim = String.fromCharCode(0),
    colDelim = '","',
    rowDelim = '"\r\n"',
    csv = '"' + $rows.map(function (i, row) {
        var $row = $(row),
            $cols = $row.find('td');

        return $cols.map(function (j, col) {
            var $col = $(col),
            text = $col.text();
            return text.replace(/"/g, '""');
        }).get().join(tmpColDelim);

    }).get().join(tmpRowDelim)
        .split(tmpRowDelim).join(rowDelim)
        .split(tmpColDelim).join(colDelim) + '"';

        // Deliberate 'false', see comment below
    if (false && window.navigator.msSaveBlob) {
        var blob = new Blob([decodeURIComponent(csv)], {
            type: 'text/csv;charset=utf8'
        });

        window.navigator.msSaveBlob(blob, filename);

    } else if (window.Blob && window.URL) {       
        var blob = new Blob([csv], { type: 'text/csv;charset=utf8' });
        var csvUrl = URL.createObjectURL(blob);

        $(this)
            .attr({
                'download': filename,
                'href': csvUrl
            });
    } else {
        var csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);
        $(this)
            .attr({
                'download': filename,
                'href': csvData,
                'target': '_blank'
            });
    }
}

$(".export").on('click', function (event) {
    var args = [$('#dvData>table'), 'export.csv'];    
    exportTableToCSV.apply(this, args);
});});

1 个答案:

答案 0 :(得分:1)

它现在正在工作,当我使用remove()而不是隐藏:)。