Reputation: 11
I was trying to list using pagination in Codeigniter. I was willing to use sorting in the list by using jquery ajax. It works perfectly for first page of pagination. I could do ascending and descending sorting in that page. But when i click on other page. It does not work.
What could be the problem. Can any one suggest me??
Thanks in advance.
This is the code for my controller
function listData(){
$this->clear_sample_id_catche();
$out=$this->Mgeneral->listData();
$per_page = 10;
$total = $this->Mgeneral->countData();
$data = $this->Mgeneral->listData($per_page, $this->uri->segment(3));
$base_url = site_url('general/listData');
$config['base_url'] = $base_url;
$config['total_rows'] = $total;
$config['per_page'] = $per_page;
$config['uri_segment'] = '3';
$this->pagination->initialize($config);
$data=array('body'=>'list','out'=>$data);
$this->load->view('template',$data);
}
This is for my model
function listData($limit = NULL, $offset = NULL)
{
$this->db->limit($limit, $offset);
$data=array();
$q=$this->db->get('tbl_sample_id');
if($q->num_rows()>0)
{ $data=$q->result_array();
return $data;
}
}
and i have use ajax as
<script type="text/javascript">
$(document).ready(function(){ $("#internal").click(function(){
$.ajax({url:"general/listData_internal_ascending",success:function(result){
$("body").html(result);
}});
});
});
Thank you
Upvotes: 0
Views: 853
Reputation: 19882
You function should look like this
function listData($limit = NULL, $offset = NULL)
{
$this->db->limit($limit, $offset);
$this->db->order_by('id');
$q = $this->db->get('tbl_sample_id');
if($q->num_rows()>0)
{
return $q->result_array();
}
}
Upvotes: 0
Reputation: 838
I can suggest you to pass sorting parameters in your ajax call of pagination . that could be the problem.
Upvotes: 0
Reputation: 2042
Pagination uses the Limit and the Offset parameters when making the database calls. If you're using pagination, then clicking the page links will pass back the Offset. If this is working for you so far, then all you need to do is make sure that you keep the Sort parameter somewhere in your page.
If your controller method is checking for the GET variable to find the offset for the pagination, you need to make sure that the method also knows which field to sort by. I personally store it in flashdata.
So in your controller methood, before the db call:
$SortColumn = $this->session->flashdata('SORT_BY');
If there's no Sort saved there, then it returns false, so you can check for that when adding your $db->order_by() method. If there is something there, then you can sort appropriately.
Then right at the bottom of the method before the view gets loaded, set the Sort back to flashdata so the next time the method is called (on next page request), it has the Sort field to work with again.
$this->session->set_flashdata('SORT', $SortColumn);
Sorry I don't have any full code examples, as I use PHP-ActiveRecord instead of the CI built-in activerecord library.
Upvotes: 1