问题描述
我有一种情况,我必须在同一个视图上显示父/子表 - 在两个不同的部分中。每个部分都需要自己的一组分页链接。第 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;
}
可悲的是,我仍然有相同的结果。系统显示两组链接,但它们似乎只与类别相关联。我无法使用产品分页链接对产品进行分页。
有人可以告诉我这是否可以做到......如果可以,我哪里出错了?
谢谢。