简体   繁体   English

CodeIgniter分页onclick

[英]CodeIgniter pagination onclick

Good day, Quick question, 美好的一天,快速提问,

I am using CI's pagination to my project, it works perfectly, but i wanted to add some filters on it. 我在项目中使用CI的分页,效果很好,但是我想在上面添加一些过滤器。

ie I need to catch its click event. 即我需要抓住它的点击事件。 although I can catch already its click event using this 尽管我已经可以使用它捕获其点击事件

 $("ul.pagination > li a[href]").click(function(e){
      loadingStart();
 });

my problem is it still redirect to the next page, 我的问题是它仍然重定向到下一页,

what I need is something like this. 我需要的是这样的东西。

 if(myVar == 0) {
      //do not redirect
 } else  {
      //redirect.
 }

Use e.preventDefault(); 使用e.preventDefault(); and then execute your custom function 然后执行您的自定义功能

If you want filter for pagination that use $_GET ... no redirect in js (You can not undo, etc.). 如果您想要使用$ _GET的分页过滤器... js中无重定向(您不能撤消等)。

I show you how I do: 我告诉你我怎么做:

Controller: 控制器:

/**
Example list
**/
public function index()
{
  $page = ($this->input->get('page') AND $this->input->get('page')>1)?(intval($this->input->get('page'))-1)*9:1; #9 item for page

  #where if isset($_GET)
  $where = array();
  if($this->input->get('city')) {
    $where['users.city'] = $this->input->get('city');
  }
  if($this->input->get('street')) {
    $where['users.street'] = $this->input->get('street');
  }
  #load model 
  $this->load->model('users_m');
  $view_data['users'] = $this->users_m->GetAll($where,$page,array('users.register_date'=>'DESC'))->result();
  //pagination library
  $this->load->library('pagination');
  $config['base_url'] = site_url('/users');
  $config['total_rows'] = $this->users_m->GetAll($where,0)->num_rows();
  $config['per_page'] = 9;
  $config['full_tag_open'] = "<ul class='pagination'>";
  $config['full_tag_close'] ="</ul>";
  $config['num_tag_open'] = '<li>';
  $config['num_tag_close'] = '</li>';
  $config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
  $config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
  $config['next_tag_open'] = "<li>";
  $config['next_tagl_close'] = "</li>";
  $config['prev_tag_open'] = "<li>";
  $config['prev_tagl_close'] = "</li>";
  $config['first_tag_open'] = "<li>";
  $config['first_tagl_close'] = "</li>";
  $config['last_tag_open'] = "<li>";
  $config['page_query_string'] = TRUE;
  $config['query_string_segment'] = 'page';
  $config['reuse_query_string'] = TRUE;
  $config['use_page_numbers'] = true;
  $config['last_tagl_close'] = "</li>";
  $this->pagination->initialize($config);
  //variable to template
  $view_data['pagination'] = $this->pagination;

  $this->load->view('/users/index',$view_data);
}

Model: users_m 型号:users_m

/**
* Get all users example
**/
public function GetAll($where=array(),$page=1,$order_by=array(),$limit=9)
{

  $this->db->select('users.*');
  #order by
  if(count($order_by)>0)
  {
    foreach($order_by as $key=>$value)
    {
      $this->db->order_by($key,$value);
    }
  }
  $this->db->where($where);
  #show all (for pagination calculate)
  if($page==0) return   $this->db->get_where('users',$where);
  #show single page
  return    $this->db->get_where('users',$where,$limit,(($page==1)?$page-1:$page));
}

And view: 并查看:

<div id="search_bar">
<?php echo form_open(site_url('/users'),array('class'=>'form-inline','method'=>'get'));?>
<div class="form-group col-lg-2 col-md-4 col-sm-4">
  <select name="type" class="form-control">
    <option value="" disabled="disabled" selected="selected" >City</option>
    <option value="1" <?php if(set_value('city')==1) echo 'selected';?>>London</option>
    <option value="2" <?php if(set_value('city')==2) echo 'selected';?>>Warsaw</option>
  </select>
</div>
<button type="submit" class="btn btn-primary" style="margin-top:-10px;">Search</button>
<?php echo form_close();?>
</div>
<hr/>
<div id="grid-user" class="row">
  <table>
    <tbody>
      <?php foreach($users as $user):?>
        <tr>
          <td><?php echo $user->id;?></td>
          <td><?php echo $user->firstname;?></td>
          <td><?php echo $user->lastname;?></td>
          <td><?php echo date("d.m.Y",strtotime($user->register_date));?></td>
        </tr>
      <?php endforeach;?>
    </tbody>
  </table>
</div>
<div class="row">
  <div class="col-lg-12">
    <div style="float:right;"><?php echo $pagination->create_links();?></div>
  </div>
</div>

I sorry for my english. 对不起,我的英语。 This is example (not checked, written from memory). 这是示例(未检查,是从内存写入的)。

Try it :) 试试吧 :)

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

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