如何将数据树下载到CSV?

时间:2019-02-28 22:50:28

标签: tabulator

使用Tabulator时如何将嵌套树数据导出为CSV文件?我尝试使用table.download(“ csv”,“ data.csv”)函数,但是,仅导出了顶层数据行。

看起来是自定义文件格式程序,或者可能需要其他选项才能实现此目的。重新编写CSV下载器似乎很愚蠢,因此在download.js模块中浏览csv下载器时,看起来似乎在找到“ _children”字段后向行解析器添加了递归函数。

我很难弄清楚从哪里开始。

最终,我需要在CSV数据中表示父子关系,并在子行的父ID字段中添加一个值(顶层父行中该字段可以为空,因为它们没有家长)。我认为我需要在数据表中包含一个ID和ParentID来实现此目的,并且可能需要在将数据插入表中时使用一些其他功能来强制执行该键的验证。

2 个答案:

答案 0 :(得分:0)

从4.2版开始,当前无法在下载中包含树数据,这将在以后的版本中出现

答案 1 :(得分:0)

以下是当前将嵌套数据表导出到CSV的方式。这将在末尾插入新列,以包含您选择的父行标识符。如果您不需要它,可以轻松地将其删除或使其成为有条件的。

// Export CSV file to download
$("#export-csv").click(function(){
    table.download(dataTreeCSVfileFormatter, "data.csv",{nested:true, nestedParentTitle:"Parent Name", nestedParentField:"name"});
});

// Modified CSV file formatter for nested data trees
// This is a copy of the CSV formatter in modules/download.js 
// with additions to recursively loop through children arrays and add a Parent identifier column
// options: nested:true, nestedParentTitle:"Parent Name", nestedParentField:"name"
var dataTreeCSVfileFormatter = function(columns, data, options, setFileContents, config){
    //columns - column definition array for table (with columns in current visible order);
    //data - currently displayed table data
    //options - the options object passed from the download function
    //setFileContents - function to call to pass the formatted data to the downloader

    var self = this,
        titles = [],
        fields = [],
        delimiter = options && options.delimiter ? options.delimiter : ",",
        nestedParentTitle = options && options.nestedParentTitle ? options.nestedParentTitle : "Parent",
        nestedParentField = options && options.nestedParentField ? options.nestedParentField : "id",
        fileContents,
        output;

    //build column headers
    function parseSimpleTitles() {
        columns.forEach(function (column) {
            titles.push('"' + String(column.title).split('"').join('""') + '"');
            fields.push(column.field);
        });
        if(options.nested) {
            titles.push('"' + String(nestedParentTitle) + '"');
        }
    }

    function parseColumnGroup(column, level) {
        if (column.subGroups) {
            column.subGroups.forEach(function (subGroup) {
                parseColumnGroup(subGroup, level + 1);
            });
        } else {
            titles.push('"' + String(column.title).split('"').join('""') + '"');
            fields.push(column.definition.field);
        }
    }

    if (config.columnGroups) {
        console.warn("Download Warning - CSV downloader cannot process column groups");

        columns.forEach(function (column) {
            parseColumnGroup(column, 0);
        });
    } else {
        parseSimpleTitles();
    }

    //generate header row
    fileContents = [titles.join(delimiter)];

    function parseRows(data,parentValue="") {
        //generate each row of the table
        data.forEach(function (row) {
            var rowData = [];

            fields.forEach(function (field) {
                var value = self.getFieldValue(field, row);

                switch (typeof value === "undefined" ? "undefined" : _typeof(value)) {
                    case "object":
                        value = JSON.stringify(value);
                        break;

                    case "undefined":
                    case "null":
                        value = "";
                        break;

                    default:
                        value = value;
                }

                //escape quotation marks
                rowData.push('"' + String(value).split('"').join('""') + '"');
            });

            if(options.nested) {
                rowData.push('"' + String(parentValue).split('"').join('""') + '"');
            }

            fileContents.push(rowData.join(delimiter));

            if(options.nested) {
                if(row._children) {
                    parseRows(row._children, self.getFieldValue(nestedParentField, row));
                }
            }
        });
    }

    function parseGroup(group) {
        if (group.subGroups) {
            group.subGroups.forEach(function (subGroup) {
                parseGroup(subGroup);
            });
        } else {
            parseRows(group.rows);
        }
    }

    if (config.columnCalcs) {
        console.warn("Download Warning - CSV downloader cannot process column calculations");
        data = data.data;
    }

    if (config.rowGroups) {
        console.warn("Download Warning - CSV downloader cannot process row groups");

        data.forEach(function (group) {
            parseGroup(group);
        });
    } else {
        parseRows(data);
    }

    output = fileContents.join("\n");

    if (options.bom) {
        output = "\uFEFF" + output;
    }

    setFileContents(output, "text/csv");
};
相关问题