注意:未定义索引:使用Datatables.net服务器端进行绘制

时间:2018-08-17 02:07:34

标签: javascript json ajax datatable

错误消息: 注意:未定义索引:绘制。

另外,我的JSON响应出现错误:{“ draw”:0,“ recordsTotal”:23,“ recordsFiltered”:23,“ data”:[]}

....绘制(上方)应该是1而不是0。

代码:

$(document).ready(function() {
var asc = true;
$('#example').DataTable( {
"processing": true,
"serverSide": true,
"ajax": {
"url": "server.php",
"type": "POST",
},


columnDefs: [{
targets: -1,
defaultContent: '<button type="button">Delete</button>'
}],
rowGroup: {
dataSrc: 1
}
});
});
 </script>
   <body>

 <table id="example" class="display" style="width:100%" class="table table-striped table-bordered table-hover table-condensed">
  <thead class="thead-inverse">
 <tr>
 <th> ID </th>
 <th>First Name </th>
 <th>Last Name </th>
 <th>Position </th>
 <th>Date </th>
<th>Updated </th>
 <th>Action</th>
 </thead> 
 </tr>
         <tbody>

         </tbody>
     </table>
     </div>         
 <?php

 $data=array();
 $requestData= $_REQUEST;

 $count=mysqli_query($con, "SELECT * FROM employees");
 $totalData= $count->num_rows;
 $totalFiltered=$totalData;

 $json_data = array(
            "draw"            => intval( $requestData['draw'] ),   // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw.
            "recordsTotal"    => intval( $totalData ),  
            "recordsFiltered" => intval( $totalFiltered ), 
            "data"            => $data   // total data array
            );

 echo json_encode($json_data);
 ?>
 </script>
   <body>

 <?php
 $data=array();
 $requestData= $_REQUEST;
 $query=mysqli_query($con, "SELECT * FROM employees");
 $totalData= $count->num_rows;
 $totalFiltered=$totalData;

 if( !empty($requestData['search']['value']) ) {
    // if there is a search parameter
    $sql = "SELECT first_name, last_name, position, date, updated";
    $sql.=" FROM employees";
    $sql.=" WHERE first_name LIKE '".$requestData['search']['value']."%' ";
    // $requestData['search']['value'] contains search parameter
    $sql.=" OR last_name LIKE '".$requestData['search']['value']."%' ";
     $sql.=" OR position LIKE '".$requestData['search']['value']."%' ";
      $sql.=" OR date LIKE '".$requestData['search']['value']."%' ";
       $sql.=" OR updated LIKE '".$requestData['search']['value']."%' ";

    $query=mysqli_query($con, $sql);
    $totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result without limit in the query

    $sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."   LIMIT ".$requestData['start']." ,".$requestData['length']."   "; // $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc , $requestData['start'] contains start row number ,$requestData['length'] contains limit length.
    $query=mysqli_query($con, $sql); // again run query with limit

} else {   

    $sql = "SELECT first_name, last_name, position, date, updated";
    $sql.=" FROM employees";
    $sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."   LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
    $query=mysqli_query($con, $sql);

}

$data = array();
while( $row=mysqli_fetch_array($query) ) {  // preparing an array
    $nestedData=array();

    $nestedData[] = $row["titulo"];
    $nestedData[] = $row["descripcion"];

    $data[] = $nestedData;
}

 ?>

很可能我没有答案。但认为这值得一试。我仍在等待datatables.net的回复。谢谢。

Server.php文件:

 <?php
    $table = 'employees';
    $primaryKey = 'id'; // Table's primary key

    $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_details = array(
        'user' => 'username',
        'pass' => 'password',
        'db'   => 'database',
        'host' => 'localhost'
    );

    require( 'ssp.class.php' );

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

3 个答案:

答案 0 :(得分:0)

您的问题之一是,您尝试访问的变量未定义。您正在尝试访问“绘制”变量,但实际上并没有定义它。

我看到您尝试与$requestData= $_REQUEST;一起在"draw" => intval( $requestData['draw'] )上访问它,但是$_REQUEST仅获得当前页面的$ _GET和$ _POST变量。在此处阅读更多信息:http://php.net/manual/en/reserved.variables.request.php

您需要对server.php单独请求以获取这些值。

您的平局为0的原因是intval(null)为0。(未定义的变量为null)

答案 1 :(得分:0)

答案:在server.php文件中将$ _get更改为$ _post。

这是我从datatables.net获得的响应。上面的帖子也与此响应相关:

“您的服务器脚本正在返回绘制值。SSP文档指示您的脚本应使用此参数执行什么操作: https://datatables.net/manual/server-side#Returned-data

每个绘制请求的draw参数增加。该文档的这一部分说明了其用法: https://datatables.net/manual/server-side#Sent-parameters

dataSrc选项不会影响绘制值。”

在我们完全解决此问题后,我将编辑完整答案。但这是目前的一般答案...

答案 2 :(得分:0)

使用isset();

如下所示

    $draw = isset($postData['draw']);
    $start = isset($postData['start']);
    $rowperpage = isset($postData['length']); // Rows display per page
    $columnIndex = isset($postData['order'][0]['column']); // Column index
    $columnName = isset($postData['columns'][$columnIndex]['data']); // Column name
    $columnSortOrder = isset($postData['order'][0]['dir']); // asc or desc
    $searchValue = isset($postData['search']['value']); // Search value

注意:未定义索引:绘制。将解决