PHP:提供JSON来填充数据表


PHP: providing JSON for filling a datatable

有PHP经验,但不熟悉jQuery:我想用ajax从json编码的PHP文件获取数据来填充一个简单的数据表。

table.php: HTML代码段

<head>...
<script type="text/javascript" charset="utf-8">
    $(document).ready(function() {
        $('#example').dataTable({
            "bProcessing": true,
            "sAjaxSource": 'deliver_tests.php',
            "sScrollY": "200px",
        "bPaginate": false
        });
    } );
</script>
</head>    
<body> ...
<div id="demo">
    <table cellpadding="0" cellspacing="0" border="0" class="display" id="example" width="100%">
    <thead>
        <tr>
        <th>id</th>
        <th>Status</th>
        <th>Abkürzung</th>
        <th>Test</th>
        <th>Patient</th>
        <th>Datum</th>
    </tr>
    </thead>
    <tbody></tbody>
    </table>
</div>

deliver_tests.php:代码片段:

try {
    $sql = "SELECT tsID, tsStatus, tbShortname, tbName, paCode, tsCompleted_Date FROM test LEFT JOIN testbase ON tsTestBaseID = tbID LEFT JOIN patient ON tsPatientID = paID WHERE tsAccountID=:accID ORDER BY tsCompleted_Date DESC";
    $dbTests = $objDB->prepare($sql);
    $bind=array('accID' => $_SESSION['aID']);
    $dbTests->execute($bind);
    $tests = $dbTests->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $dbe) {
    // error 
}
$tests = array('aaData' => $tests);
echo json_encode($tests);  

查看这里的输出: http://pastebin.com/uZiLepSb

调用table.php,我得到一个 js-alert:

DataTables warning (table id = 'example'): 
Requested unknown parameter '0' from the data source for row 0

我想我必须重新组合这个数组?
我需要一个提示如何继续从这里-提前谢谢你!

每个表行需要是一个数组而不是JSON中的对象,您应该能够通过使用PDO::FETCH_NUM而不是PDO::FETCH_ASSOC来做到这一点。

参见:DataTables AJAX源码示例