没有数据提供者的分页

时间:2016-03-15 11:35:01

标签: view pagination yii2

您好我尝试使用没有dataProvider的分页。我可以这样使用:

 public function actionShowtest($id){
      $model = $this->findModel($id);
      $messages= \common\models\PrMessage::find()->where(['Rel_Dialog'=>$id])->all();
      $count = count($messages);
      $pagination = new Pagination(['totalCount' => $count, 'pageSize'=>10]);
        return $this->render('messages', [
                'model' => $model,
                'messages'=>$messages,
                'pagination'=>$pagination
            ]);
    }

在我看来:

<?php echo \yii\widgets\LinkPager::widget([
    'pagination' => $pagination,
]); ?>

我的$ count让我回复:

  

int 12

在我看来,我可以看到1,2的分页,但它不起作用,因为它向我显示所有12条记录,但我想看到这10条记录。我有两个页面按钮,但它仍然显示所有记录。我该怎么做才能解决这个问题?

我的观点:

<?php

use yii\helpers\Html;
use yii\widgets\DetailView;
use yii\helpers\Url;
use yii\widgets\ListView;
/* @var $this yii\web\View */
/* @var $model common\models\BlogPost */
$this->title = $model->Id;
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Konwersacje'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;

?>
<div class="blog-post-view">
<?php foreach($model->prMessages as $msg): ?>

  <?=$msg->relSender->Name.' '.$msg->relSender->Surname;  ?>
  <?=$msg->CreatedAt; ?>
  <?=$msg->Text; ?>

<?php endforeach; ?>

</div>
<?php echo \yii\widgets\LinkPager::widget([
    'pagination' => $pagination,
]); ?>

1 个答案:

答案 0 :(得分:1)

它的becoz你的$ messages变量包含所有值,它不受你的分页限制

试试这个

public function actionShowtest($id){
  $model = $this->findModel($id);
  $messagesQuery = \common\models\PrMessage::find()->where(['Rel_Dialog'=>$id]);
  $pagination = new Pagination(['totalCount' => $messagesQuery->count(), 'pageSize'=>10]);
  $messages = $messagesQuery->offset($messagesQuery->offset)
            ->limit($pagination->limit)
            ->all();
  return $this->render('messages', [
            'model' => $model,
            'messages'=>$messages,
            'pagination'=>$pagination
  ]);
}