magento 2 Pagination code example

Example 1: pagination magento

<?php if ($block->getPagerHtml()): ?>
        <div class="order-products-toolbar toolbar bottom"><?php echo $block->getPagerHtml(); ?></div>
    <?php endif ?>

Example 2: pagination magento

public function getNews()
    {
      //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') : 1;


        $newsCollection = $this->newscollectionFactory->create();
        $newsCollection->addFieldToFilter('is_active',1);
        $newsCollection->setOrder('title','ASC');
        $newsCollection->setPageSize($pageSize);
        $newsCollection->setCurPage($page);
        return $newsCollection;
    }

Tags:

Php Example