【发布时间】:2017-01-10 21:33:14
【问题描述】:
最近我在这里寻求有关 javascript 的帮助。我需要能够解析 CSV 文件并将其输出到 HTML 的东西。
有人能够大力帮助我。唯一的问题是它输出为单行表。在 CSV 文件中,每一行没有特定数量的列/数据,这意味着行数据的长度不同。
我一直在尝试做的是编写一些 if 语句,只选择“姓氏”或“已知”之类的内容,这样我就可以对结果进行排序。
最好的方法是什么?我需要设置输出数据的样式,所以我认为 div id 会比表格更好。另外,我应该在哪里编辑代码(我的 javascript 知识非常初学者)。
如果我尝试过的语句(可能完全错误):
function firstName($container){
var firstN = $container;
var n = firstN.includes("First Name");
if (n != 0){
document.getElementById("first_name").innerHTML="First name = ";
return;
}
}
主要代码块(CSV 文件可在http://www.fooda.website/testResults.csv 找到):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="jquery-1.12.3.min.js" type="text/javascript">
</script>
<title>Untitled Document</title>
<script type="text/javascript">
// ============================
// Allow for a cached result
// ============================
var csvRows = [];
// ============================
// ============================
// Given an array of rows build a table.
// ============================
function buildTable(csvRows){
// Our base result
var $table = $("<table cellpadding=\"2\" cellspacing=\"0\"></table>");
// ============================
// For each row in the CSV build a <tr /> and append it to the <table />
// ============================
$table = csvRows.reduce(function($table, csvRow){
// For this demo just take the first few cells/columns
var csvRowCells = csvRow.split(",");
// Our base table row
var $tr = $("<tr>/tr>");
// ============================
// For each cell row build a <td /> and append it to the <tr />
// ============================
$tr = csvRowCells.reduce(function($tr, csvRowCell){
return $tr.append($("<td>/</td>").text(csvRowCell));
}, $tr);
// ============================
// Add our new <tr /> to the table then return the table
return $table.append($tr);
}, $table);
// ============================
return $table;
}
// ============================
// ============================
// Given an array of rows, randomly select one (as an array) and build a table with it.
// ============================
function fillContainerWithTable(csvRows, $container){
var randomRow = [csvRows[Math.floor(Math.random() * csvRows.length)]];
var $table = buildTable(randomRow);
$container.append($table);
}
// ============================
// ============================
// the click handler
// ============================
function myFunction(){
// some random csv I found...
var uri = "http://www.fooda.website/testResults.csv";
var $container = $("#wrap");
// You probably want a clean slate.
$container.empty();
// ============================
// If we have the data locally already just use it.
// ============================
if (csvRows.length !== 0){
console.log("using local data...");
fillContainerWithTable(csvRows, $container);
return;
}
// ============================
console.log("fetching remote data...");
$.get(uri, function(data){
csvRows = data.split("\n");
fillContainerWithTable(csvRows, $container);
});
}
// ============================
</script>
<style type="text/css">
body {
font-family: arial, helvetica, sans-serif;
font-weight: normal;
font-size: 13px;
color: #000;
text-align: left;
margin: 3px 0px;
}
#wrap {
padding: 20px;
}
#wrap table {
border: solid 1px;
border-collapse: collapse;
background-color: aliceblue;
height:400px;
width:100%;
}
#first_name {
height:200px;
width:200px;
background-color:#0C0;
}
</style>
</head>
<body>
<button onclick="myFunction()">Click me</button>
<div id="wrap"></div>
<div id="first_name">
</div><!--first_name-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</body>
</html>
提前致谢!
【问题讨论】:
-
看起来你的“CSV”实际上是 JSON,对吗?
-
@henry:我也有同样的想法,但更像是每一行都是一个 JSON 对象。但你是对的,“CSV”在这种情况下失去了所有意义。 gezer4000:你能控制“CSV”格式吗?如果将数据转换为真正的 JSON,处理起来会容易得多。如果您设置为 CSV,则应考虑执行更类似于 CSV 的布局,其中每列代表特定类型的值,即使该列为空。
-
@TheJim01 包含的文件只是结果示例。实际文件是 CSV,大约有 700 个条目。我对 javascript 非常陌生,之前只涉足过,所以我不确定什么/如何解析 JSON。我正在寻找一种方法来获取 CSV 行中的每个条目并在 HTML 中输出它,并使用一些代码来过滤结果,以便只显示某些值。
-
这里的错字 - var $tr = $("
/tr>");... 应该是 var $tr = $(" ");
标签: javascript jquery html css csv