繁体   English   中英

使用 Jquery 获取服务器端分页

[英]Getting Server-side pagination using Jquery

目前,我的视图类中有一个表,它是从我的数据库中填充的。 为了填充这些数据,我有一个控制器类来获取所有数据,然后,我在视图类中包含以下代码以显示我的所有数据:

<table id="user_data" class="table table-bordered table-striped">  
                     <thead>  
                          <tr>  
                               <th width="35%">First Name</th>  
                               <th width="35%">Last Name</th>  
                          </tr>  
                     </thead>  
                </table> 
<script type="text/javascript" language="javascript" >  
 $(document).ready(function(){  
      var dataTable = $('#user_data').DataTable({  
           "processing":true,  
           "serverSide":true,  
           "order":[],  
           "ajax":{  
                url:"<?php echo base_url() . 'contacts/fetch_user'; ?>",  
                type:"GET"  
           },  
           "columnDefs":[  
                {  
                     "targets":[0, 3, 4],  
                     "orderable":false,  
                },  
           ],  
      });  
 });  
 </script> 

控制器类:

function fetch_user(){  
        $this->load->model("contacts_model");  
        $fetch_data = $this->contacts_model->make_datatables();  
        $data = array();  
        foreach($fetch_data as $row)  
        {  
             $sub_array = array();  
             $sub_array[] = $row->firstname;  
             $sub_array[] = $row->lastname;  
             $data[] = $sub_array;  
        }  
        $output = array(  
             "draw"                    =>     intval($_GET["draw"]),  
             "recordsTotal"          =>      $this->contacts_model->get_all_data(),  
             "recordsFiltered"     =>     $this->contacts_model->get_filtered_data(),  
             "data"                    =>     $data  
        );  
        echo json_encode($output);  
        $this->load->view('crm/contacts/test',$data);
   }  

型号分类:

function make_query()  
      {  
           $this->db->select("*");  
           $this->db->from("crm_contacts");  
           if(isset($_GET["order"]))  
           {  
                $this->db->order_by($this->order_column[$_GET['order']['0']['column']], $_GET['order']['0']['dir']);  
           }  
           else  
           {  
                $this->db->order_by('id', 'DESC');  
           }  
      }  
      function make_datatables(){  
           $this->make_query();  
           if($_GET["length"] != -1)  
           {  
                $this->db->limit($_GET['length'], $_GET['start']);  
           }  
           $query = $this->db->get();  
           return $query->result();  
      }  
      function get_filtered_data(){  
           $this->make_query();  
           $query = $this->db->get();  
           return $query->num_rows();  
      }       
      function get_all_data()  
      {  
           $this->db->select("*");  
           $this->db->from("crm_contacts");  
           return $this->db->count_all_results();  
      }  

现在每当我通过这个 URL http://localhost/contacts/fetch_user?length=10&start=10&draw=9 ,我的输出看起来像这样:

在此处输入图片说明

它在顶部将所有值显示为 JSON,而不是在我的实际表中,它只显示 Processing。

在ajax URL中发送POST请求,更多详情请查看数据表手动链接中的服务器端处理: https : //datatables.net/manual/server-side

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM