简体   繁体   English

分页的第二页上无法排序

[英]Sorting does not work on 2nd page of pagination

I was trying to list using pagination in Codeigniter. 我试图在Codeigniter中使用分页列出。 I was willing to use sorting in the list by using jquery ajax. 我愿意通过使用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 我已经使用ajax作为

<script type="text/javascript">

$(document).ready(function(){ $("#internal").click(function(){ $(document).ready(function(){$(“#internal”)。click(function(){

$.ajax({url:"general/listData_internal_ascending",success:function(result){
  $("body").html(result);

    }});
  });
});

Thank you 谢谢

Pagination uses the Limit and the Offset parameters when making the database calls. 在进行数据库调用时,分页使用Limit和Offset参数。 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. 如果到目前为止这对您仍然有效,那么您要做的就是确保将Sort参数保留在页面中的某个位置。

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. 如果您的控制器方法正在检查GET变量以查找分页的偏移量,则需要确保该方法也知道要按哪个字段排序。 I personally store it in flashdata. 我个人将其存储在flashdata中。

So in your controller methood, before the db call: 因此,在您的控制器环境中,在db调用之前:

$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. 如果没有在其中保存排序,则它返回false,因此您可以在添加$ db-> order_by()方法时进行检查。 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. 然后,在加载视图之前,在方法的底部,将“排序”设置回flashdata,以便下次调用该方法时(在下一页请求时),它具有“排序”字段可再次使用。

$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. 抱歉,我没有完整的代码示例,因为我使用PHP-ActiveRecord而不是CI内置的activerecord库。

I can suggest you to pass sorting parameters in your ajax call of pagination . 我建议您在ajax分页调用中传递排序参数。 that could be the problem. 那可能是问题所在。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM