【问题标题】:Datatable is not displaying on view page数据表未显示在视图页面上
【发布时间】:2019-04-14 05:57:21
【问题描述】:

我正在使用 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;

     }

【问题讨论】:

    标签: php ajax html datatable codeigniter-3


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的回答,我用我的代码更新了您的代码。现在我得到了 37 条记录,但我的视图仍然没有显示。我的意思是记录没有像数据表中的列表那样显示
    • 哦!等等,我认为我的结果有些问题。给我一些时间检查。
    • 您检查过您的控制台吗?通常你可以在浏览器->网络选项卡中查看返回数据,如果前端出现一些错误,你也可以在控制台选项卡上看到它们
    • @NigelRen,我知道这不是重定向任何成员的好方法,但相信我,我尝试了一切,但仍然没有找到解决方案。你能帮我这个链接stackoverflow.com/questions/63360592/…
    猜你喜欢
    • 2019-09-07
    • 2016-04-13
    • 1970-01-01
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 2019-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多