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');
}
$this->getCollection()->load(); $pager->setCollection($collection); $this->setChild('pager', $pager);