首页 > 解决方案 > 调用未定义函数 setSurroundCount() CodeIgniter 4

问题描述

我试图在我的视图中实现分页,但是当我调用 setSurroundCount 函数时,它给了我一个错误“调用未定义的方法 CodeIgniter\Pager\Pager:ConfusedetSurroundCount()”有人知道为什么吗?

这是我的控制器:

function index($limit=100) {

    $model = new \App\Models\TransactionModel();

    $data = [
        'title' => "Transactions",
        'transactionsHTMLTable' => $this->formatTransactionsAsHTMLTable($model->paginate($limit)),
        'pager' => $model->pager
    ];

    echo view('top', $data);
    echo view('transactions', $data);
    echo view('bottom');
} 

这是我的看法;

<h3>Transactions</h3>
<?php
    echo $transactionsHTMLTable;
    // echo $pager->links();
    $pager->setSurroundCount(2);
?>

<nav aria-label="Page navigation">
    <ul class="pagination">
    <?php if ($pager->hasPrevious()) : ?>
        <li>
            <a href="<?= $pager->getFirst() ?>" aria-label="First">
                <span aria-hidden="true">First</span>
            </a>
        </li>
        <li>
            <a href="<?= $pager->getPrevious() ?>" aria-label="Previous">
                <span aria-hidden="true">&laquo;</span>
            </a>
        </li>
    <?php endif ?>

    <?php foreach ($pager->links() as $link) : ?>
        <li <?= $link['active'] ? 'class="active"' : '' ?>>
            <a href="<?= $link['uri'] ?>">
                <?= $link['title'] ?>
            </a>
        </li>
    <?php endforeach ?>

    <?php if ($pager->hasNext()) : ?>
        <li>
            <a href="<?= $pager->getNext() ?>" aria-label="Previous">
                <span aria-hidden="true">&raquo;</span>
            </a>
        </li>
        <li>
            <a href="<?= $pager->getLast() ?>" aria-label="Last">
                <span aria-hidden="true">Last</span>
            </a>
        </li>
    <?php endif ?>
    </ul>
</nav> 

这是函数 formatTransactionsAsHTMLTable() :

function formatTransactionsAsHTMLTable($transactions) {
    $htmlTable = array();

    $table = new \CodeIgniter\View\Table();

    $template = [
        'table_open' => '<table class="table table-striped table-hover table-sm small">'
    ];

    $table->setTemplate($template);

    $table->setHeading('ID', 'Date', 'Subscription ID', 'Duration', 'Price', 'Proceed', 'Device', 'Country', 'Subscriber ID');

    foreach ($transactions as $key => $transaction) {
        $table->addRow($transaction['id'], $transaction['date'], $transaction['subscriptionId'], $transaction['duration'], $transaction['price'] . " " . $transaction['currency'], $transaction['proceed'] . " " . $transaction['proceedCurrency'], $transaction['device'], $transaction['country'], $transaction['subscriberId']);
    }

    return $table->generate();
}

我从 Codeigniter 4 文档中获取了所有内容。

如果我回显 $pager->links() 它可以工作。所以 $pager 在这里但是函数 setSurroundCount getFirst, getNext, ... 不起作用。有人能帮我吗 ?

标签: phphtmlcodeignitercodeigniter-4

解决方案


您需要按照手册中的子标题来自定义链接。在 app/Views 中创建一个文件夹(Pagers),然后在其中创建一个视图文件。它将是 Pagers/name_of_view_file.php。

然后您需要编辑 app/Pager.php 以使用您的视图文件。


推荐阅读