Add code in your block file.
protected $_productFactory;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Model\ProductFactory $productFactory,
array $data = []
) {
$this->_productFactory = $productFactory;
parent::__construct($context, $data);
}
public function displayCollection()
{
//get values of current page
$page = ($this->getRequest()->getParam('p')) ? $this->getRequest()->getParam('p') : 1;
//get values of current limit
$pageSize = ($this->getRequest()->getParam('limit')) ? $this->getRequest()->getParam('limit') : 10;
// get your collection here for ex:- $this->_productFactory->create()->getCollection()
$productCollection = $this->_productFactory->create()->getCollection();
$productCollection->setPageSize($pageSize);
$productCollection->setCurPage($page);
return $productCollection;
}
protected function _prepareLayout()
{
parent::_prepareLayout();
if ($this->displayCollection()) {
$pager = $this->getLayout()->createBlock(
'Magento\Theme\Block\Html\Pager',
'Your_module_namespace.Your_module_name.record.pager'
)->setAvailableLimit(array(10 => 10, 15 => 15, 20 => 20, 25 => 25))->setShowPerPage(true)->setCollection(
$this->displayCollection()
);
$this->setChild('pager', $pager);
$this->displayCollection()->load();
}
return $this;
}
public function getPagerHtml()
{
return $this->getChildHtml('pager');
}
Add code in your phtml file.
<?php
if ($block->displayCollection()) {
$custommodel = $block->displayCollection();
}
?>
<?php if($custommodel && count($custommodel)): ?>
<?php foreach ($custommodel as $value):?>
//get your content here
<?php endforeach; ?>
<?php if ($block->getPagerHtml()): ?>
<div class="order-products-toolbar toolbar bottom custom-pager"><?php echo $block->getPagerHtml(); ?></div>
<?php endif ?>
<?php else: ?>
<div class="message info empty" ><span><?php echo __('You have no any data.....'); ?></span></div>
<?php endif; ?>