ajax分页在codeigniter中不起作用?

时间:2017-07-19 05:27:43

标签: php codeigniter

controller:test.php

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

观点:exam-centers-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>

model:dependent_field.php

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

在这段代码中,我有一个index.php文件,当我点击提交按钮时,我正在调用exam-centers-colleges.php文件。现在,我想使用ajax分页但仍然无法正常工作我不知道为什么以及我在哪里做错了。那么,我怎样才能使用ajax分页?请帮助我。

感谢

1 个答案:

答案 0 :(得分: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>

如果它不起作用,请提醒我。