codeigniter4 分页

从CI3升级到CI4了,看着官方的中文文档,太头痛了无力吐槽。摸索了好久才把分页搞定,记录一下过程。

一、模型

public function getUserList($where,$p){
	$result = $this->select('users_id,mobile,name,sex,age,addtime')->where($where)->orderBy('users_id', 'DESC')->limit($p['page_num'],$p['start_position'])->get()->getResultArray();
	return $result;
}

二、控制器

$model = new UsersModel();
// 加载所需的 helper
helper(['url', 'pagination']);
 
// 定义分页配置
$config = [
	 'baseURL' => base_url('main/users_list'), // 设置分页链接的基本URL
	 'totalRows' => $model->countAll(), // 获取总行数
	 'perPage' => 3, // 每页显示的记录数
];
 
// 初始化分页类
$pager = \Config\Services::pager();
 
// 获取当前页码
$currentPage = $this->request->getVar('page') ? $this->request->getVar('page') : 1;
// 获取分页数据
$data['users'] = $model->getUserList([], array('page_num' => $config['perPage'],'start_position' => ($currentPage - 1) * $config['perPage']));
 
// 生成分页链接
// my_template在app\Config\Pager.php中配置
$data['pager'] = $pager->makeLinks($currentPage, $config['perPage'], $config['totalRows'],'my_template');
 
echo view('admin/users', $data);

三、视图

<?php foreach ($users as $user) : ?>
    <p><?= $user['users_id'] ?></p>
	<p><?= $user['username'] ?></p>
<?php endforeach; ?>
<!-- 分页链接 -->
<?= $pager ?>

四、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_template' => 'CodeIgniter\Pager\Views\page_test',
];
//page_test.php文件在\system\Pager\Views文件夹中创建

五、分页模板page_test.php
<?php $pager->setSurroundCount(2) ?>
 
<ul class="pagination">
    <?php if ($pager->hasPreviousPage()) : ?>
        <li class="page-item">
            <a href="<?= $pager->getFirst() ?>" class="page-link" aria-label="First">
                <span aria-hidden="true">&laquo;</span>
            </a>
        </li>
        <li class="page-item">
            <a href="<?= $pager->getPreviousPage() ?>" class="page-link" aria-label="Previous">
                <span aria-hidden="true">&lsaquo;</span>
            </a>
        </li>
    <?php endif ?>
 
    <?php foreach ($pager->links() as $link) : ?>
        <?php if ($link['active']) : ?>
            <li class="page-item active">
                <a href="<?= $link['uri'] ?>" class="page-link"><?= $link['title'] ?></a>
            </li>
        <?php else : ?>
            <li class="page-item">
                <a href="<?= $link['uri'] ?>" class="page-link"><?= $link['title'] ?></a>
            </li>
        <?php endif ?>
    <?php endforeach ?>
 
    <?php if ($pager->hasNextPage()) : ?>
        <li class="page-item">
            <a href="<?= $pager->getNextPage() ?>" class="page-link" aria-label="Next">
                <span aria-hidden="true">&rsaquo;</span>
            </a>
        </li>
        <li class="page-item">
            <a href="<?= $pager->getLast() ?>" class="page-link" aria-label="Last">
                <span aria-hidden="true">&raquo;</span>
            </a>
        </li>
    <?php endif ?>
</ul>

发表评论

邮箱地址不会被公开。 必填项已用*标注