questionbank
questionbank

Reputation: 440

DataTables warning: invalid JSON response when there are no records in the table

I am using CodeIgniter with the bootstrap datatable to display the records from the database.

I am getting the error when there are no records are available in the table.

DataTables warning: table id=employee-List - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1

Records are displaying when available in the table.

I tried to find out the solution on SO but none of the solutions is working for me. Can aneone assist me what's the issue with my code?

I tried @BlueWater86 answer, the error goes off but my records are not displaying.

Would you help me out with this issue?

enter image description here

My code is,

Script

$('#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",
    "dataSrc": ""
  },
  "columns": [{
      "data": "id",
      "className": "reg_bg"
    },
    {
      "data": "name"
    },
    {
      "data": "email"
    },
    {
      "data": "mobileno"
    },
    {
      "data": "emp_id"
    },
    {
      "data": "address"
    },
    {
      "data": "action"
    }
  ]

});

Controller

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;
}

Model

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 0;
      }
    }

Upvotes: 1

Views: 405

Answers (1)

BlueWater86
BlueWater86

Reputation: 1817

In a statically typed language this sort of issue would not be encountered. If you consider the interface of the fetch_employeeList() function, it returns a dynamic; sometimes an array of objects and sometimes the number 0.

You should return an empty array instead of the number 0 in the case that there are no employee query results.

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 [];
      }
    }

Upvotes: 1

Related Questions