将数据加载到数据表中

时间:2018-10-26 10:39:24

标签: javascript php json datatable

我正在从数组中的数据库中获取数据,然后将其转换为json对象,以将数据传递到数据表,但是表中未显示数据,而在控制台上却显示了数据。我是Data Table插件的新手,无法弄清楚我在哪里做错了。 有帮助吗?

这是我的代码

<table id="example" class="display" cellspacing="0" width="90%">
    <thead>
        <tr>
            <th>DESIGNATION</th>
            <th>EMP_FIRST_NAME</th>
            <th>EMP_MIDDLE_NAME</th>
            <th>EMP_LAST_NAME</th>
            <th>HOME_ADDRESS</th>
        </tr>
    </thead>
</table>

<script>
        $.ajax({
        url: "test2.php",
        type: "POST",
        dataType: 'json',
        processing: true,
        serverSide: true,

            success: function(data) {
                alert(data);
                console.log(data);
                assignToEventsColumns(data);
            }
    });


    function assignToEventsColumns(data) {
        var table = $('#example').dataTable({
            "bAutoWidth": false,
            "bProcessing": true,
            "columns": [{
                "mData": "DESIGNATION"
            }, {
                "mData": "EMP_FIRST_NAME"
            }, {
                "mData": "EMP_MIDDLE_NAME"
            }, {
                "mData": "EMP_LAST_NAME"
            }, {
                "mData": "HOME_ADDRESS"
            }]
        })
    }

</script>

这是我的后端代码:

function recordSetToJson($resultSet){
$rs = array();
while($rs[] = odbc_fetch_array($resultSet)) {
    // you don´t really need to do anything here.
}
//return json_encode($rs);
return $rs;}

function getFormListForHR($client_Id,$pageNo,$pageSize) {
    $dbcon = new portlet_connect();
    $conn = $dbcon->getConnection();
    $sql = "SELECT * FROM (SELECT a.*, ROWNUM r__
            FROM (SELECT * FROM FORM_MAIN_MEMBER WHERE USER_ID in (select USER_ID from PORTLETS_USERS Where CLIENT_ID = '$client_Id') ORDER BY FORM_ID DESC ) a
            WHERE ROWNUM < ( ($pageNo * $pageSize) + 1))
            WHERE r__ >= ( ( ($pageNo-1) * $pageSize) + 1) ";
    $HRForms = recordSetToJson(odbc_exec($conn, $sql));
    odbc_close($conn);

    $jsonData = json_encode(array('MembersList' => $HRForms), JSON_FORCE_OBJECT);;
    return $jsonData;}

echo getFormListForHR($client_Id,$pageNo,$pageSize);

0 个答案:

没有答案