【问题标题】:DataTable rowReordering issue PHPDataTable rowReordering问题PHP
【发布时间】:2018-04-10 06:21:18
【问题描述】:

我尝试从数据库表中检索数据并显示在 DataTable 中,但出现以下错误

DataTables 警告:表 id=example - JSON 响应无效。有关此错误的更多信息,请参阅http://datatables.net/tn/1

您能帮我看看这段代码中的错误吗?

PHP

$displayBoxList = $this->category_m->getCategoryDisplayboxList(null, $type, $page);
$boxList = array();

foreach($displayBoxList as $key => $val)
{
    $status = ($val['isActive'] == 1) ? 'Active' : 'De-active';

    // $boxList[] = array($val['ID'],$val['heading'],$val['displayOrder'],$status);

    $boxList[$key]['ID'] = $val['ID'];
    $boxList[$key]['heading'] = $val['heading'];
    $boxList[$key]['displayOrder'] = $val['displayOrder'];
    $boxList[$key]['status'] = ($val['isActive'] == 1) ? 'Active' : 'De-active';
}

$json = json_encode($boxList);
echo $json;

AJAX

<script> 
$(document).ready(function(){
   var table = $('#example').DataTable({  

        "processing": true,
        "serverSide": true,
        "ajax": '<?=site_url()?>category/getDatatableData/<?=$activetab?>',
        "createdRow": function(row, data, dataIndex){
         $(row).attr('id', 'row-' + dataIndex);
        }

   });

   table.rowReordering();   
});
</script>

HTML

<table id="example" class="display" cellspacing="0" width="100%">
   <thead>
      <tr>
         <th>ID</th>
         <th>heading</th>
         <th>displayOrder</th>
         <th>status</th>
      </tr>
   </thead>
   <tfoot>
      <tr>
          <th>ID</th>
         <th>heading</th>
         <th>displayOrder</th>
         <th>status</th>
      </tr>
   </tfoot>
</table>

【问题讨论】:

  • 打印您的 JSON 并使用 this 检查它是否是有效的 JSON 格式
  • 我检查了我的 Json,它是有效的。谢谢!
  • 您忘记了 JSON 的标头:检查 here
  • 查看示例部分。绘制、记录总计等

标签: javascript php ajax datatable


【解决方案1】:

它显示 invalid JSON response 错误,因为您只将数据部分返回到数据表 ajax。 php 文件应返回以下格式的数据:

$jsonData = array(
      "draw" => intval($_REQUEST['draw']),
      "recordsTotal" => 10,            // you have to get this thorugh count(*) query 
      "recordsFiltered" => 8,          // recordsFiltered will get count of records when search input is filled,
       "data" => $boxList             // This is your data in array
);

echo json_encode($jsonData); 

当数据将以这种格式被echoed 时,您将不会遇到invalid Json response 错误。 jQuery 数据表在 json 数组中接受带有 drawrecordsTotalrecordsFiltereddata 的 json 响应。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-18
    • 2020-08-25
    • 1970-01-01
    • 2014-10-26
    • 2011-03-09
    • 2011-09-27
    • 1970-01-01
    相关资源
    最近更新 更多