【问题标题】:Process CSV text within the DOM - JQuery CSV to Table在 DOM 中处理 CSV 文本 - JQuery CSV to Table
【发布时间】:2015-09-13 13:29:12
【问题描述】:

现在我正在使用 javascript 获取位于我的服务器上的 csv 文件,处理文件中的数据,并将结果打印到网页上(以表格格式)。

我不需要获取本地 csv 文件,只需手动将 csv 数据直接内联插入以进行处理。

这是我的代码:

<script>
  $(function() {
    $.ajaxSetup({ mimeType: "text/plain" });
    $('#CSVTable').CSVToTable('https://www.example.com/files/test.csv',
        {
          loadingImage: 'https://www.example.com/images/loading.gif',
          headers: ['Plan Information', '&nbsp;'],
          startLine: 0,
          separator: ","
        });
  });
</script>

<div id="CSVTable"></div>

有人可以帮我找出一种方法来直接在代码中指定 csv 数据,而不是抓取文件吗?理想情况下,我希望将 CSV 数据放在一个 div 中 - 然后以某种方式使用 JS 处理该 div 中的 csv 数据。

仅供参考:我正在使用https://code.google.com/p/jquerycsvtotable/

我也在考虑使用https://github.com/evanplaice/jquery-csv 来解析 div 中的 CSV,但它似乎不起作用。

非常感谢任何帮助。

编辑:

我使用了 Mottie 的 Inline CSV Example,但它似乎仍然不适合我。
这是我的代码:

<html>
<head>
<!-- jQuery -->
<script src="https://www.example.com/tablesorter/dist/js/jquery-1.10.2.min.js"></script>

<!-- Tablesorter: required -->
<link rel="stylesheet" href="https://www.example.com/tablesorter/dist/css/theme.blue.css"> <!-- choose any theme -->
<script src="https://www.example.com/tablesorter/dist/js/jquery.tablesorter.js"></script>

<!-- build table widget -->
<script type="text/javascript" src="https://www.example.com/tablesorter/dist/js/widgets/widget-build-table.js"></script>
</head>
<body>
<!--
 Note: if the csv data contains commas ("10,000 days") wrap it in quotes
-->
<div class="csv" style="display:none;">
  Album,Artist,Price (USD)
  Lateralus,Tool,$13.00
  Aenima,Tool,$12.00
  "10,000 days",Tool,$14.00
  Down In It,Nine Inch Nails,$3.00
  Broken,Nine Inch Nails,$6.00
  Muse,Black Holes and Revelations,$7.00
  Anon,"fake album, with comma", $1.00
  Album,Artist,Price (USD)
</div>

<div id="csv2Table"></div>

<script>
$(function() {
  $('#csv2Table').tablesorter({
    theme: 'blue',
    widgets: ['zebra'],
    widgetOptions: {
      // *** build widget core ***
      build_type      : 'csv',
      build_source    : $('.csv'),
      build_complete  : 'tablesorter-build-complete', // triggered event when build completes

      // *** CSV & array ***
      build_headers   : {
        rows    : 1,   // Number of header rows from the csv
        classes : [],  // Header classes to apply to cells
        text    : [],  // Header cell text
        widths  : ['3%', '27%', '50%', '20%'] // set header cell widths
      },
      build_footers : {
        rows    : 1,   // Number of header rows from the csv
        classes : [],  // Footer classes to apply to cells
        text    : []   // Footer cell text
      },
      build_numbers : {
        addColumn : "#", // include row numbering column?
        sortable  : true // make column sortable?
      },

      // *** CSV options ***
      build_csvStartLine : 0,  // line within the csv to start adding to table
      build_csvSeparator : "," // csv separator
    }
  });
});
</script>
</body>
</html>

任何建议将不胜感激! 谢谢。

【问题讨论】:

  • CSV 文件只是一个简单的文本,用 e.g. 分隔。逗号或分号。只需在记事本中打开文件并将内容复制粘贴到 JS 变量即可。
  • 我的 CSV 文件包含引号,它似乎干扰了 JS 变量。你介意发布一个例子吗?我真的很感激。
  • 据我了解 CSVToTable 函数的第一个变量是文件路径,当我从其源代码中获取它时,它使用 jquery ajax get 方法从您的服务器中获取它:$.get(csvFile, function(data) { ... });。正如@thomas-weglinski 所说,您可以在 js 文件中定义 csv 数据,将其存储在字符串变量中,但您需要修改 csvToTable 的源代码以防止它从您的服务器获取数据,而是从本地变量获取数据。
  • 在jquerycsvtotable.googlecode.com/svn/trunk/js/…中修改CSVToTable的原代码可以创建函数csvToTable2 ...
  • @NickyTheWrench 不幸的是,引号或换行符等特殊字符应该用 \ 转义。请参考:stackoverflow.com/questions/805107/… 有很多好的做法。出于测试目的,我建议您仅将几行 CSV 文件复制/粘贴到 JS 变量中。

标签: javascript jquery csv tablesorter jquery-csv


【解决方案1】:

如果你使用我的fork of tablesorter,有一个build widget 支持通过 ajax 的 CSV 到表格。构建小部件内置了 CSVToTable 代码,因此 options 包含该插件的选项。

CSV

Album,Artist,Price ($)
Lateralus,Tool,$13.00
Aenima,Tool,$12.00
"10,000 days",Tool,$14.00
Down In It,Nine Inch Nails,$3.00
Broken,Nine Inch Nails,$6.00
Muse,Black Holes and Revelations,$7.00
Anon,"fake album, with comma", $1.00
Album,Artist,Price ($)

HTML

<div id="csv2Table"></div>

脚本

$(function() {
  $('#csv2Table').tablesorter({
    theme: 'blue',
    widgets: ['zebra'],
    widgetOptions: {
      // *** build widget core ***
      build_type      : 'csv',
      build_source    : { url: 'assets/csv.txt', dataType: 'text' },
      build_headers   : {
        widths  : ['30%', '50%', '20%'] // set header cell widths
      }
    }
  });
});

【讨论】:

  • Mottie - 非常感谢您在这里插话。我希望你会。有什么方法可以在不调用外部 txt 文件的情况下做到这一点?我真的需要处理内联的 CSV,可能在 div 内。
  • Inline CSV demo 是 ajax 之前的演示(在手风琴中)。
  • 请查看我更新的问题。再次感谢您对此的帮助。
  • 确保 jQuery、tablesorter 和 build 小部件都按顺序加载。另外,请确保没有 javascript 错误(按 F12 打开开发窗口)。
猜你喜欢
  • 1970-01-01
  • 2015-09-13
  • 2015-03-04
  • 2017-12-04
  • 2020-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-09
相关资源
最近更新 更多