如何显示ajax分页数据而不刷新或重新加载codeigniter中的页面?

controller:test.php

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('index.php/').'test/'; $config['total_rows'] = $this->dependent_field->count_field_exam($field); $config['per_page'] = 10; $config['full_tag_open'] = '
    '; $config['full_tag_close'] = '
'; $config['cur_tag_open'] = '
  • '; $config['num_tag_open'] = '
  • '; $config['num_tag_close'] = '
  • '; $config['cur_tag_close'] = ''; $config['first_link'] = 'First'; $config['first_tag_open'] = '
  • '; $config['first_tag_close'] = '
  • '; $config['last_link'] = 'Next'; $config['last_tag_open'] = '
  • '; $config['last_tag_close'] = '
  • '; $config['next_link'] = FALSE; $config['next_tag_open'] = '
  • '; $config['next_tag_close'] = '
  • '; $config['prev_link'] = FALSE; $config['prev_tag_open'] = '
  • '; $config['prev_tag_close'] = '
  • '; $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); }

    观点:exam-colleges.php

     

    pagination->create_links(); ?>
    $(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'); } }); }); });

    我在codeigniter中创建ajax分页。 当我搜索结果数据完美显示每页限制10和分页也显示。 但问题是,当我点击分页时,它会更改url并显示任何内容,例如我的链接是:

     http://localhost/cs/index.php/test 

    点击分页按钮后,url链接变为

     http://localhost/cs/index.php/test/10 

    哪里什么都没显示。 那么,如何删除此问题并以分页方式显示数据? 请帮我。

    谢谢

    在你的控制器上为分页创建一个单独的函数并使用json

    在Ajax URL和分页URL上,请确保将其更改为您想要的只是下面的示例。

     load->view('common/example_view'); } public function pagination() { $json = array(); $this->load->library('pagination'); $config['base_url'] = base_url('example/pagination'); $config['total_rows'] = $this->pagination_total_rows(); $config['per_page'] = '2'; $config['uri_segment'] = 3; $config["use_page_numbers"] = TRUE; $config['num_links'] = "16"; $config['full_tag_open'] = "
      "; $config['full_tag_close'] = "
    "; $config['num_tag_open'] = '
  • '; $config['num_tag_close'] = '
  • '; $config['cur_tag_open'] = "
  • "; $config['cur_tag_close'] = "
  • "; $config['next_tag_open'] = "
  • "; $config['next_tagl_close'] = "
  • "; $config['prev_tag_open'] = "
  • "; $config['prev_tagl_close'] = "
  • "; $config['first_tag_open'] = "
  • "; $config['first_tagl_close'] = "
  • "; $config['last_tag_open'] = "
  • "; $config['last_tagl_close'] = "
  • "; $this->pagination->initialize($config); $page = $this->uri->segment(3); $start = ($page - 1) * $config["per_page"]; $json = array( 'pagination_links'=> $this->pagination->create_links(), 'pagination_results' => $this->pagination_data($config["per_page"], $start) ); $this->output ->set_content_type('application/json') ->set_output(json_encode($json)); } public function pagination_data($limit, $start) { $this->db->limit($limit, $start); $query = $this->db->get($this->db->dbprefix . 'movie'); return $query->result_array(); } public function pagination_total_rows() { $query = $this->db->get($this->db->dbprefix . 'movie'); return $query->num_rows(); } }

    然后在视图上使用ajax之类的

    view:example_view.php

     

    看来你在jQuery函数中有一个错误的选择器。 您应该将$('#body')用于