問題の説明
同じビュー(2つの異なるセクション)に表示する必要がある親/子テーブルがある状況があります。各セクションには、独自のページ付けリンクのセットが必要です。セクション2の内容(したがってページ付けリンク)は、親セクション/セクション1で選択した内容に応じて変更する必要があります。
具体的には、カテゴリテーブルがあり、カテゴリごとに製品があります。ページの上部にカテゴリのリストを表示したいのですが、下部に選択したカテゴリの製品がある場合はそれが表示されます。
最初の試み:
私はこの問題を2つの異なる方法で解決しようとしました。初めてコードを記述したとき、ユーザーが選択したIDに基づいてすべてのサブカテゴリを検索する1つのメソッドがコントローラーにありました。次のようになりました。
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; ?>
これは、実際には1つのページネーションオブジェクトしか作成していないことに気付いたため、機能しませんでした。データセットごとに1つずつ、合計2つのコードセットがありましたが、最初のページネーションオブジェクトを、宣言した2番目のページネーションオブジェクトで上書きしていました(私は思います)。いずれにせよ、システムがビュー上の2つのデータセットを非表示にする場合、製品のページングリンクをクリックすると、カテゴリのページングがトリガーされます。何をしても、商品をめくることはできませんでした。
2回目の試行
私はajaxを使おうと決心しました。したがって、私のコントローラーでは、次のように、機能を分割してカテゴリと製品を2つの方法で検索します。
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;
}
悲しいことに、私はまだ同じ結果を持っています。システムは2セットのリンクを表示しますが、それらはカテゴリにのみ関連付けられているようです。製品のページ付けリンクを使用して製品をページングできません。
誰かがこれができるかどうか教えてもらえますか...もしそうなら、私はどこが間違っているのですか?
ありがとう。