【问题标题】:How do I download data trees to CSV?如何将数据树下载到 CSV?
【发布时间】:2019-02-28 22:50:27
【问题描述】:

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

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

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

最终,我需要在 CSV 数据中使用子行中父 ID 字段中的值表示父子关系(该字段在顶级父行中可以为空白,因为它们没有父母)。我想我需要在数据表中包含一个 ID 和 ParentID 来实现这一点,并且可能在将数据插入表中时使用一些额外的函数来强制验证该键。

【问题讨论】:

    标签: tabulator


    【解决方案1】:

    以下是我目前如何将嵌套数据表导出到 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");
    };
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 嗨 @oli-folkerd,我从 4.2 modules/download.js 复制了 CSV 格式化程序,将其放入新的文件格式化程序并进行了一些修改以支持嵌套数据。它似乎对我的目的很有效,但我没有修改它以与列组一起使用,因为它们的解析似乎有点不同。我将在下面发布代码以防其他人使用它。如果这与您计划开发该功能的方式一致,我很乐意在这方面进行更多工作并提出拉取请求。
      猜你喜欢
      • 1970-01-01
      • 2021-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多