yii2分页不起作用

时间:2017-04-17 15:30:51

标签: php pagination yii2

我做了分页,但所有帖子(我有8个)都显示在每一页上。我想在每个页面上显示其中的4个。我的错误在哪里?我认为问题出在count($posts),因为在我看到的其他帖子中,他们使用的是$posts->count(),但这种方法对我不起作用。感谢每一条建议!

控制器动作:

public function actionIndex()
{
    $searchModel = new PostSearch();
    $postModel = new \app\models\Post();
    $userModel = new \app\models\BlogUser();

    $posts = $postModel::find()->orderBy(['post_id' => SORT_DESC])->all();

    $pagination = new Pagination(['totalCount' => count($posts), 'pageSize' => 4]);

    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
        'postModel' => $postModel,
        'userModel' => $userModel,
        'posts' => $posts,
        'pagination' => $pagination
    ]);
}

查看index.php:

<div class="content">
        <div class="row">
            <?php Pjax::begin(['id' => 'posts']) ?>
            <div class="col-md-12">
                <?php

                    foreach ($posts as $post)
                    {
                        $id = $post->user_id;

                        $user = $userModel::find()->where(['id' => $id])->one();

                        echo "<div class='col-md-6 blog-post'>
                                  <div class='col-md-3 post-prof-img'>"
                                        . Html::img('../images/' . $user->image, ['alt' => 'image', 'class' => 'tall img-circle']) .
                                  "<p class='text-center title-par'><strong><em> $user->username </em></strong></p>
                                   <p class='text-center'> $post->date_create</p>
                                  </div>
                                  <div class='col-md-9 post-cont'><p>"
                                        . Html::a('Click for more!',['view', 'id' => $post->post_id]) .
                                        "</p><label class='text-center'> $post->title : </label>
                                        <div class='fade-post'>
                                            $post->content
                                        </div>
                                  </div>
                              </div>";
                    }

                ?>

            </div>
            <?php Pjax::end() ?>
        </div>

    </div>

    <div class="row">
        <div class="col-md-3">
            <?= Html::a('Create Post', ['create'], ['class' => 'btn btn-primary']) ?>
            <?= Html::submitButton('Search', ['class' => 'btn search-button', 'style' => 'color: blue']) ?>
        </div>
    </div>

posts page

1 个答案:

答案 0 :(得分:2)

您应该使用dataProvider-&gt; pagination-&gt; pageSize(而不是创建新的分页并传递给渲染)

  $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  $dataProvider->pagination->pageSize=4;

并且在视图中您可以使用例如:listview小部件作为

/* index.php view file*/

 <?= 
   ListView::widget([
     'dataProvider' => $dataProvider,
   ]); 
 ?>

或gridview

<?= GridView::widget([
      'dataProvider' => $dataProvider,
      'filterModel' => $searchModel,
      'columns' => [
          ['class' => 'yii\grid\SerialColumn'],
          'your_column1',
          'your_column2',
           .....

          ['class' => 'yii\grid\ActionColumn',
          'contentOptions' => ['style' => 'width:84px; font-size:18px;']],
      ],
  ]); ?>