V
V
Voroncheg2019-09-01 18:09:12
Yii
Voroncheg, 2019-09-01 18:09:12

Yii2. How to move posts to another page when using pagination?

Hello. Displayed entries in the view with pagination, but it only shows the pagination widget, but does not transfer entries to another page. Please tell me how to make it transfer?
Here is the controller

public function actionUsers(){
        $users = GetUsers::find()->all();

        $pagination = new Pagination([
            'defaultPageSize' => 5,
            'totalCount' => count($users),
        ]);

        return $this->render('users', [
            'users' => $users,
            'pagination' => $pagination
        ]);
    }

Here is the view
use yii\helpers\Html;
use yii\widgets\LinkPager;
?>
<h1>Users</h1>
<ul>
    <?php foreach ($users as $user): ?>
        <li>
            <?php
                echo $user->firstname;
            ?>
        </li>
    <?php endforeach; ?>
</ul>

<?= LinkPager::widget(['pagination' => $pagination]) ?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2019-09-01
@Voroncheg

You misunderstood the work of the piganator. See what it says in the documentation.
https://www.yiiframework.com/doc/api/2.0/yii-data-...
Record selection is changed with

$models = $query->offset($pages->offset)
        ->limit($pages->limit)
        ->all();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question