0

I am using CodeIgniter, Datatable is not working. I am displaying all the records in the data table. but it's not displaying. I am getting my view page like this.

enter image description here

I am getting the records from my model. There is no issue with the model. Let me know if require the model code.

Would you help me out on this issue?

view

<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
    <table cellspacing="0" id="team_members_list">
      <thead>
        <tr>
          <th> Employee Name </th>
          <th> EMP ID</th>
          <th> Mobile No. </th>
          <th> Designation </th>
          <th> Role </th>
          <th> Status </th>
        </tr>
      </thead>
      <tbody>
      </tbody>
    </table>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>

js

$(document).ready(function() {
  var oTable = $('#team_members_list').DataTable({
    'responsive': true,
    //"processing": true,
    // "serverSide": true,
    "pageLength": 10,
    "ajax": {
      "url": baseUrl + "/Employee_control/team_members",
      "type": "POST"
    },
    "columns": [{
        "data": "name"
      },
      {
        "data": "employee_id"
      },
      {
        "data": "mobileno"
      },
      {
        "data": "emp_designation"
      },
      {
        "data": "emp_role_name"
      }
    ],
    'responsive': true
  });
});

Controller

public function team_members(){ 
    $draw = intval($this->input->get("draw"));
    $start = intval($this->input->get("start"));
    $length = intval($this->input->get("length"));
    $books = $this->Employee_model->getTotalList_of_TeamLeader();

    $data['draw'] = 1;
    $data['recordsTotal'] = count($books);
    $data['recordsFiltered'] = count($books);
//     // $n=1;
    foreach ($books as $key => $row) 
    {
        $arr_result = array(
              // "Sr.No" => $n,
                    "id" => base64_encode($this->encryption->encrypt($row->id)),
                    "name" => $row->firstname.' ' .$row->lastname,
                    "employee_id" => $row->employee_id,
                    "mobileno" => $row->mobileno,
                    "emp_designation" => $row->emp_designation,
                    "emp_role_name" => $row->emp_role_name,
                    "emp_teamLeader" => $row->team_leadername
        );

$data['data'] = $arr_result;
      }
    echo json_encode($data);
     exit;

     }
user9437856
  • 2,360
  • 2
  • 33
  • 92

1 Answers1

0

You are continually overwriting the same variable in your controller to build up the data, so change it to use [] to add the data...

$arr_result = [];    // Initialise the array
foreach ($books as $key => $row) 
{
    $arr_result[] = array(
          // "Sr.No" => $n,
                "id" => base64_encode($this->encryption->encrypt($row->id)),
                "name" => $row->firstname.' ' .$row->lastname,
                "employee_id" => $row->employee_id,
                "mobileno" => $row->mobileno,
                "emp_designation" => $row->emp_designation,
                "emp_role_name" => $row->emp_role_name,
                "emp_teamLeader" => $row->team_leadername
    );

  }
 // Add to main data after loop has finished
 $data['data'] = $arr_result;
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • Thanks for the answer,I updated your code with my code. Now i am getting 37 records but still my view not displaying. I mean records are not displaying like list in datatable – user9437856 Nov 11 '18 at 07:08
  • Oh! wait I think there is some issue from my end. give me some time to check. – user9437856 Nov 11 '18 at 07:19
  • Have you checked your console? Normally you can check returning data in Browser->Network tab , if some errors caused on front-end you can see them on console tab as well – Ramin Nov 11 '18 at 08:48
  • @NigelRen, I know it's not a good way to redirect any member but trust me I tried everything from my side but still not found the solution. Can you help me in this link https://stackoverflow.com/questions/63360592/search-and-pagination-not-working-while-using-server-side – user9437856 Aug 12 '20 at 15:21