简体   繁体   English

如何从文件夹的动态csv文件创建HTML数据表

[英]How to create HTML data tables from dynamic csv files of a folder

I was wondering how to create HTML data tables from multiple CSV file. 我想知道如何从多个CSV文件创建HTML数据表。

For example, I have a folder containing CSV files named "Data_Source1-Memory.csv", "Data_Source1-CPU.csv", Data_Source1-Disk.csv", Data_Source2-Memory.csv" and "Data_Source2-CPU.csv" . 例如,我有一个文件夹,其中包含名为"Data_Source1-Memory.csv", "Data_Source1-CPU.csv", Data_Source1-Disk.csv", Data_Source2-Memory.csv" and "Data_Source2-CPU.csv" CSV文件。

These files are created dynamically depending on the "Data_Source1" or "Data_Source2". 这些文件是根据“ Data_Source1”或“ Data_Source2”动态创建的。 So, every time it will not necessarily have Memory or CPU or Disk CSV files. 因此,每次不一定都具有内存,CPU或磁盘CSV文件。

Is there a way I can cycle through these csv files for "Data_Source1" and present these in a HTML table ( one table for Memory, one table for Disk and one table for CPU ). 有没有一种方法可以循环浏览这些用于“ Data_Source1”的csv文件,并将它们呈现在HTML表中( one table for Memory, one table for Disk and one table for CPU )。 Then cycle through the folder for "Data_Source2" and present these csv as HTML tables. 然后循环浏览“ Data_Source2”文件夹,并将这些csv呈现为HTML表。

So far all I can find is jQuery plugins , that as far as I can test will only work for static csv files or data (eg DataTables). 到目前为止,我所能找到的只是jQuery plugins ,据我测试,仅适用于静态csv文件或数据(例如DataTables)。

I also know what files exist so I don't have to search to see if there is a file, I know which files I need. 我也知道存在哪些文件,所以我不必搜索是否有文件,我知道我需要哪些文件。 I just don't know how to loop through different csv files to produce a table per csv file. 我只是不知道如何循环通过不同的csv文件来为每个csv文件生成一个表。

Any help or advice would be much appreciated. 任何帮助或建议,将不胜感激。

As long as the server directory has directory listings enabled, and you run your script from within the domain, then there should be no problems by collecting a list of certain files from a certain directory. 只要服务器目录启用了目录列表,并且您在域中运行脚本,那么从某个目录中收集某些文件的列表就不会有问题。 Simply use $.ajax and parse filenames out of the retrieved directory listing HTML page. 只需使用$.ajax并从检索到的列出HTML页面的目录中解析文件名。 The below works on a standard apache : 以下适用于标准apache:

var fileMask = '-CPU', //signature of the files
    fileExt = 'csv'; //file extension
$.ajax({
    url: 'path/to/directory/',
    success: function (data) {
        var file, files = [];
        $('a[href]', data).each(function(i, a) {
            file = $(a).attr('href');
            if (~file.indexOf(fileMask) && file.match(/[^.]+$/) == fileExt) {
                files.push(file);
            }
        })
    }
})

The result is a files array containing 结果是一个包含以下内容的files数组

['Data_Source1-CPU.csv', 'Data_Source2-CPU.csv', 'Data_Source3-CPU.csv']

You can use files.sort() if you want the list of files to be in numeric order. 如果希望文件列表按数字顺序排列,则可以使用files.sort() Now it should be a nobrainer to use files as base for an automatically created dataTable - see Create dataTable column definition from uploaded csv data - cycle through files and insert the data from each CSV instead of a single file as in the answer. 现在,将files用作自动创建的dataTable的基础应该是明智的选择-请参阅从上传的csv数据创建dataTable列定义 -循环浏览files并插入每个CSV中的数据,而不是答案中的单个文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM