Codeigniter jquery分页库

时间:2015-03-18 23:34:00

标签: jquery ajax codeigniter pagination codeigniter-2

我找到了这个Codeigniter jquery分页库https://github.com/bcit-ci/CodeIgniter/wiki/AJAX-Pagination-with-CI-Pagination-Library,我在使用它时遇到了问题。当我点击分页链接时,它会将整个网站(所有视图)加载到目标div中。这是我的设置:

控制器/ 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将所有视图作为响应文本获取并用它替换目标div元素。我应该更改什么?

1 个答案:

答案 0 :(得分:0)

好的,我找到了一个解决方案。我必须创建一个新功能,只显示类别视图内容,并将链接放入分页基本url.I还必须将目标div更改为.contentwrap。

所以我的$ config [&#39; base_url&#39;] = base_url()。&#39;类别/分页&#39;。$ 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
    }