2017-08-08 117 views
0

控制器:test.php的如何在codeigniter中實現ajax分頁?

<?php 
    defined('BASEPATH') OR exit('No direct script access allowed'); 
    class Test extends CI_Controller 
    { 
     function __construct() 
     { 
      parent :: __construct(); 
      $this->load->helper(array('form', 'url')); 
      $this->load->model('dependent_field'); 
     } 
     public function index() 
     { 
      $this->load->view('index');   
     } 

     public function get_exam_college($offset=null) 
     { 
      $this->load->library('table'); 
      $this->load->library('pagination'); 

      $field=$this->input->post('field'); 

      $config['base_url'] = base_url().'test/'; 
      $config['total_rows'] = $this->dependent_field->count_field_exam($field); 
      $config['per_page'] = 10; 
      $config['full_tag_open'] = '<ul class="pagination" id="search_page_pagination">'; 
      $config['full_tag_close'] = '</ul>'; 
      $config['cur_tag_open'] = '<li class="active"><a href="javascript:void(0)">'; 
      $config['num_tag_open'] = '<li>'; 
      $config['num_tag_close'] = '</li>'; 
      $config['cur_tag_close'] = '</a></li>'; 
      $config['first_link'] = 'First'; 
      $config['first_tag_open'] = '<li>'; 
      $config['first_tag_close'] = '</li>'; 
      $config['last_link'] = 'Last'; 
      $config['last_tag_open'] = '<li>'; 
      $config['last_tag_close'] = '</li>'; 
      $config['next_link'] = FALSE; 
      $config['next_tag_open'] = '<li>'; 
      $config['next_tag_close'] = '</li>'; 
      $config['prev_link'] = FALSE; 
      $config['prev_tag_open'] = '<li>'; 
      $config['prev_tag_close'] = '</li>'; 
      $config['page_query_string'] = FALSE; 
      $this->pagination->initialize($config); 

      $data['field'] = $this->dependent_field->field_exam_college($field,$config['per_page'],$offset); 
      $this->load->view('exam-colleges',$data); 
     } 
    } 

視圖:考試-colleges.php

<?php 
    foreach ($field as $fetch) 
    { 
?> 
    <p><?php echo $fetch['college_name']; ?></p> 
<?php 
    } 
?> 
<div id="body"> 
    <?php 
     echo $this->pagination->create_links(); 
    ?> 
</div> 

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script> 
<script type="text/javascript"> 
    $(function(){ 
     $('body').on('click','ul#search_page_pagination>li>a',function(e){ 
     e.preventDefault(); 
     var Pagination_url = $(this).attr('href'); 
      $.ajax({ 
       url:Pagination_url, 
       type:'POST', 
       success:function(data){ 
        var $page_data = $(data); 
        $('#container').html($page_data.find('div#body')); 
        $('table').addClass('table'); 
       } 
      }); 
     }); 
    }); 
</script> 

模型:Dependent_field.php

<?php 
    class Dependent_field extends CI_Model 
    { 
     function __construct() 
     { 
      parent::__construct(); 

     public function count_field_exam($field) 
     { 
      $this->db->select('*'); 
      $this->db->from('all_colleges'); 
      $where = "field = '$field'"; 
      $this->db->where($where); 
      $query = $this->db->get(); 
      return $query->num_rows(); 
     } 

     public function field_exam_college($field,,$limit,$start) 
     {  
      $this->db->select('*'); 
      $this->db->from('all_colleges'); 
      $where = "field = '$field'"; 
      $this->db->where($where); 
      $this->db->limit($limit,$start); 
      $query = $this->db->get(); 
      $result = $query->result_array(); 
      return $result; 
     } 
    } 

我在測試控制器的索引文件,其中我當我點擊提交按鈕時,提交按鈕,然後結果顯示從考試students.php文件和分頁也顯示,但當我點擊分頁按鈕,然後頁面重新加載,我不想要。那麼,如何在codeigniter中使用ajax分頁?請幫助我。

謝謝

回答

0

請注意一兩件事,分頁作品的使用方法=「GET」。

對AJAX

在View:

<input type="hidden" name="page" id="page" value="10"> // initially it contain the value which is equal to config['per_page'] 

AJAX請求:

    $.ajax({ 
         url:'', // your url 
         type:'GET', 
         dataType: 'json', // Im using json as type in ajax request 
         data:{ 
          page :$('#page').val(); 
          //you can add more data here for your get request 
         }, 
         success :function(data) 
          $('#page').val(data.offset); //upadte page by new offset from the controller 

         // append or add the result data to the page 
         }, 
         error: function() { 
          alert("ERROR"); 
         } 

        }) 

在控制器

$page =$this->input->get('page'); 
//pagination configs here 
$data['offset'] = $page + 10; //10 is $config['per_page'] 
$data['result'] = $this->YourModel->yourFunction($config["per_page"], $offset); 
echo json_encode($data); // Im using json as type in ajax request 
在模型

public function yourFunction($limit,$start) 
{ 
    $this->db->select('*'); 
    $this->db->limit($limit,$start); 
    $query = $this->db->get('table_name'); 
    return $query->result(); 
} 
+0

請@Visakh重溫我的編輯代碼。 – omkara

+0

請仔細閱讀我寫的@omkara的全部代碼...只需要用您的變量和函數名稱替換...也請閱讀註釋 –