2015-03-18 61 views
0

我發現這個Codeigniter jquery分頁庫https://github.com/bcit-ci/CodeIgniter/wiki/AJAX-Pagination-with-CI-Pagination-Library,我有一個問題,使用它。當我點擊分頁鏈接它將整個網站(所有視圖)加載到目標div。這裏是我的設置:Codeigniter jquery分頁庫

控制器/ categories.php

<?php 
class Categories extends CI_Controller{ 

    function __construct(){ 
     parent::__construct(); 
      $this->load->model('business_model'); 
      $this->load->model('catalogue_model'); 
      $this->load->model('basket_model'); 
      $this->load->library('Jquery_pagination'); 
    } 


    function category($slug){ 
     $data['price']="&pound;"; 

     $numitem['shopname']=$this->business_model->getShop(); 
     $numitem['slogan']=$this->business_model->getSlogan(); 
    $numitem['metacats']=$this->catalogue_model->cat_name($slug); 
    $kitties['cats']=$this->catalogue_model->get_categories(); 

    $kitties['msgcart']=$this->basket_model->number_of_items; 
     $kitties['subt']=$this->basket_model->_sub_total; 
     $kitties['vat']=$this->basket_model->_vat; 
     $kitties['total']=$this->basket_model->_total; 
     $kitties['vatrate']=$this->basket_model->_vat_rate; 
$start = ($this->uri->segment(4) > 0)?$this->uri->segment(4):0; 

      $num=$this->catalogue_model->get_products_count($slug); 

     $data['products']=$this->catalogue_model->getCat($slug,6,$start); 
     $data['categories']=$this->catalogue_model->cat_name($slug); 





      $config['base_url']=base_url().'categories/category/'.$slug; 
    $config['total_rows']=$num; 
    $config['uri_segment'] = 4; 
    $config['per_page']=6; 
    $config['full_tag_open'] = '<ul class="pagination">'; 
$config['full_tag_close'] = '</ul>'; 
$config['prev_link'] = '&lt;'; 
$config['prev_tag_open'] = '<li>'; 
$config['prev_tag_close'] = '</li>'; 
$config['next_link'] = '&gt;'; 
$config['next_tag_open'] = '<li>'; 
$config['next_tag_close'] = '</li>'; 
$config['cur_tag_open'] = '<li class="active"><a href="#"><span class="sr-only">(current)</span>'; 
$config['cur_tag_close'] = '</a></li>'; 
$config['num_tag_open'] = '<li>'; 
$config['num_tag_close'] = '</li>'; 

$config['first_tag_open'] = '<li>'; 
$config['first_tag_close'] = '</li>'; 
$config['last_tag_open'] = '<li>'; 
$config['last_tag_close'] = '</li>'; 
$config['div'] = '#content_area'; 
    $this->jquery_pagination->initialize($config); 
    $data['pages']=$this->jquery_pagination->create_links(); 
     $numitem['num_item']=$this->basket_model->number_of_items; 

    $this->load->view('header',$numitem); 
    $this->load->view('sidebar',$kitties); 
    $this->load->view('categories',$data); 
    $this->load->view('footer'); 

    } 

} 
?> 

視圖/ categories.php

<div class="contentwrap"> 
<div id="content_area"> 
<?php 
if(!empty($categories))echo "<h2>".$categories->name."</h2>"; 
if(!empty($products)){ 
    foreach($products as $p){ 
     ?> 
     <div class="catalogue_wrapper_category"> 
      <div class="catalogue_wrapper_image"> 
       <?php 
        $image=!empty($p['image'])? base_url().'images/'.$p['image']:base_url().'images/unavailable.png'; 
        echo "<a href='".base_url()."products/product/".$p['product_id']."'><img src='".$image."' alt='".$p['product_name']."' width='120' height='120'/></a>"; 
       ?> 
      </div> 
      <div class="catalogue_wrapper_info"> 
      <div class="product_naming"> 
      <h4><a href="<?=base_url()?>products/product/<?=$p['pr_slug']?>"><?php if(strlen($p['product_name'])>26){echo substr($p['product_name'],0,26)."...";}else{echo $p['product_name'];}?></a></h4> 
      </div> 
      <div class="product_price"> 
      <h4>Price:<?=$p['price']." ".$price?></h4> 
      </div> 
      </div> 
     </div> 

     <?php 
    } 
}else{ 
    echo "<div class='well well-sm'><p>Category empty</p></div>"; 

} 

?> 
<div style="clear:both;"></div> 
<div id="pagin"><?php 
echo $pages; 
?></div> 
</div> 

</div> 

我認爲AJAX是獲取所有視圖作爲響應噸ext和和用它替換目標div元素。我應該改變什麼?

回答

0

好的,我已經找到了一個解決方案。我不得不創建一個新的功能,只顯示類別查看內容,並把鏈接到分頁的基礎url.I也必須改變目標div爲.contentwrap。

所以我的$ config ['base_url'] = base_url()。'categories/pagination'。$ slug在類別函數中。

我的分頁功能:

function pagination($slug){ 
     $start = ($this->uri->segment(4) > 0)?$this->uri->segment(4):0; 

     $num=$this->catalogue_model->get_products_count($slug); 

     $products=$this->catalogue_model->getCat($slug,6,$start); 
     $categories=$this->catalogue_model->cat_name($slug); 





      $config['base_url']=base_url().'categories/pagination/'.$slug; 
    $config['total_rows']=$num; 
    $config['uri_segment'] = 4; 
    $config['per_page']=6; 
    $config['full_tag_open'] = '<ul class="pagination">'; 
$config['full_tag_close'] = '</ul>'; 
$config['prev_link'] = '&lt;'; 
$config['prev_tag_open'] = '<li>'; 
$config['prev_tag_close'] = '</li>'; 
$config['next_link'] = '&gt;'; 
$config['next_tag_open'] = '<li>'; 
$config['next_tag_close'] = '</li>'; 
$config['cur_tag_open'] = '<li class="active"><a href="#"><span class="sr-only">(current)</span>'; 
$config['cur_tag_close'] = '</a></li>'; 
$config['num_tag_open'] = '<li>'; 
$config['num_tag_close'] = '</li>'; 

$config['first_tag_open'] = '<li>'; 
$config['first_tag_close'] = '</li>'; 
$config['last_tag_open'] = '<li>'; 
$config['last_tag_close'] = '</li>'; 
$config['div'] = '#content_area'; 
    $this->jquery_pagination->initialize($config); 
    $pages=$this->jquery_pagination->create_links(); 
    $price="&pound;"; 
    ?> 
    <div id="content_area"> 
<?php 
if(!empty($categories))echo "<h2>".$categories->name."</h2>"; 
if(!empty($products)){ 
    foreach($products as $p){ 
     ?> 
     <div class="catalogue_wrapper_category"> 
      <div class="catalogue_wrapper_image"> 
       <?php 
        $image=!empty($p['image'])? base_url().'images/'.$p['image']:base_url().'images/unavailable.png'; 
        echo "<a href='".base_url()."products/product/".$p['product_id']."'><img src='".$image."' alt='".$p['product_name']."' width='120' height='120'/></a>"; 
       ?> 
      </div> 
      <div class="catalogue_wrapper_info"> 
      <div class="product_naming"> 
      <h4><a href="<?=base_url()?>products/product/<?=$p['pr_slug']?>"><?php if(strlen($p['product_name'])>26){echo substr($p['product_name'],0,26)."...";}else{echo $p['product_name'];}?></a></h4> 
      </div> 
      <div class="product_price"> 
      <h4>Price:<?=$p['price']." ".$price?></h4> 
      </div> 
      </div> 
     </div> 

     <?php 
    } 
}else{ 
    echo "<div class='well well-sm'><p>Category empty</p></div>"; 

} 

?> 
<div style="clear:both;"></div> 
<div id="pagin"><?php 
echo $pages; 
?></div> 
</div> 

    <?php 
    }