1

I have filtered customer collection and showing in account navigation with one new custom link. upon clicking custom link on left nav i need to show collection on right side with pagination.

How this can be done? So far i am able to bring custom link on left nav and after clicking on it getting customer collection. How to add pagination to it with limit ? Is that can be done. please advice anyone.

3
  • please review this blog.qaisarsatti.com/magento_2/…
    – Shorabh
    Aug 3, 2018 at 8:56
  • can you explain in detail , i want to add pagination for my custom collection on customer account navigation. Already i bought the link on left nav with collection. how to show that in pagination? Aug 3, 2018 at 9:07
  • Can you update your code to the question? Aug 3, 2018 at 10:46

2 Answers 2

1

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; ?>
2
  • 1
    What is Your_module_namespace.Your_module_name.record.pager? namespace and module_name i will replace with mine, what is record and pager? Aug 3, 2018 at 10:50
  • any update about this? Aug 5, 2019 at 15:21
0

Use the below method

$collection->setPageSize()
3
  • So it will show pagination this line? Please explain elaborately Aug 3, 2018 at 9:22
  • just check vendor/magento/module-backend/view/adminhtml/templates/widget/grid/extended.phtml You will now what to do.
    – Avesh Naik
    Aug 3, 2018 at 9:25
  • @AveshNaik what you mean You will now what to do??? Jul 5, 2019 at 13:51

Your Answer

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

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