最近我在这里找到了一些关于javascript的帮助。我需要解析CSV文件并将其输出为HTML。将这些表格输出转换为div输出
有人能够大量帮助我。唯一的问题是它作为单行表输出。在CSV文件中,每行没有特定数量的列/数据,这意味着行数据的长度会有所不同。
我一直在试图做的是写一些if语句只接收'Last name'或'known for'之类的东西,所以我可以给出结果的一些命令。
这样做的最好方法是什么?我需要设置输出数据的样式,所以我认为div id会比表格更好。另外,我应该在哪里编辑代码(我的JavaScript知识非常初学者)。
if语句我试图(可能完全错误的):
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
@henry:我认为同样的事情,但它更像是每一行都是一个JSON对象。但你说得对,“CSV”在这方面失去了一切意义。 gezer4000:你可以控制“CSV”格式吗?如果将数据转换为真正的JSON,处理起来会容易得多。如果您设置为CSV,则应考虑实施更类似CSV的布局,其中每列表示特定类型的值,即使该列为空。 – TheJim01
@ TheJim01包含的文件只是一个结果样本。实际文件是CSV并且有大约700个条目。我对JavaScript很陌生,因此我不确定怎么解析JSON。我正在寻找一种方法来获取CSV行中的每个条目并使用一些代码在HTML中输出它以过滤结果,以便只显示某些值。 – gezer4000