I am working on custom module. How can i use default magento pagination with my custom collection and also set custom limits on that?
Magento2 Custom Collection – How to Add Pagination
collection;custommagento2pagination
Related Solutions
This is how you can set custom toolbar and pager for your custom collection, below is an example shared, I am using this snippet currently.
Set Toolbar and Pager inside your phtml (your file that is to be called inside static block):
/* Set Toolbar Pagination for Custom Loaded Collection */
$toolbar = Mage::getBlockSingleton('catalog/product_list')->getToolbarBlock();
$toolbar->setCollection($_productCollection);
/* Set Pager */
$pager = $this->getLayout()->createBlock('page/html_pager', 'custom.pager');
$pager->setAvailableLimit(array($itemsLimit=>$itemsLimit));
$pager->setCollection($_productCollection);
$toolbar->addPagerLimit('grid', 50, 50);
$toolbar->addPagerLimit('grid', 75, 75);
$toolbar->addPagerLimit('grid', 100, 100);
$toolbar->setChild('product_list_toolbar_pager', $pager);
$toolbar->setData('_current_limit', $itemsLimit);
//Set order, define $sortBy and $orderBy
$toolbar->setData('_current_grid_order', $sortBy);
$toolbar->setData('_current_grid_direction', $orderBy);
To display toolbar on top of list, use :
<?php echo $toolbar->toHtml(); ?>
And to display pager on bottom, use :
<!-- Show Bottom Pagination Toolbar -- PAGER -->
<div class="toolbar-bottom">
<div class="toolbar">
<?php echo $pager->toHtml();?>
</div>
</div>
- Now you can call static block on a category page
- Then you can define your file path inside that static block
- Call your custom collection inside that file and use steps above
- And That's it :) Enjoy
Ok so i had to changed the way i used to access from 2 tables to 1 table and it has resolved my issue but yes somehow i was able to manage to get pagination with two collection by merging them as below rather than using array_merge
$page= ($this->getRequest()->getParam('p'))? $this->getRequest()->getParam('p') : 1;
if($page == 1)
{
foreach($collection_1 as $coll) {
$collection_2->addItem($coll);
}
return $collection_2;
}
else{
return $collection_2;
}
But the above code made one issue persistent that is incorrect pagination count eg: 1 to 12 of 18, Though in frontend all of the values were displayed i.e 20 in total
In my collection_1 there were 2 values and in collection_2 there were 18 values so in first page rather than showing first 10 values as per the limiter it showed 12 as i was adding 2 items runtime in collection_2 but pagination took the original count of collection_2 i.e 18 because using addItem() adds the data to the _items and not _data in collection array
Class: Magento\Framework\Data\Collection
Function: public function addItem(\Magento\Framework\DataObject $item)
which i think was the issue for the wrong pagination count as well as in getting the totalnum in pager.php which was eventually returning the collection_2 original count i.e 18 i added page check in my code to get whether it was page 1 because looping in foreach was adding those 2 values from collection_1 on every page.
I had to change the way due to time constraint, In case in future if anybody reaches till this point and goes further than would request to post their solution here as well so it can help others.
Best Answer
get collection for that
Add pagination
adding the child block
in phtml file
Reference