2017-07-19 35 views
0

コントローラ:test.phpをajaxページネーションがcodeigniterで機能していませんか?

<?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', 'captcha', 'email')); 
      $this->load->model('dependent_field'); 
      $this->load->library('curl'); 
     } 
     public function get_exam_college($offset=null) 
     { 

      $this->load->library('pagination'); 
      $config['base_url'] = base_url().'test/get_exam_college/'; 
      $config['total_rows'] = $this->dependent_field->count_field_exam_college($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); 

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

ビュー:試験-中心-colleges.php

<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> 
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script> 
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> 
<?php 
    foreach ($field as $fetch) 
    { 
?> 
    <div class="col-lg-12" id = "box" > 
     <div id="about"> 
      <ul id="list"> 
       <li><?php echo $fetch['college_name']; ?></li> 
       <li><?php echo $fetch['address']; ?></li> 
      </ul> 
     </div> 
    </div> 
<?php 
     echo $this->pagination->create_links(); 
    } 
?> 
<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); 
        $('#box').html($page_data.find('div#box')); 
        $('table').addClass('table'); 
       } 
      }); 
     }); 
    }); 
</script> 

モデル:このコードで

<?php 
    class Dependent_field extends CI_Model 
    { 
     function __construct() 
     { 
     parent::__construct(); 
     } 
     public function field_exam_college($field) 
     {  
     $this->db->select('college_name, field, city, state, address, website, courses'); 
     $this->db->from('all_colleges'); 
     $where = "field = '$field'"; 
     $this->db->where($where); 
     $query = $this->db->get(); 
     $result = $query->result_array(); 
     return $result; 
     } 

     public function count_field_exam_college($field) 
     { 
     $this->db->select('college_name, field, city, state, address, website, courses'); 
     $this->db->from('all_colleges'); 
     $where = "field = '$field'"; 
     $this->db->where($where); 
     $query = $this->db->get(); 
     $result = $query->count(); 
     return $result; 
     } 
    } 

dependent_field.php Iインデックスを持っています。 phpファイルを開き、submit-buttonをクリックするとexam-centers-colleges.phpファイルを呼び出しています。今、私はajaxページネーションを使用したいが、それでも動作しない理由と、どこで間違っているのか分からない。だから、どのように私はajaxページネーションを使用できますか?私を助けてください。

ありがとう

+0

こんにちは、なぜあなたはdataTypeを使用しないでください: 'json'または 'text'、ajaxはあなたがurlで設定したメソッドからのデータをエコーし​​なければなりません。 –

答えて

0

申し訳ありませんが、コードを変更する必要があります。

<?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', 'captcha', 'email')); 
      $this->load->model('dependent_field'); 
      $this->load->library('url'); 
     } 
    public function get_exam_college($offset=null) 
    { 

     $this->load->library('pagination'); 
     $config['base_url'] = base_url().'test/get_exam_college/'; 
     $config['total_rows'] = $this->dependent_field->count_field_exam_college($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); 

     $field = $this->input->post('field'); 
     $data['field'] = $this->dependent_field->field_exam_college($field, $config['per_page'],$offset); 
     $value['string'] = ''; 
     foreach($data['field'] as $token => $fetch) 
     { 
      $value['string'] .= ' 
        <div class="about"> 
         <ul id="list"> 
          <li>' . $fetch['college_name'] . '</li> 
          <li>' . $fetch['address'] . '</li> 
         </ul> 
        </div>'; 
     } 
     $value['pagination'] = $this->pagination->create_links(); 
     echo json_encode($value); 
    } 
} 

モデル:

<?php 
    class Dependent_field extends CI_Model 
    { 
     function __construct() 
     { 
     parent::__construct(); 
     } 
     public function field_exam_college($field) 
     {  
     $this->db->select('college_name, field, city, state, address, website, courses'); 
     $this->db->from('all_colleges'); 
     $this->db->where('field', $field); 
     $query = $this->db->get(); 
     $result = $query->result_array(); 
     return $result; 
     } 
     public function count_field_exam_college($field) 
     { 
     $this->db->select('college_name, field, city, state, address, website, courses'); 
     $this->db->from('all_colleges'); 
     $this->db->where('field', $field); 
     $query = $this->db->get(); 
     return $result->num_rows(); 
     } 
    } 

ビュー:

<div class="col-lg-12" id = "box" > 

    </div> 
    <div id="pagination"> 

    </div> 
<script type="text/javascript"> 
    $(function(){ 
     $('body').on('click','ul#search_page_pagination > li > a',function(event){ 
      e.preventDefault(); 
      var Pagination_url = $(this).attr('href'); 
      $.ajax({ 
       url:Pagination_url, 
       type:'POST', 
       dataType: 'json', 
       success: function(data) 
       { 
        $('#box').html(data.string); 
        $('#pagination').html(data.pagination); 
        $('table').addClass('table'); 
       } 
      }); 
     }); 
    }); 
</script> 

それが動作しない場合は、私を思い出してください。

関連する問題