【问题标题】:Jquery Datatable problem. Ajax call to PHP recieving json-dataJquery 数据表问题。 Ajax 调用 PHP 接收 json-data
【发布时间】: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


【解决方案1】:

您需要从result(DataTable 的属性aaData)中删除方括号,并应显示为:

 "aaData": result,

并且需要在您的 PHP 文件中进行其他更改(因为您没有通过 while 循环正确编码 JSON 字符串):

$conn = connectDB();
$sql = "SELECT ID, Name, Email FROM `person` WHERE 1";
$result = $conn->query($sql);
if($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $dataArray = array();
        $dataArray[] = $row["ID"];
        $dataArray[] = $row["Name"];
        $dataArray[] = $row["Email"];
        $dataResult[] = $dataArray; 
    }
}
closeDB($conn);

echo json_encode($dataResult);

【讨论】:

    【解决方案2】:

    我认为你的数据应该这样设置:

     while($row = $result->fetch_assoc()) {
        $dataArray[] = array($row["ID"], $row["Name"], $row["Email"] );        
    }
    

    【讨论】:

    • 不,比我在每一列“id”、“name”和“email”都有3个条目。
    猜你喜欢
    • 2011-02-19
    • 2010-09-09
    • 2013-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    相关资源
    最近更新 更多