Answer the question
In order to leave comments, you need to log in
Yii2. How to implement pagination?
I have a list of news. I use a widget to display them. I attached pagination to the page, but it does not work. If you display records per page in a cycle, everything is fine. But it doesn't work through the widget...
The action code in the controller
public function actionIndex()
{
/*Загрузка новостей*/
$query = News::find();
$pages = new Pagination(['totalCount' => $query->count(), 'pageSize' => 10, 'pageSizeParam' => false]);
$newsList = $query->offset($pages->offset)->limit($pages->limit)->orderBy('created_at DESC')->all();
return $this->render('index', compact('newsList', 'pages'));
}
<?php
use yii\helpers\Html;
$this->title = Yii::t('news', 'title');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="news-index container">
<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a(Yii::t('news', 'btn_create'), ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= \app\components\MainViewWidget::widget(['template' => 'news-block-list', 'data' => $newsList]) ?>
<div class="row"><?php LinkPager::widget(['pagination' => $pages]) ?></div>
</div>
namespace app\components;
use yii\base\Widget;
use yii\widgets\LinkPager;
class MainViewWidget extends Widget
{
public $template;
public $data;
public $pages;
public $viewHtml;
//Инициальзация виджета. Если не передан шаблон, используем block-list
public function init(){
parent::init();
if ($this->template === null) {
$this->template = 'blocks-list';
}
$this->template .= '.php';
}
//Запуск виджета
public function run(){
$this->viewHtml = $this->getMainViewHtml($this->data);
return $this->viewHtml;
}
//Отправляем данные в шаблон
protected function getMainViewHtml($data){
$str = '';
foreach ($data as $item){
$str .= $this->catToTemplate($item);
}
return $str;
}
//Подключаем нужный шаблон
protected function catToTemplate($items){
ob_start();
include __DIR__ . '/template/mainView/' . $this->template;
return ob_get_clean();
}
}
<div class="row">
<div id="news-block-<?= $items->id ?>" class="col-lg-12 news-block-one">
<h3><a href=""><?= $items->title ?></a></h3>
<div class="news-content max-news-content" id = "news-content-<?= $items->id ?>">
<p><?= $items->content ?></p>
</div>
<div class="news-action">
<span><i class="fa fa-clock-o" aria-hidden="true"></i> <?= Yii::$app->formatter->asDatetime($items->publish_at, "php:d.m.Y / H:i"); ?> </span>
</div>
</div>
</div>
Answer the question
In order to leave comments, you need to log in
Change the line
on the
<div class="row"><?= LinkPager::widget(['pagination' => $pages]) ?></div>
Use ActiveDataProvider to get news www.yiiframework.com/doc-2.0/yii-data-activedatapr...
And to display news on the page use www.yiiframework.com/doc-2.0/yii-widgets-listview.html You
will get the same but much easier.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question