Tabulator - 下载保持原始 Javascript 结构的文件

问题描述 投票:0回答:1

我正在使用 Tabulator 开发交互式网页,但我在 文件下载 方面遇到了一些问题。 基本上,我想以 Javascript 格式 下载表格。这意味着我正在将一个 Javascript 表加载到网页中,用户可以交互并更改表中的某些值并Save更改。单击“保存”时,我想下载一个文件,该文件必须与我最初加载的 Javascript 相同(但是,当然,用户更改了值)。

在 Tabulator 网站上,我发现只能以 CSV、HTML、XLSX、JSON 和 PDF 格式执行保存/下载,而不能以 Javascript.

例如,要下载 CSV 格式的数据,可以执行以下操作:

table.download("csv", "data.csv")
我想知道是否有类似的东西:
table.download("js", "data.js")

我试图实现的解决方案是创建我的自定义 file formatter,它可以解决问题,但我想知道是否有更优雅、更简单的解决方案,因为我需要处理不同的情况,因为我使用不同的表结构(嵌套,分组,...)。

提前感谢您的帮助!

javascript web-applications download web-development-server tabulator
1个回答
0
投票

最后,我决定继续创建一个自定义文件格式化程序来生成一个 Javascript 文件。 代码是:

//custom file formatter
var jsFileFormatter = function(list, options, setFileContents, mimeType){
  //list - an array of export rows representing one row of data for the table;
  //options - the options object passed from the download function
  //setFileContents - function to call to pass the formatted data to the downloader

  var fileContents = [];
  var first_row = true;
  var jsConstructorFields = ["  {field1:\"", "\", field2:\"", "\", field3:\"", "\", field4:\"", "\", field5:\"", "\", field6:\""];

  fileContents.push("var tabledata = [\n");
  //iterate over rows
  list.forEach((row) => {
    var item = [];
    var field_cnt = 0;

    if (first_row==false) {
      //iterate over the columns in a row
      row.columns.forEach((col) => {
          if(col){
              item.push(jsConstructorFields[field_cnt]);
              item.push(col.value);
              field_cnt++
          }
      });

      // End of row
      item.push("\"},\n");
      fileContents.push(item.join(''));
    } else {
      first_row=false;
    }
  });

  fileContents.push("];");
  //trigger file download, passing the formatted data and mime type
  setFileContents(fileContents, "text/plain");
}

使用此格式化程序的下载可以使用以下方式触发:

  table.download(jsFileFormatter, "file.csv");
© www.soinside.com 2019 - 2024. All rights reserved.