如何使用Ajax Codigniter使用分页实现搜索



控制器

 function view_product() 
    {     
        $config = [
            'base_url'          =>  base_url('admin/view_product'),
            'per_page'          =>  10,
            'total_rows'        =>  $this->AdminModel->num_rows(NULL),
            'full_tag_open'     =>  "<ul class='pagination'>",
            'full_tag_close'    =>  "</ul>",
            'first_tag_open'    =>  '<li>',
            'first_tag_close'   =>  '</li>',
            'last_tag_open'     =>  '<li>',
            'last_tag_close'    =>  '</li>',
            'next_tag_open'     =>  '<li>',
            'next_tag_close'    =>  '</li>',
            'prev_tag_open'     =>  '<li>',
            'prev_tag_close'    =>  '</li>',
            'num_tag_open'      =>  '<li>',
            'num_tag_close'     =>  '</li>',
            'cur_tag_open'      =>  "<li class='active'><a>",
            'cur_tag_close'     =>  '</a></li>',
        ];
        $this->pagination->initialize($config);
        $data['page'] =($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data['product'] =$this->AdminModel->view_product(NULL,$config['per_page'],$data['page']);  
        $data['pagination'] = $this->pagination->create_links();
        $this->load->view('admin/view_product',$data);
    }
 public function ajaxsearch()
     {
          $query = $this->input->post('query');
          print_r($this->AdminModel->num_rows($query));         
          $config = [
            'base_url'          =>  base_url('admin/ajaxsearch'),
            'per_page'          =>  10,
            'total_rows'        =>  $this->AdminModel->num_rows($query),
            'full_tag_open'     =>  "<ul class='pagination'>",
            'full_tag_close'    =>  "</ul>",
            'first_tag_open'    =>  '<li>',
            'first_tag_close'   =>  '</li>',
            'last_tag_open'     =>  '<li>',
            'last_tag_close'    =>  '</li>',
            'next_tag_open'     =>  '<li>',
            'next_tag_close'    =>  '</li>',
            'prev_tag_open'     =>  '<li>',
            'prev_tag_close'    =>  '</li>',
            'num_tag_open'      =>  '<li>',
            'num_tag_close'     =>  '</li>',
            'cur_tag_open'      =>  "<li class='active'><a>",
            'cur_tag_close'     =>  '</a></li>',
        ];
        $this->pagination->initialize($config);
        $data['page'] =($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data['product'] =$this->AdminModel->view_product($query,$config['per_page'],$data['page']);
        $data['pagination'] = $this->pagination->create_links();
        $this->load->view('admin/product_search_result',$data,false);
     }

模型

public function num_rows($searchKeyword=NULL)
    {
        if ($searchKeyword == "NULL") $searchKeyword = "";
        $query = $this->db
                      ->select('*')
                      ->from('product')
                      ->like('p_name',$searchKeyword)
                      ->get();                  
        return $query->num_rows();
    }
 function view_product($searchKeyword=NULL,$limit,$offset)
    {
            if ($searchKeyword == "NULL") $searchKeyword = "";
            $query = $this->db->select("*")
                              ->from('product')
                              ->limit($limit, $offset )
                              ->join('product_category', 'product.p_cid = product_category.p_cid')
                              ->join('company','product.c_id = company.c_id')
                              ->order_by('p_id')
                              ->like('p_name',$searchKeyword)
                              ->get();
            return $query->result();
    }

查看

function load_data(query)
      {
            $.ajax({
                      url:"<?php echo base_url();?>admin/view_product",
                      method:"POST",
                      data:{query:query},
                      success:function(data)
                      { 
                          $('#showData').html(data);
                          $('#product_list').html(data);
                      }
                    });
      }
  $('#product_name').keyup(function()
      {
          var search = $(this).val();
          if(search != '')
          {
             load_data(search);
          }
        /*  else
          {
             load_data();
          }*/
      });

我是C.I的新手,我正在创建一个搜索过滤器,该过滤器正常工作,直到我移动到下一个分页链接为止。当我首先链接时,数据是根据过滤器/搜索关键字显示的,但是当我转到下一个链接时,所有内容都关闭(页面上显示了所有数据)如何解决此问题?

我建议您拥有一个函数以进行搜索&amp;在控制器中分页,在模型中具有两个独立的功能

function view_product() 
{     
    $config = [
        'base_url'          =>  base_url('admin/view_product'),
        'per_page'          =>  10,
        'total_rows'        =>  $this->AdminModel->num_rows(NULL),
        'full_tag_open'     =>  "<ul class='pagination'>",
        'full_tag_close'    =>  "</ul>",
        'first_tag_open'    =>  '<li>',
        'first_tag_close'   =>  '</li>',
        'last_tag_open'     =>  '<li>',
        'last_tag_close'    =>  '</li>',
        'next_tag_open'     =>  '<li>',
        'next_tag_close'    =>  '</li>',
        'prev_tag_open'     =>  '<li>',
        'prev_tag_close'    =>  '</li>',
        'num_tag_open'      =>  '<li>',
        'num_tag_close'     =>  '</li>',
        'cur_tag_open'      =>  "<li class='active'><a>",
        'cur_tag_close'     =>  '</a></li>',
    ];
    $this->pagination->initialize($config);
    $data['page'] =($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
    if(@$this->input->post('searchKeyword')==null)
    {
    $results=$this->AdminModel->view_product($config["per_page"],$page);    
    }
    else
    {
    $searchKeyword=$this->input->post('searchKeyword'); 
    $results=$this->AdminModel->view_product_withKey($config["per_page"],$page,$searchKeyword);
    }
    $data['product']=$results;
    $data['pagination'] = $this->pagination->create_links();
    $this->load->view('admin/view_product',$data);
}

模型

public function view_product($limit,$start)
{
    $this->db->from($this->table);
    $this->db->where('status',TRUE);
    $this->db->limit($limit, $start);
    $query = $this->db->get();
    return $query->result();
}
public function view_product_withKey($limit,$start,$searchKeyword)
{
    $this->db->from($this->table);
    $this->db->like('field1', $searchKeyword);
    $this->db->or_like('fiel2', $searchKeyword);
    $this->db->or_like('field3', $searchKeyword);
    $this->db->or_like('field4', $searchKeyword);
    $this->db->where('status',TRUE);
    $this->db->limit($limit, $start);
    $query = $this->db->get();
    return $query->result();
}

请测试代码。并考虑了变量名称。请分别使用您的变量。

最新更新