0

enter image description here

I display custom products in page. use controller ,block and .phtml files define block and template in xml file.

Products are showing correctly i use template module/catalog/view/frontend/template\product\list.phtml .

Now i want to apply pagination and iuse many solution like

how to add pagination on custom collection in Magento 2

https://meetanshi.com/blog/add-pagination-in-magento-2-custom-collection/#comment-962

https://mage2.pro/t/topic/1035

https://www.fmeextensions.com/blog/how-to-add-magnto-2-pagination-to-custom-collection/

All these solution just show limited products on page but not show next page counting toolbar.

Can anyone help me please ? i already spend too much time

1 Answer 1

1

enter image description here

In Your .phtml file :-

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

Block file :-

<?php
namespace Vendor\Module\Block;

use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\View\Element\Template;

class ClassName extends \Magento\Framework\View\Element\Template
{

    protected $YourFactory;

    protected $_storeManager;

    public function __construct(Template\Context $context,
                                StoreManagerInterface $storeManager,
                                \Vendor\Module\Model\YourFactory $YourFactory,
                                array $data = [])
    {
        $this->YourFactory=$YourFactory;
        $this->_storeManager = $storeManager;
        parent::__construct($context, $data);
    }

    public function _prepareLayout()
    {
        parent::_prepareLayout();
        $this->pageConfig->getTitle()->set(__('Your Title'));

        if ($this->getTestimonials()) {
            $pager = $this->getLayout()->createBlock(
                'Magento\Theme\Block\Html\Pager',
                'name.pager'
            )->setAvailableLimit(array(12=>12,24=>24,36=>36,72=>72))
                ->setShowPerPage(true)->setCollection(
                    $this->getYourFunction()
                );
            $this->setChild('pager', $pager);
            $this->getTestimonials()->load();
        }
        return $this;
    }
    public function getPagerHtml()
    {
        return $this->getChildHtml('pager');
    }

    public function getYourFunction()
    {
        $page=($this->getRequest()->getParam('p'))? $this->getRequest()->getParam('p') : 1;
        //get values of current limit
        $pageSize=($this->getRequest()->getParam('limit'))? $this->getRequest
        ()->getParam('limit') : 12;
        $collection = $this->yourFactory->create()->getCollection();
        $collection->setPageSize($pageSize);
        $collection->setCurPage($page)->getData();
        return $collection;
    }

}
12
  • Dear Rk ? what is the difference in your answer and links which i mention in my question ?
    – HaFiz Umer
    Commented May 9, 2019 at 5:42
  • "just show limited products on page but not show next page counting toolbar. " this line not understand please explain this Commented May 9, 2019 at 5:45
  • Sir please check screenshot in my question . i want to navigation for next page.
    – HaFiz Umer
    Commented May 9, 2019 at 5:58
  • Sir i want to pagination for next page. ex. page 1 2 3 4 >
    – HaFiz Umer
    Commented May 9, 2019 at 6:01
  • check screen shot i hve used this code in my site Commented May 9, 2019 at 6:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.