DataIgniter中的DataTables服务器端处理,但表中未显示数据

时间:2019-06-12 19:54:39

标签: codeigniter datatables

我可以打印出数据,但是数据没有显示在表格中。
我正在尝试使用数据表来显示数据。

以下是错误消息:

  

DataTables警告:表格ID =示例-无效的JSON响应。有关此错误的更多信息,请参见http://datatables.net/tn/1

enter image description here

模型

function allposts_count()
{   
    $query = $this
            ->db
            ->get('books');

    return $query->num_rows();  

}

function allposts($limit,$start,$col,$dir)
{   
    $query = $this
            ->db
            ->limit($limit,$start)
            ->order_by($col,$dir)
            ->get('books');

    if($query->num_rows()>0)
    {
        return $query->result(); 
    }
    else
    {
        return null;
    }

}

控制器

function Book()
{
    $columns = array( 
                        0 =>'id', 
                        1 =>'title',

                    );

    $limit = $this->input->post('length');
    $start = $this->input->post('start');

    $columns=$this->input->post('order')[0]['column'];
    $order = $columns;
    $dir = $this->input->post('order')[0]['dir'];

    $totalData = $this->user_model->allposts_count();

    $totalFiltered = $totalData; 

    if(empty($this->input->post('search')['value']))
    {            
        $posts = $this->user_model->allposts($limit,$start,$order,$dir);
    }
    else {
        $search = $this->input->post('search')['value']; 

        $posts =  $this->user_model->posts_search($limit,$start,$search,$order,$dir);

        $totalFiltered = $this->user_model->posts_search_count($search);
    }

    $data = array();
    if(!empty($posts))
    {
        foreach ($posts as $post)
        {
            $nestedData['id'] = $post->book_id;
            $nestedData['title'] = $post->book_title;

            $data[] = $nestedData;
        }
    }

    $json_data = array(
                "draw"            => intval($this->input->post('draw')),  
                "recordsTotal"    => intval($totalData),  
                "recordsFiltered" => intval($totalFiltered), 
                "data"            => $data   
                );

    $json = json_encode($json_data); 
    echo $json;
    $this->load->view('templates/header');
    $this->load->view('users/Book',$json);
    $this->load->view('templates/footer');
}

视图

<div class="container">
    <h1>Server Side Process Datatable</h1>
    <table id="example" class="display" cellspacing="0" width="100%">
        <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>

        </tr>
        </thead>
        <tfoot>
        <tr>
            <th>ID</th>
            <th>Name</th>

        </tr>
        </tfoot>
    </table>

    <!--create modal dialog for display detail info for edit on button cell click-->
    <div class="modal fade" id="myModal" role="dialog">
        <div class="modal-dialog">
            <div id="content-data"></div>
        </div>
    </div>
</div>


<script>
    $(document).ready(function(){
        var dataTable=$('#example').DataTable({
            "processing": true,
            "serverSide":true,
            "ajax":{
             "url": "<?php echo base_url('users/Book') ?>",
         "dataType": "json",
         "type": "POST",
         "data":{  '<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>' }
                       },
               "columns": [
              { "data": "book_id" },
              { "data": "book_title" },

           ]     
        });
    });
</script>

3 个答案:

答案 0 :(得分:0)

datatables需要一个纯json响应。如果您按照发布的链接中的调试步骤进行操作,您将看到您的响应还包含一个视图。

在这里有json,然后有一个视图:

$json = json_encode($json_data); 
      echo $json;
      $this->load->view('templates/header');
      $this->load->view('users/Book',$json);
      $this->load->view('templates/footer');

删除视图和/或将json内容移动到另一个URL。然后将"url": "<?php echo base_url('users/Book') ?>",更新为新的网址。

答案 1 :(得分:0)

您应该回显$ data以在表#example中显示数据。

答案 2 :(得分:0)

 <table id="example" cellpadding="0" cellspacing="0">
  <thead><tr>
    <th>ID</th>
    <th>Name</th>
  </tr></thead>

  <tbody>
    <!-- rows will go here -->
  </tbody>
</table> 

在javascript中

$("#example tbody").html(someHtmlString);

您无需在代码中放入返回查询。 然后调试以检查数据是否有效。