问题说明
我有一种情况,我有父/子表,我必须在同一个视图上显示 - 两个不同 部分。每个部分都需要自己的一组分页链接。第2部分中的内容(以及分页链接)必须根据父部分/部分1中选择的内容进行更改 具体来说,我有一个类别表,对于每个类别,我都有产品。我想在页面顶部显示一个类别列表,底部将显示所选类别中的产品(如果有)。
首次尝试:
我试图以两种不同的方式解决这个问题。我第一次编写代码时,控制器中有一个方法可以根据用户选择的id查找所有子类别。它看起来像这样:
public function browsecategory($category_id)
{
//find all products in category.
$this->load->model('category/product_category_model');
//pagination for products
$this->load->library('pagination');
$config['base_url'] = site_url('/product/browsecategory/'.$category_id);
$config['uri_segment'] = 4;
$config['total_rows'] = count($productrecords);
$config['per_page'] = 10;
$config['num_links'] = 10;
$this->pagination->initialize($config);
$offset = ($this->uri->segment($config['uri_segment'])) ? $this->uri->segment($config['uri_segment']) : 0;
$productrecords = $this->product_model->limit($config['per_page'], $offset)->find_all();
//find any subcategories for this category
$this->load->model('category/category_model');
//pagination for subcategories
$this->load->library('pagination');
$config['base_url'] = site_url('/product/browsecategory/'.$category_id);
$config['uri_segment'] = 5;
$config['total_rows'] = count($this->category_model->find_all_by('parent_id', $category_id););
$config['per_page'] = 10;
$config['num_links'] = 10;
$this->pagination->initialize($config);
$offset = ($this->uri->segment($config['uri_segment'])) ?
$this->uri->segment($config['uri_segment']) : 0;
$subcategoriesrecords = $this->category_model->limit($config['per_page'], $offset)->find_all_by('parent_id', $category_id);
Template::set('categorydetails', $categorydetails);
Template::set('productrecords', $productrecords);
Template::set('subcategoriesrecords', $subcategoriesrecords);
Template::render();
}//end browsecategory
视图看起来像这样:
<?php if (isset($subcategoriesrecords) && is_array($subcategoriesrecords) && count($subcategoriesrecords)) : ?>
<?php echo $this->pagination->create_links(); ?>
<div>
<h1 class="page-header">Sub-categories in <i> <?php echo $categorydetails->title; ?></i>:</h1>
</div>
<br />
<table class="table table-striped table-bordered">
</table>
<?php endif; ?>
<?php if (isset($productrecords) && is_array($productrecords) && count($productrecords)) : ?>
<?php echo $this->pagination->create_links(); ?>
<div>
<h1 class="page-header">Products in category <i> <?php echo $categorydetails->title; ?></i>:</h1>
</div>
<br />
<?php endif; ?>
这不起作用,因为我意识到我只是创建了一个分页对象。虽然我有两组代码,每个数据集一个代码,但我用我声明的第二个分页覆盖(我认为)第一个分页对象。 在任何情况下,当系统在视图上显示两组数据时,单击产品的分页链接将触发对类别的分页。无论我做了什么,我都无法浏览产品。
第二次尝试
我决定尝试使用ajax。因此,在我的控制器中,我将功能分解为两种方法,如下所示:
public function browsecategory($category_id)
{
//find any subcategories for this category
$this->load->model('category/category_model');
$this->load->model('category/product_category_model');
$subcategoriesrecords = $this->category_model->find_all_by('parent_id', $category_id);
//look up category information ... used for display purposes.
$categorydetails = $this->category_model->find_by('category_id', $category_id);
//pagination for subcategories
$this->load->library('pagination');
$config['base_url'] = site_url('/product/browsecategory/'.$category_id);
$config['uri_segment'] = 4;
$config['total_rows'] = count($subcategoriesrecords);
$config['per_page'] = 5;
$config['num_links'] = 10;
$this->pagination->initialize($config);
$offset = ($this->uri->segment($config['uri_segment'])) ? $this->uri->segment($config['uri_segment']) : 0;
$subcategoriesrecords = $this->category_model->limit($config['per_page'], $offset)->find_all_by('parent_id',$category_id);
//check for any products within this current category.. if exists, include ajax to display
if ( count ($this->product_category_model->find_all_by('category_id', $category_id)) > 0 ) {
//add jquery logic to call getproductsincategory on document load
$inline = "$.ajax({
url:'http://myserver/myapp/product/getproductsincategory/".$category_id."',
type:'POST',
dataType:'html',
contentType : 'text/html',
success: function(returnDataFromController) {
$('#products').html(returnDataFromController);
},
error: function(jqXHR, textStatus, errorThrown)
{
alert(errorThrown);
}
});
";
Assets::add_js( $inline, 'inline' );
}
Template::set('categorydetails', $categorydetails);
Template::set('subcategoriesrecords', $subcategoriesrecords);
Template::render();
}//end browsecategory
public function getproductsincategory($category_id)
{
//find all products in category.
$this->load->model('category/product_category_model');
$this->load->model('category/category_model');
//pagination for prodcuts
$this->load->library('pagination');
$config['base_url'] = site_url('/product/browsecategory/'.$category_id);
$config['uri_segment'] = 5;
$config['total_rows'] = count($productrecords);
$config['per_page'] = 10;
$config['num_links'] = 10;
$this->pagination->initialize($config);
$offset = ($this->uri->segment($config['uri_segment'])) ? $this->uri->segment($config['uri_segment']) : 0;
$productrecords = $this->product_model->limit($config['per_page'], $offset)->find_all_by('category_id',$category_id);
//build html string for displaying products
print $htmlstring;
}
可悲的是,我仍然有相同的结果。系统显示两组链接,但它们似乎只与类别相关联。我无法使用产品分页链接浏览产品。
有人可以告诉我是否可以这样做......如果是的话,我哪里出错了?
感谢。
答案 0 :(得分:1)
结帐https://github.com/EllisLab/CodeIgniter/wiki/AJAX-Pagination-with-CI-Pagination-Library此处还有一些关于如何使用它的教程https://www.google.com/search?q=codeigniter+pagination+ajax
我认为主要问题是生成的分页链接只是下一个/上一个/第一个/最后一个结果的href。您将希望覆盖第二组分页链接,以便它们使用新数据请求并重新填充第二个窗格。