For implementing pagination in magento 2, you need to implement the code accourding to below custom extension block file(news.php):-

namespace News\NewsContent\Block;

use News\NewsContent\Model\ItemsFactory;

class NewsContent extends \Magento\Framework\View\Element\Template
{
protected $context;
protected $_newscontentcontent;

public function __construct(
\Magento\Backend\Block\Template\Context $context,
ItemsFactory $newscontentcontent,
array $data = []
)
{
$this->_newscontentcontent = $newscontentcontent;
parent::__construct($context, $data);
}

protected function _prepareLayout()
{

parent::_prepareLayout();
// $this->pageConfig->getTitle()->set(__(‘My News’));

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$conf_text_meta_limit = $objectManager->get(‘Magento\Framework\App\Config\ScopeConfigInterface’)->getValue(‘news/general/text_meta_limit’);

if ($this->getNewsCollection()) {
$pager = $this->getLayout()->createBlock(
‘Magento\Theme\Block\Html\Pager’,
‘news.collection.pager’
)->setAvailableLimit(array($conf_text_meta_limit=>$conf_text_meta_limit))
->setShowPerPage(true)->setCollection(
$this->getNewsCollection()
);
$this->setChild(‘pager’, $pager);
$this->getNewsCollection()->load();
}
return $this;
}

public function getNewsCollection()
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$conf_text_meta_limit = $objectManager->get(‘Magento\Framework\App\Config\ScopeConfigInterface’)->getValue(‘news/general/text_meta_limit’);

$page=($this->getRequest()->getParam(‘p’))? $this->getRequest()->getParam(‘p’) : 1;
//get values of current limit
$pageSize=($this->getRequest()->getParam(‘limit’))? $this->getRequest
()->getParam(‘limit’) : $conf_text_meta_limit;

$collection = $this->_newscontentcontent->create()->getCollection();
$collection->addFieldToFilter(‘is_active’, ‘1’);
$collection->setPageSize($pageSize);
$collection->setCurPage($page);
return $collection;
}

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

public function getImagesCollection($id){
$this->_resources = \Magento\Framework\App\ObjectManager::getInstance()->get(‘Magento\Framework\App\ResourceConnection’);
$connection = $this->_resources->getConnection();
$table = $this->_resources->getTableName(‘news_newscontent_items’);
$sql = “Select * FROM ” . $table.” WHERE id = $id”;

$result = $connection->fetchAll($sql);
$arr=array();
$arr1=array();
$arr2=array();
$arr3=array();
$i = 0;
foreach($result as $re){
$arr = array_filter(explode(‘<img’,$re[‘image’]));
}

foreach($arr as $ar){
#foreach($ar as $ar1){
$arr1[] = array_filter(explode(‘url=”‘,$ar));
#}
}
foreach($arr1 as $ar1){
foreach($ar1 as $ar2){
$arr2[] = array_filter(explode(‘”}}’,$ar2));
}
}
foreach($arr2 as $ar2){
$imgurl = $ar2[0];
if (strpos($imgurl, ‘wysiwyg’) !== false){
$arr3[] = $ar2[0];
}
}
return $arr3;
}

}

Your Answer
Sign In
Enter email and password to comment or answer
Not the answer you are looking for? Browse other questions tagged or ask your own question.