Reputation: 169
I can't find a way to paginate my entities in magento 2. I have this code :
public function getPosts()
{
if (!$this->hasData('posts')) {
$posts = $this->_postCollectionFactory->create()->addOrder(
PostInterface::CREATED,
PostCollection::SORT_ORDER_DESC
);
$this->setData('posts', $posts);
}
return $this->getData('posts');
}
in magento 1.x i had custom block which had "page/html_pager" type but I can't find in magento 2 documentation a way to paginate my entities ... I fetch them from my block controller (code above).
Upvotes: 1
Views: 10479
Reputation: 1
There are following Steps to add pagination to custom block in Magento 2
Step 1
public function getPosts()
{
$page=($this->getRequest()->getParam('p'))? $this->getRequest()->getParam('p') : 1;`enter code here`
$pageSize=($this->getRequest()->getParam('limit'))? $this->getRequest()->getParam('limit') : 5;
if (!$this->hasData('posts')) {
$posts = $this->_postCollectionFactory->create();
$posts->addOrder('field name','ASC');
}
return $posts;
}
Upvotes: 0
Reputation: 703
You should check out the file toolbar.phtml. In here you find a class method called
$block->getPagerHtml()
This method calls the pagination for your entities. For example in Category pages, products will be the entities. but I'm guessing you can always change this default method. If you keep following up this method you find the block class located in
\Magento\Catalog\Block\Product\ProductList\Toolbar.php
You will find the function getPagerHtml()
public function getPagerHtml()
{
$pagerBlock = $this->getChildBlock('product_list_toolbar_pager');
if ($pagerBlock instanceof \Magento\Framework\DataObject) {
/* @var $pagerBlock \Magento\Theme\Block\Html\Pager */
$pagerBlock->setAvailableLimit($this->getAvailableLimit());
$pagerBlock->setUseContainer(
false
)->setShowPerPage(
false
)->setShowAmounts(
false
)->setFrameLength(
$this->_scopeConfig->getValue(
'design/pagination/pagination_frame',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
)
)->setJump(
$this->_scopeConfig->getValue(
'design/pagination/pagination_frame_skip',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
)
)->setLimit(
$this->getLimit()
)->setCollection(
$this->getCollection()
);
return $pagerBlock->toHtml();
}
return '';
}
Conclusion:
Create a custom module in app\code\Your\Custom\Block\Toolbar.php that extends to \Magento\Catalog\Block\Product\ProductList\Toolbar
namespace Your\Custom\Block\;
Class Toolbar extends \Magento\Catalog\Block\Product\ProductList\Toolbar
{
public function getPagerHtml()
{
..... Your Code for you post entities .....
}
}
Your layout xml add the toolbar block and add the block class -> your_custom_index.xml
<block class="Your\Custom\Block\Toolbar" name="product_list_toolbar" template="Your_Custom::product/list/toolbar.phtml">
Templates -> view\frontend\templates\product\list\toolbar.phtml:
<?php
echo $block->getPagerHtml();
Now this is an example on how you would extend this functionality. Read more about creating and extending core modules because you'll need a few more files to extend custom module that I'm not posting here because they get out of the main subject.
Upvotes: 0
Reputation: 624
Refer following link, in that example pagination is added.
http://www.mage-world.com/blog/create-the-news-list-page-via-frontend-in-magento-2.html
Upvotes: 3