1
我做了分頁,但所有的帖子(我有8個)顯示在每一頁上。我想在每個頁面上顯示其中的4個。我的錯誤在哪裏?我認爲問題出在count($posts)
,因爲在我看過的其他帖子中,他們使用的是$posts->count()
,但這種方法對我不起作用。感謝每一個建議!yii2分頁不起作用
控制器動作:
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>
好!我像你說的那樣做了,但是我認爲該怎麼做?我很抱歉,我只是在yii2和編程方面還是新手。也將編輯我的看法! 我做的是'LinkPagination ::小部件([[我應該在這裏做什麼])':-) –
哪種類型的視圖..你需要..一個gridview? ..一個listview ..? ..正常的分頁很容易管理的小部件(gridview或列表視圖小部件)..答案更新.. – scaisEdge
我只是想分裂我的佈局在每個頁面有4個職位,但也許我不明白分頁enought: )將添加圖像與我的看法,看看我想要做什麼。我有6個帖子,我希望他們在這個頁面上是4,其餘的在第二頁上。也許這對我來說是先進的:) –