DataTables警告:表中没有记录时,JSON响应无效。还添加了空数组,但记录未显示

时间:2019-06-06 02:49:41

标签: javascript jquery html datatables codeigniter-3

我正在将CodeIgniter与引导数据表一起使用,以显示数据库中的记录。

当表中没有可用记录时,我会收到错误消息。

  

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

在表中可用时显示记录。

注意:如果我在模型中添加了一个值为0的空数组,那么我的错误就消失了,但是没有从数据库中显示记录。)

我试图找出关于SO的解决方案,但是没有一个解决方案对我有用。谁能帮我解决我的代码有什么问题?

enter image description here

我的代码是

脚本

$('#employee-List').DataTable({
  language: {
    sLengthMenu: "Show _MENU_", // remove entries text
    searchPlaceholder: "Search",
    emptyTable: "No record found",
    search: ""
  },
  "ordering": false, // remove sorting effect from header 
  "processing": true,
  "serverSide": true,
  "scrollX": true,
  "bInfo": false,
  "pageLength": 10,
  "ajax": {
    "url": baseUrl + "/Employee_control/fetch_employeeList",
    "type": "POST"
  },
  "columns": [{
      "data": "id",
      "className": "reg_bg"
    },
    {
      "data": "name"
    },
    {
      "data": "email"
    },
    {
      "data": "mobileno"
    },
    {
      "data": "emp_id"
    },
    {
      "data": "address"
    },
    {
      "data": "action"
    }
  ]

});

控制器

public function fetch_employeeList(){
  $order_list=$this->Employee_model->fetch_employeeList();

// Datatables Variables
$draw = intval($this->input->get("draw"));
$start = intval($this->input->get("start"));
$length = intval($this->input->get("length"));

$data['draw'] = 1;
$data['recordsTotal'] = count($order_list);
$data['recordsFiltered'] = count($order_list);
foreach ($order_list as $key => $row) 
{
$action='<a href="" class="action-btn action-btn-border">View</a><a href="" class="action-btn action-btn-red-bg">Archive</a>';
$arr_result = array(
            "id" =>$row->id,
            "name" => $row->firstname." ".$row->middlename." ".$row->lastname,
            "email" => $row->email_id,
            "mobileno" => $row->mobileno,
            "emp_id" => $row->employee_id,
            "address" => $row->address,
            "action" => $action
);

$data['data'][] = $arr_result;

}
//print_r($arr_result);
echo json_encode($data);
exit;
}

模型

public function fetch_employeeList(){
        $this->db->select('*');
      $this->db->from('tbl_employee');

        $query = $this->db->get();
        $result = $query->result();
        if($result)
      {
          return $result;
      }
      else{
          return []; //I added empty array instated of 0. Using this my error goes of but records are not displaying from the database.
      }
    }

1 个答案:

答案 0 :(得分:0)

问题在于,控制器返回的JSON响应必须始终包含一个名为data的属性,并且在控制器中编写代码的方式仅是将data属性设置为响应数组(如果有结果)。

在您的控制器中,您需要将$data['data']默认设置为一个空数组,如下所示:

$data['draw'] = 1;
$data['recordsTotal'] = count($order_list);
$data['recordsFiltered'] = count($order_list);
$data['data'] = []; // add this line in controller

有关更多信息,请参见DataTables manual on server side processing(向下滚动至“返回的数据”部分)

更新

在模型方法中,应该始终返回一个数组,而不要在没有结果时返回0。查看codeigniter的文档,如果没有结果,$query->getResult()将返回一个空数组,因此您可以将模型方法更新为:

public function fetch_employeeList(){
    $this->db->select('*');
    $this->db->from('tbl_employee');

    $query = $this->db->get();
    return $query->result();
}