排序在分页的第 2 页不起作用


Sorting does not work on 2nd page of pagination

我试图在Codeigniter中使用分页列出。我愿意通过使用jquery ajax在列表中使用排序。它非常适合分页的第一页。我可以在该页面中进行升序和降序排序。但是当我点击其他页面时。它不起作用。

可能是什么问题。任何人都可以建议我吗?

提前谢谢。

这是我的控制器的代码

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

这是针对我的模型的

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

我已经使用阿贾克斯作为

<script type="text/javascript">

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

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

谢谢

分页在进行数据库调用时使用"限制"和"偏移量"参数。 如果您使用的是分页,则单击页面链接将传回偏移量。 如果到目前为止这对您有用,那么您需要做的就是确保将 Sort 参数保留在页面中的某个位置。

如果控制器方法正在检查 GET 变量以查找分页的偏移量,则需要确保该方法还知道要作为排序依据的字段。 我个人将其存储在闪存数据中。

因此,在您的控制器 methood 中,在数据库调用之前:

$SortColumn = $this->session->flashdata('SORT_BY');

如果那里没有保存 Sort,则返回 false,因此您可以在添加 $db->order_by() 方法时检查这一点。 如果那里有东西,那么您可以适当地排序。

然后在加载视图之前的方法底部,将"排序"设置回 flashdata,以便下次调用该方法(在下一页请求时)时,它具有"排序"字段以再次使用。

$this->session->set_flashdata('SORT', $SortColumn);

抱歉,我没有任何完整的代码示例,因为我使用 PHP-ActiveRecord 而不是 CI 内置的 activerecord 库。

你的函数应该看起来像这样

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

我可以建议您在ajax分页调用中传递排序参数。 这可能是问题所在。