2

I'm trying to create a pagination for my collection to display in a .phtml file, but am getting error page Invalid template file: 'Magento_Theme::html/pager.phtml.

Here is my code: code/Retailinsights/Promotion/Block/Adminhtml/Index/index.php

<?php

namespace Retailinsights\Promotion\Block\Adminhtml\Index;
use Retailinsights\Promotion\Model\PostTableFactory;

class Index extends  \Magento\Framework\View\Element\Template
{
    public function __construct(\Magento\Backend\Block\Widget\Context $context,
    PostTableFactory $PostTableFactory,        
    array $data = [])
    {
        $this->_PostTableFactory = $PostTableFactory;    
        parent::__construct($context, $data);
    }
    public function getPostCollection(){

        $page = ($this->getRequest()->getParam('p'))? $this->getRequest()->getParam('p') : 1;
        $limit = ($this->getRequest()->getParam('limit'))? $this->getRequest()->getParam('limit') : 1;

        $collection = $this->_PostTableFactory->create()->getCollection();

        $collection->setPageSize($limit);
        $collection->setCurPage($page);
      ///  $collection->setPageSize(3); // fetching only 3 products

        return $collection;
    }
    protected function _prepareLayout(){
        parent::_prepareLayout();
        $this->pageConfig->getTitle()->set(__('Price Rules'));

        if ($this->getPostCollection()){
            $pager = $this->getLayout()->createBlock('Magento\Theme\Block\Html\Pager', 
            'retailinsights.promotion.pager')
                                    ->setAvailableLimit(array(2=>2,10=>10,15=>15,20=>20))
                                    ->setShowPerPage(true)
                                    ->setCollection($this->getPostCollection());


            $this->setChild('pager', $pager);

            $this->getPostCollection()->load();
        }
        return $this;
    }

    public function getPagerHtml()
    {
        return $this->getChildHtml('pager');
    }

}

Its layout file: code/Retailinsights/Promotion/view/adminhtml/layout

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <head>
        <title>Promo Store Mapping</title>
        <css src="Retailinsights_Promotion::css/css.css"/>

    </head> 
    <body>
        <referenceContainer name="content">
            <block class="Retailinsights\Promotion\Block\Adminhtml\Index\Index"
             name="Promotion" 
              template="Retailinsights_Promotion::promotion_index_index.phtml" />
        </referenceContainer>
    </body>
</page>

Its .phtml file: code/Retailinsights/Promotion/view/adminhtml/templates/promotion_index_index.phtml

<h2>Promotions Details</h2>
<table>
    <tr>
    <th>S.no.</th>
    <th>Store Name</th>
    <th>Promotion Name</th>
    <th>Start Date</th>
    <th>End Date</th>
    <th>Status</th>
    </tr>
<?php
$postCollection = $block->getPostCollection();
foreach ($postCollection as $promo) {
    $status = "Disabled";
    if($promo->getData('status')==1){
        $status = "Enabled";
    }
   // print_r($promo->getData());   
    echo "<tr>";
    echo "<td>".$promo->getData('p_id')."</td>";
    echo "<td>".$promo->getData('seller_name')."</td>";
    echo "<td>".$promo->getData('description')."</td>";
    echo "<td>".$promo->getData('pstart_date')."</td>";
    echo "<td>".$promo->getData('pend_date')."</td>";
    echo "<td>".$status."</td>";
    echo "</tr>";
}

?>
</table>

<?php
if ($this->getPagerHtml()){
    echo $this->getPagerHtml();
}else{
    echo 'records not available.';
} 

?>

I can not understand why Pager is not creating, can you help?

  • Any reason to avoid magento grid system? – Sohel Rana Apr 25 at 6:43
  • 1
    Thank you @SohelRana for replying, requirement is to avoid grid system. can we add pagination here – Praveen Negimani Apr 25 at 6:53
  • Did you find solution on this? – Shoaib Munir May 6 at 6:17
  • 1
    @ShoaibMunir I switched to magento grid system – Praveen Negimani May 6 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.