Codeigniter 4分页是工作,但是,我的引导4分页模板不是

mefy6pfw  于 2023-11-14  发布在  其他
关注(0)|答案(2)|浏览(119)

我使用CI4.我已经成功地实现了分页.我有9个职位,并已使用'3'分页,因此3页.一切都工作得很好,但CSS是可怕的.这里是一个pict的分页.而不是页数是正确的.


的数据
因此,我将bootstrap 4 css添加到CI的分页。当我添加bootstrap 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>

col17t5w

col17t5w1#

您已指定要使用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'); ?>

wooyq4lh

wooyq4lh2#

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

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') ?>


我得到的结果如下:


的数据

相关问题