Codeigniter 4 分页正常工作,但是,我的 Bootstrap 4 分页模板不起作用

问题描述 投票:0回答:2

我正在使用 CI4。我已经成功实现了分页。我有 9 个帖子,并使用“3”进行分页,因此有 3 页。一切都很好,但 CSS 很糟糕。这是分页的图片。不是正确的页数。

因此,我将 boostrap 4 css 添加到 CI 的分页中。当我添加boostrap 4的分页时,它只显示1页。 css 采用 bs4 格式,但页面仅显示“1”。这是一张图片:

这是我的控制器:

class Post extends BaseController
{

function __construct(){
    $this->PostModel        = new \App\Models\PostModel;    
    $this->CategoryModel    = new \App\Models\CategoryModel;
}

function index(){
    $post_category_id = null;
    $category_is_published = 1;
    $post_is_published = 1; 
    $pagination = 1;  // this is just to tell the model to return $this instead of the results.
    $this->data['posts']    = $this->PostModel->getPostsByCategory($post_category_id, 
                              $category_is_published, $post_is_published, $pagination )->paginate(3);
    $this->data['pager']    = $this->PostModel->pager;
    return view('Post/post_index', $this->data);
}

}//end controller

这是我的视图文件(当然,我已经缩短了它)。

$pager->links()
将显示正确的页数,但是当我添加 bootstrap 4 模板时,如下所示,它只显示一页。

<?php foreach ($posts as $post) :  ?>
    <p class="card-text"> <?php echo word_limiter($post->post_body, 30); ?></p>
<?php endforeach; ?>
<?= $pager->links('bootstrap', 'bootstrap4_pagination'); ?>

这是我的 bootstrap4_pagination 模板;

<?php $pager->setSurroundCount(4); ?>
<nav>
<ul class="pagination">
    <?php if ($pager->hasPrevious()) { ?>
        <li class="page-item">
             <a href="<?= $pager->getFirst() ?>" aria-label="First" class="page-link">
                <span aria-hidden="true">First</span>
            </a>
        </li>
        <li class="page-item">
            <a href="<?= $pager->getPrevious() ?>" class="page-link" aria-label="Previous">
                <span>&laquo;</span>
            </a>
        </li>
    <?php } ?>

    <?php 
        foreach ($pager->links() as $link) { 
            $activeclass = $link['active']?'active':'';
    ?>
        <li class="page-item <?= $activeclass ?>">
            <a href="<?= $link['uri'] ?>" class="page-link">
                <?= $link['title'] ?>
            </a>
        </li>
    <?php } ?>

    <?php if ($pager->hasNext()) { ?>
        <li class="page-item">
            <a href="<?= $pager->getNext() ?>" aria-label="Next" class="page-link">
                <span aria-hidden="true">&raquo;</span>
            </a>
        </li>
        <li class="page-item">
             <a href="<?= $pager->getLast() ?>" aria-label="Last" class="page-link">
                <span aria-hidden="true">Last</span>
            </a>
        </li>
    <?php } ?>
</ul>
</nav>
php twitter-bootstrap codeigniter bootstrap-4 codeigniter-4
2个回答
1
投票

您已指定要使用

bootstrap
组:

<?= $pager->links('bootstrap', 'bootstrap4_pagination'); ?>

但是您没有创建

bootstrap
群组:

$this->data['posts']    = $this->PostModel->getPostsByCategory($post_category_id,  $category_is_published, $post_is_published, $pagination )->paginate(3);

这应该是:

$this->data['posts']    = $this->PostModel->getPostsByCategory($post_category_id,  $category_is_published, $post_is_published, $pagination )->paginate(3, 'bootstrap');

或者,您可以省略该组并将

bootstrap
替换为
default

<?= $pager->links('default', 'bootstrap4_pagination'); ?>


0
投票

只是为了帮助其他人和初学者,这是我的方法:

app\Config\Pager.php

public array $templates = [
    'default_full' => 'CodeIgniter\Pager\Views\default_full',
    'default_simple' => 'CodeIgniter\Pager\Views\default_simple',
    'default_head' => 'CodeIgniter\Pager\Views\default_head',
    'my_pagination' => 'App\Views\Pager\my_pagination',
];

我创建了一个视图文件

app\Views\Pager\my_pagination.php
(bootstrap 4)

            <?php $pager->setSurroundCount(4); ?>
            <nav>
            <ul class="pagination">
                <?php if ($pager->hasPrevious()) { ?>
                    <li class="page-item">
                        <a href="<?= $pager->getFirst() ?>" aria-label="First" class="page-link">
                            <span aria-hidden="true">First</span>
                        </a>
                    </li>
                    <li class="page-item">
                        <a href="<?= $pager->getPrevious() ?>" class="page-link" aria-label="Previous">
                            <span>&laquo;</span>
                        </a>
                    </li>
                <?php } ?>

                <?php 
                    foreach ($pager->links() as $link) { 
                        $activeclass = $link['active']?'active':'';
                ?>
                    <li class="page-item <?= $activeclass ?>">
                        <a href="<?= $link['uri'] ?>" class="page-link">
                            <?= $link['title'] ?>
                        </a>
                    </li>
                <?php } ?>

                <?php if ($pager->hasNext()) { ?>
                    <li class="page-item">
                        <a href="<?= $pager->getNext() ?>" aria-label="Next" class="page-link">
                            <span aria-hidden="true">&raquo;</span>
                        </a>
                    </li>
                    <li class="page-item">
                        <a href="<?= $pager->getLast() ?>" aria-label="Last" class="page-link">
                            <span aria-hidden="true">Last</span>
                        </a>
                    </li>
                <?php } ?>
            </ul>
            </nav>

然后使用时,我编写了如下所示的控制器:

    $data = [
        'result' => $model->paginate(25),
        'headers_col' => $model->getFieldNames('my_table_name'),
        'pager' => $model->pager,
    ];

在如下视图中:

 <?= $pager->links('default', 'my_pagination') ?>

我得到了如下结果:

© www.soinside.com 2019 - 2024. All rights reserved.