简体   繁体   中英

Getting Server-side pagination using Jquery

Currently, I have a table in my view class that is getting populated from my database. To populate this data I have a controller class that is fetching all the data and after that, I'm including the following code in my view class to show all my data:

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

Controller class:

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

Model Class:

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

Now whenever I pass this URL http://localhost/contacts/fetch_user?length=10&start=10&draw=9 , my output look like this:

在此处输入图片说明

Where its showing all the values as a JSON at the top and not in my actual table where it just shows Processing.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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