【发布时间】:2021-09-21 20:45:19
【问题描述】:
试图直接从 Google Sheet 为 Datatables 表提供一组 json 数据,我首先遇到了这种错误:
DataTables warning: table id=example - Requested unknown parameter 'name' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4
这是我的 json 与 in their example 提供的 json,当然格式不同
{
"range": "json!A1:Y1000",
"majorDimension": "ROWS",
"values": [
[
"name",
"position",
"salary",
"start_date",
"office",
"extn"
],
[
"Unity Butler",
"prova",
"$85,675",
"2009/12/09",
"San Francisco",
"5384"
]
]
}
{
"data": [
{
"id": "1",
"name": "Tiger Nixon",
"position": "System Architect",
"salary": "$320,800",
"start_date": "2011/04/25",
"office": "Edinburgh",
"extn": "5421"
},
{
"id": "2",
"name": "Garrett Winters",
"position": "Accountant",
"salary": "$170,750",
"start_date": "2011/07/25",
"office": "Tokyo",
"extn": "8422"
},
{...}
]
}
我了解不同的格式需要不同的处理方法,我很想知道如何处理。我接受的@andrewjames 提供的答案解释了不同格式的性质以及如何处理它们。
基本上只需要从ajax option 中删除“columns”参数,正如您可以从jsfiddle 中看到的那样,您可以在其中找到完整的工作代码(以及库)。
更新:补充任务
现在,假设您想将一些额外信息安排到一组 toggle hide-show child rows 中。
只要数据格式尊重示例提供的原始格式,我可以轻松做到这一点:我什至可以通过 .php 文件从 mysql db 中获取数据并将其排列为 json! jsfiddle
<?php
$con=mysqli_connect("","","","");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$out = array();
if ($result = mysqli_query($con, "SELECT * FROM your_table")) {
$out = $result->fetch_all(MYSQLI_ASSOC);
}
echo "{ \"data\": ", json_encode( $out ), "}";
mysqli_close($con);
?>
当我尝试从我常用的 GSheet-fed json 数组中提取数据时,问题就开始了:如何结合子行切换功能和 GSheet 通常格式化的 json 文件?
【问题讨论】:
标签: javascript json google-sheets datatables google-sheets-api