控制器
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的新手,我正在创建一个搜索过滤器,过滤器正常工作,直到我移动到下一个Pagination链接。当我在第一个链接时,数据显示根据过滤器/搜索关键字,但是当我移动到下一个链接时,一切都会消失(所有数据显示在页面上)如何解决这个问题?
答案 0 :(得分:1)
我建议你有一个搜索和功能的功能;在CONTROLLER中分页并在MODEL中有两个独立的功能
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();
}
请测试代码。并考虑变量名称。请分别使用你的变量。