在datatables.net中使用json返回的未定义值

时间:2018-04-05 23:34:02

标签: php jquery json datatables

简介:我在datatables.net中使用ajax制作服务器端数据表,可在下面找到: https://databasetable-net.000webhostapp.com/

错误: 表格中的数据返回为' undefined'而不是实际数据(点击上面的链接看)。控制台或数据表dubug tracer中没有错误,这就是我现在寻求帮助的原因。

的index.php

$(document).ready(function() {
    $('#example').DataTable( {
        "processing": true,
        "serverSide": true,
            "ajax": {
                "url": "server.php",
                "type": "POST"
            },
            "columns": [
                { "data" : "id"},
                { "data" : "first_name"},
                { "data" : "last_name"},
                { "data" : "position"},
                { "data" : "date"},
                { "data" : "updated"},
            ],
});
 }); 
$(document).ready(function(){ $.getJSON('server.php', function(data){
    var employee_data=''; 
    $.each(data, function(key,value){ 
    employee_data+='<tr>';
    employee_data+='<td>'+value.id+'</td>';
    employee_data+='<td>'+value.first_name+'</td>';
    employee_data+='<td>'+value.last_name+'</td>';
    employee_data+='<td>'+value.position+'</td>';
    employee_data+='<td>'+value.date+'</td>';
    employee_data+='<td>'+value.updated+'</td>';
 }); 
    $('#example').append(employee_data);
    }); 
}); 
  </script>

<table id="example" class="display" style="width:100%">
        <thead>
            <tr>
                <th>ID</th>
                <th>First name</th>
                <th>Last name</th>
                <th>Position</th>
                <th>Date</th>
                <th>Updated</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
             <th>ID</th>
                <th>First name</th>
                <th>Last name</th>
                <th>Position</th>
                <th>Date</th>
                <th>Updated</th>
            </tr>
        </tfoot>
    </table>

<?php
$records = mysqli_query($con, "SELECT * FROM employees");
$totalData= $records->num_rows;
$totalFiltered=$totalData;
$data=array();
while ($row = mysqli_fetch_array($records)) { 
    $subdata=array();
    $subdata[]=$row[0]; //id
    $subdata[]=$row[1]; 
    $subdata[]=$row[2]; 
    $subdata[]=$row[3]; 
    $subdata[]=$row[4]; 
    $subdata[]=$row[5]; 
    $data[]=$subdata;
}

$requestData= $_REQUEST;
//https://www.datatables.net/forums/discussion/comment/94864/

$json_data = array(
                  "draw" => intval(isset($_GET['draw'])), 
                  "recordsTotal"    => intval( $totalData ), 
                  "recordsFiltered" => intval( $totalFiltered ),
                  "data"            => $data //How To Retrieve This Data
                 );

echo json_encode($json_data);  
?>

Server.php

$table = 'employees';

// Table's primary key
$primaryKey = 'id';

// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
    array( 'db' => 'id', 'dt' => 0 ),
    array( 'db' => 'first_name', 'dt' => 1 ),
    array( 'db' => 'last_name',  'dt' => 2 ),
    array( 'db' => 'position',   'dt' => 3 ),
    array( 'db' => 'date',     'dt' => 4 ),
     array( 'db' => 'updated',     'dt' => 5 ),
);

// SQL server connection information
$sql_details = array(
    'user' => 'id3741634_username',
    'pass' => 'password',
    'db'   => 'id3741634_database',
    'host' => 'localhost'
);

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * If you just want to use the basic configuration for DataTables with PHP
 * server-side, there is no need to edit below this line.
 */

require( 'ssp.class.php' );

echo json_encode(
    SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);

结论: 像本文一样排序,但我没有使用stringify:JSON returning undefined values

我主要使用这篇文章作为我的代码的基础。 https://datatables.net/examples/data_sources/server_side

我不确定如何解决此问题,没有错误和少量JSON经验。我怀疑我的问题可能是次要的。如果我可以进行任何更改以改进我的帖子,请告诉我。谢谢!

1 个答案:

答案 0 :(得分:0)

1)摆脱手动插入html的初始 std::wifstream file(filepath); std::wstringstream ss; ss << file.rdbuf(); for (int i = 0; i < 42; ++i) { wchar_t ch; ss >> ch; std::cout << static_cast<unsigned>(ch) << ' '; } 调用。当您使用带有ajax源数据的数据表时,您需要让数据表构建html。因为它是插入行(以及<{em> $.getJSON()之后隐含的<tbody> ),这也是有问题的。

2)你的ajax调用包括初始<tfoot>都返回0记录,并且是空数组。这就是为什么所有单元格值都未定义的原因。实际上这些行不应该存在......但不管怎样,你的$.getJSON()脚本都没有返回任何行。

如果您尝试更改左上角的Server.php选择菜单,您会看到表格已正确更新服务器端数据。请注意它现在如何显示(正确) Show XX Entries