簡體   English   中英

DataTable rowReordering問題PHP

[英]DataTable rowReordering issue PHP

我試過從數據庫表中檢索數據並顯示在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>

它顯示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); 

當將以這種格式echo數據時,您將不會遇到invalid Json response錯誤。 jQuery data通過json數組中的drawrecordsTotalrecordsFiltered data接受json響應。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM