gpt4 book ai didi

jQuery 数据表错误解析 json

转载 作者:行者123 更新时间:2023-12-01 06:28:17 25 4
gpt4 key购买 nike

我只是尝试使用以下 json 对象进行查询的数据表示例...

[{"firstName":"pom",
"lastName":"sdfpom",
"email":null,
"password":"cfe9a43acec0a35f903bc2d646ce8e58b5ef6b67",
"username":"Dave",
"access":null,
"id":1},
{"firstName":"FirstName",
"lastName":"LastName",
"email":null,
"password":"8d60258ef3ae1b8eae67e9298f15edf5c6e05dfe",
"username":"Username",
"access":null,
"id":2}]

这在下面的变量数据中返回...

<script>
$(document).ready(function() {
$.getJSON('userManagement/getAllUsers', function(data) {
$('#table').dataTable( {
"sAjaxSource": data
});
});
});

</script>
<table id="table">
<thead>
<tr>
<th>firstName</th>
<th>lastName</th>
<th>email</th>
<th>password</th>
</tr>
</thead>
<tbody>
<tr>
<td>Row 1 Data 1</td>
<td>Row 1 Data 2</td>
<td>etc</td>
</tr>
<tr>
<td>Row 2 Data 1</td>
<td>Row 2 Data 2</td>
<td>etc</td>
</tr>
</tbody>
</table>

现在 JSON 似乎是有效的,当我用它做任何其他事情时,例如在 jquery 中使用它,它工作正常,但数据表根本无法正确呈现。我使用的 JavaScript 有问题吗?

最佳答案

默认情况下,DataTables 将为其数据源处理数组数组:当它必须处理对象数组时(如您的情况),还有一个额外的步骤。它在 this example 中有描述。在插件文档中。基本上,您要做的就是添加“列”属性的描述(作为数组):

$('#table').dataTable({
"aaData": data,
"aoColumns": [
{ "mData": "firstName" },
{ "mData": "lastName" },
{ "mData": "email" },
{ "mData": "password" },
{ "mData": "username" },
{ "mData": "access" },
{ "mData": "id" }
]
});

这是fiddle一起玩。

关于jQuery 数据表错误解析 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12844837/

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