首页 > 解决方案 > 数据表未显示在视图页面上

问题描述

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

在此处输入图像描述

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

你能帮我解决这个问题吗?

看法

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

     }

标签: phpajaxhtmldatatablecodeigniter-3

解决方案


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

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

推荐阅读