javascript - DataTables Net,表中没有可用数据,但提供了有效的 JSON
问题描述
感谢您帮助理解为什么 DataTables 返回 No data available in table。JSON 端点位于https://spreadsheets.google.com/feeds/list/1YFDhlGve0L7tYjYx6WVOPVUA5eIo9lMJfWrKXyU1hAI/1/public/values?alt=json
我使用的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link
rel="stylesheet"
type="text/css"
href="https://cdn.datatables.net/1.10.23/css/jquery.dataTables.css"
/>
<script
type="text/javascript"
charset="utf8"
src="https://cdn.datatables.net/1.10.23/js/jquery.dataTables.js"
></script>
</head>
<body>
<table id="example" class="display" style="width: 100%">
<thead>
<tr>
<th>myid</th>
<th>myname</th>
</tr>
</thead>
<tfoot>
<tr>
<th>myid</th>
<th>myname</th>
</tr>
</tfoot>
</table>
</body>
<script>
$(document).ready(function () {
$("#example").DataTable({
ajax: {
url:
"https://spreadsheets.google.com/feeds/list/1YFDhlGve0L7tYjYx6WVOPVUA5eIo9lMJfWrKXyU1hAI/1/public/values?alt=json",
dataType: "jsonp",
dataSrc: function (json) {
interim_ = json.feed.entry;
let rows = [];
for (const i of interim_) {
let row_ = [i.gsx$myid.$t, i.gsx$myname.$t];
console.log(row_);
rows.push(row_);
}
let final_ = { data: rows };
console.log(final_);
return final_;
},
},
});
});
</script>
</html>
解决方案
您的final_
对象的格式不正确,数据表无法识别。
一个简单的数组就足够了。
更改为return rows
;
关于json
vs jsonp
- 数据源是json
,但 jquery 足够智能,使用dataType: "jsonp"
也将允许json
$(document).ready(function() {
$("#example").DataTable({
ajax: {
url: "https://spreadsheets.google.com/feeds/list/1YFDhlGve0L7tYjYx6WVOPVUA5eIo9lMJfWrKXyU1hAI/1/public/values?alt=json",
dataType: "jsonp",
dataSrc: function(json) {
interim_ = json.feed.entry;
let rows = [];
for (const i of interim_) {
let row_ = [i.gsx$myid.$t, i.gsx$myname.$t];
console.log(row_);
rows.push(row_);
}
//let final_ = {
// data: rows
//};
//console.log(final_);
return rows;
},
},
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.23/css/jquery.dataTables.css" />
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.23/js/jquery.dataTables.js"></script>
<table id="example" class="display" style="width: 100%">
<thead>
<tr>
<th>myid</th>
<th>myname</th>
</tr>
</thead>
<tfoot>
<tr>
<th>myid</th>
<th>myname</th>
</tr>
</tfoot>
</table>
推荐阅读
- go - Go 中的 Java DataOutputStream 替代方案
- jquery - 我可以使用 jQuery 来检测 ID 是否存在任何 CSS 样式吗?
- javascript - 我在 mousemove 上将图像附加到光标上。它一直有效,直到我滚动,分离图像,在页面上向上移动
- angularjs - 获取 firebase.database 不是函数?
- performance - 站点在第一次加载非常慢的 IIS 调试
- neo4j - 是否可以在 CYPHER 中设置(复制)另一个节点的标签?
- reactjs - 在 React Hooks 中使用函数 useEffect() 的问题
- java - 如何从节点获取列表并在 recyclerView 中使用该列表从 firebase 获取数据
- javascript - 使用对象键值对动态创建变量?
- android - 我可以将 Firebase 更新通知发送到 Playstore 上早期没有 Firebase 连接的旧版应用程序吗?