gpt4 book ai didi

javascript - 使用纯 JavaScript 将 JSON 文件转换为可排序表

转载 作者:搜寻专家 更新时间:2023-11-01 05:22:02 24 4
gpt4 key购买 nike

我有一个名为 cats.json 的 JSON 文件。

[{
"breed" : "Abyssinian",
"country" : "Ethiopia",
"coffeePreference" : "espresso",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/9/9b/Gustav_chocolate.jpg/100px-Gustav_chocolate.jpg"
}, {
"breed" : "Aegean",
"country" : "Greece",
"coffeePreference" : "medium roast, cream and sugar",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/5/51/Aegean_cat.jpg/100px-Aegean_cat.jpg"
}]

以上是一个简短的片段。我正在尝试使用 getJson 加载此 JSON 文件并将其格式化为可排序的表格。我可以将表格呈现到屏幕上,但无法完全让我的排序功能正常工作。我知道排序功能适用于常规 HTML 表格,我认为这与我的整体方法有关,因为我是前端方面的新手。代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=Windows-1252">
<style type="text/css">
table {
border-collapse: collapse;
border: none;
}
th,
td {
border: 1px solid black;
padding: 4px 16px;
font-family: Times New Roman;
font-size: 24px;
text-align: left;
}
th {
background-color: #C8C8C8;
cursor: pointer;
}
</style>
</head>
<body>
<div id="catTable"></div>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script>
var cats, asc1 = 1,
asc2 = 1,
asc3 = 1;
window.onload = function () {
cats = document.getElementById("cats");
}

function sort_table(tbody, col, asc) {
var rows = tbody.rows,
rlen = rows.length,
arr = new Array(),
i, j, cells, clen;
// fill the array with values from the table
for (i = 0; i < rlen; i++) {
cells = rows[i].cells;
clen = cells.length;
arr[i] = new Array();
for (j = 0; j < clen; j++) {
arr[i][j] = cells[j].innerHTML;
}
}
// sort the array by the specified column number (col) and order (asc)
arr.sort(function (a, b) {
return (a[col] == b[col]) ? 0 : ((a[col] > b[col]) ? asc : -1 * asc);
});
// replace existing rows with new rows created from the sorted array
for (i = 0; i < rlen; i++) {
rows[i].innerHTML = "<td>" + arr[i].join("</td><td>") + "</td>";
}
}
$.getJSON('cats.json', function(cats) {
var output="<table>";
output+="<thead>"
output+="<tr>";
output+="<th> HeadShot </th>";
output+= '<th onclick="sort_table(cats, 0, asc1); asc1 *= -1; asc2 = 1; asc3 = 1;">Breed</th>';
output+= '<th onclick="sort_table(cats, 1, asc2); asc2 *= -1; asc3 = 1; asc1 = 1;">Country</th>';
output+= '<th onclick="sort_table(cats, 2, asc3); asc3 *= -1; asc1 = 1; asc2 = 1;">CoffeePreference</th>';
output+="</tr>";
output+="</thead>";

for (var i in cats) {
output+="<tbody id = 'cats'>";

output+="<tr>";
output+="<td><img src='" + cats[i].picture+"' alt='cat picture'> </td>";
output+="<td>" + cats[i].breed + "</td>";
output+="<td>" + cats[i].country + "</td>";
output+="<td>" + cats[i].coffeePreference + "</td>";
output+="</tr>";
output+="</tbody>";

}
output+="</table>";
document.getElementById("catTable").innerHTML=output;



});


</script>
</body>
</html>

任何帮助或指导。将不胜感激。

最佳答案

https://www.datatables.net/

这将从 JSON 自动为您生成表格并对其进行排序。您的特定设置的一个很好的起点是:

https://www.datatables.net/examples/server_side/object_data.html

您可以在没有“处理”和“服务器端”的情况下使用它,并将“ajax”部分替换为您的 JSON 文件。

编辑

这是您的数据集的基本实现:http://jsbin.com/kajina/1/edit?html,js,output

编辑2

为了使用远程数据源,您需要将 {data: cats} 属性替换为 {ajax: "cats.json"}。这将使 DataTables 为您运行 $.getJSON() 函数并从服务器获取数据。

这里有多种类型的数据源可用https://www.datatables.net/examples/data_sources/

此外,对于大型 JSON 文件,我建议考虑分页(服务器过滤数据并一次只向您发送一页项目)。请参阅此处的文档:https://www.datatables.net/examples/server_side/simple.html

关于javascript - 使用纯 JavaScript 将 JSON 文件转换为可排序表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32017759/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com