【发布时间】:2019-11-18 13:12:38
【问题描述】:
我尝试创建一个 jquery_dataTable。它与文档 https://datatables.net/examples/api/row_details.html
配合得很好现在我尝试将调用从 "ajax": "objects.txt" 更改为 "ajax": "some.php" ändern。
我的 HTML 表格:
<table id="systeme" class="display" style="width:100%">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody></tbody>
<tfoot>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</tfoot>
</table>
接收数据的ajax调用是:
$(document).ready(function() {
$.ajax({
type : 'POST',
url : 'some.php',
dataType: 'json',
//cache: false,
success : function(result)
{
console.log(result);
$('#systeme').DataTable({
"searching": false,
"aaData": [result], //get the array data from the ajax call.
"aoCcolumns": [
{
"className": 'details-control',
"orderable": false,
"data": null,
"defaultContent": ''
},
{ "result": "ID" },
{ "result": "Name" },
{ "result": "Email" }
],
"order": [[1, 'asc']]
});
}
});
在 PHP 文件中,我连接到数据库并接收信息。
$conn = connectDB();
$dataArray = array();
$sql = "SELECT ID, Name, Email FROM `person` WHERE 1";
$result = $conn->query($sql);
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$dataArray[] = $row["ID"];
$dataArray[] = $row["Name"];
$dataArray[] = $row["Email"];
}
}
closeDB($conn);
echo json_encode($dataArray);
当我检查日志时,我会收到所有相关数据。它们的格式类似于
0: "1"
1: "Tom"
2: "mail@mail"
3: "2"
4: "Tim"
5: "mail@mail"
6: "3"
7: "Daniel"
8: "mail@mail"
但我的桌子里面只有一个条目(第一个条目)。 我不知道如何正确格式化 json 文件或正确处理数据。为了达到这一点,我尝试了很多很多小时,但现在我需要一些帮助。
我对所有这些东西都很陌生,答案会很棒
谢谢
提莫
【问题讨论】:
-
我认为响应数据必须是对象数组而不是文本列表
-
数据表确实有自己的 ajax 处理程序,它只读取 json datatables.net/examples/data_sources/ajax 尝试使用它
-
使用 dt-ajay-notation 进行处理效果很好,但只能使用 formattet txt-File。我的问题是以正确的形式格式化数据...
-
将
aoCcolumns更改为aoColumns,希望它有效
标签: php jquery json ajax datatable