数据表未显示在视图页面上

时间:2018-11-11 06:47:25

标签: php ajax html5 datatable codeigniter-3

我正在使用CodeIgniter,Datatable无法正常工作。我正在显示数据表中的所有记录。但没有显示。我正在获得这样的视图页面。

enter image description here

我正在从模型中获取记录。该模型没有问题。让我知道是否需要模型代码。

您能在这个问题上帮助我吗?

视图

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

控制器

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;

     }

1 个答案:

答案 0 :(得分:0)

您将不断覆盖控制器中的同一变量以建立数据,因此将其更改为使用[]添加数据...

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