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'));
$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);
}
}
观点:exam-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>
model: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;
}
}
我在测试控制器中有一个索引文件,当我点击提交按钮时我有一个下拉提交按钮然后结果显示来自exam-colleges.php文件并且分页也显示但是当我点击分页按钮时然后页面重新加载,我不想要。那么,我如何在codeigniter中使用ajax分页?请帮助我。
谢谢
答案 0 :(得分:0)
请注意分页使用method =&#34; GET&#34;。
对于ajax
在视图中:
<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();
}