I have displayed custom collection on my custom page. The issue i am facing is that pagination is supposed to limit the collection per page but here in frontend whole collection is displayed on one page and the page count shows as
Items 1 to 20 of 20 Total for the collection count of 20 where as the page links are created and divided into two as per the collection count.
Also important thing is that the collection i fetch is in two different array which i merged and then converted the resultant array to magento collection. The merged array collection is proper though.and pagination works fine if i pass only one collection.
Code used as below:
The merged collection array
$all_collection = array_merge((array)$store_table_collection->getData() ,(array)$attribute_table_collection->getData());
return $all_collection
Merged Collection dumped result.
object(stdClass)#1546 (20) {
[0]=>
array(3) {
["logo_path"]=>
NULL
["url_key"]=>
string(8) "hiketest"
["option_value"]=>
string(4) "Hike"
}
[1]=>
array(3) {
["logo_path"]=>
NULL
["url_key"]=>
string(6) "yogass"
["option_value"]=>
string(4) "Yoga"
}
[2]=>
array(3) {
["logo_path"]=>
NULL
["url_key"]=>
string(7) "outdoor"
["option_value"]=>
string(7) "Outdoor"
}
[3]=>
array(3) {
["logo_path"]=>
NULL
["url_key"]=>
string(7) "running"
["option_value"]=>
string(7) "Running"
}
[4]=>
array(3) {
["logo_path"]=>
NULL
["url_key"]=>
string(6) "warmup"
["option_value"]=>
string(6) "Warmup"
}
[5]=>
array(3) {
["logo_path"]=>
NULL
["url_key"]=>
string(10) "recreation"
["option_value"]=>
string(10) "Recreation"
}
[6]=>
array(3) {
["logo_path"]=>
NULL
["url_key"]=>
string(6) "lounge"
["option_value"]=>
string(6) "Lounge"
}
[7]=>
array(3) {
["logo_path"]=>
NULL
["url_key"]=>
string(3) "gym"
["option_value"]=>
string(3) "Gym"
}
[8]=>
array(3) {
["logo_path"]=>
NULL
["url_key"]=>
string(8) "climbing"
["option_value"]=>
string(8) "Climbing"
}
[9]=>
array(3) {
["logo_path"]=>
NULL
["url_key"]=>
string(13) "crosstraining"
["option_value"]=>
string(13) "Crosstraining"
}
[10]=>
array(3) {
["logo_path"]=>
NULL
["url_key"]=>
string(11) "postworkout"
["option_value"]=>
string(12) "Post-workout"
}
[11]=>
array(3) {
["logo_path"]=>
NULL
["url_key"]=>
string(7) "cycling"
["option_value"]=>
string(7) "Cycling"
}
[12]=>
array(3) {
["logo_path"]=>
NULL
["url_key"]=>
string(8) "athletic"
["option_value"]=>
string(8) "Athletic"
}
[13]=>
array(3) {
["logo_path"]=>
NULL
["url_key"]=>
string(6) "sports"
["option_value"]=>
string(6) "Sports"
}
[14]=>
array(3) {
["logo_path"]=>
NULL
["url_key"]=>
string(6) "hiking"
["option_value"]=>
string(6) "Hiking"
}
[15]=>
array(3) {
["logo_path"]=>
NULL
["url_key"]=>
string(9) "overnight"
["option_value"]=>
string(9) "Overnight"
}
[16]=>
array(3) {
["logo_path"]=>
NULL
["url_key"]=>
string(6) "school"
["option_value"]=>
string(6) "School"
}
[17]=>
array(3) {
["logo_path"]=>
NULL
["url_key"]=>
string(5) "trail"
["option_value"]=>
string(5) "Trail"
}
[18]=>
array(3) {
["logo_path"]=>
NULL
["url_key"]=>
string(6) "travel"
["option_value"]=>
string(6) "Travel"
}
[19]=>
array(3) {
["logo_path"]=>
NULL
["url_key"]=>
string(5) "urban"
["option_value"]=>
string(5) "Urban"
}
}
The raw array converted to magento collection in construct
//$this->collectiondata is object of \Magento\Framework\Data\Collection
//$this->frameworkobject is object of \Magento\Framework\DataObject
protected function _construct()
{
$attrcollection = $this->getAttrCollection('all');
foreach($attrcollection as $attr)
{
$this->frameworkobject->setData($attr);
$this->collectiondata->addItem($this->frameworkobject);
}
$this->setCollection($this->collectiondata);
}
Collection set to pager (Have not used toolbar)
public function _prepareLayout()
{
parent::_prepareLayout();
if($this->getCollection()){
$pager = $this->getLayout()->createBlock('Magento\Theme\Block\Html\Pager', 'pager');
$pager->setCollection($collection);
$this->setChild('pager', $pager);
$this->getCollection()->load();
return $this;
}
}
}
public function getPagerHtml()
{
return $this->getChildHtml('pager');
}
Best Answer
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
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
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.