6

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');
}

Attached the frontend view of pagination.enter image description here

  • Any help / guidance would be appreciated...! – mp196 Sep 1 '16 at 10:48
  • Has anybody faced this issue before if so please do guide me through...I am unable to find solution..! – mp196 Sep 15 '16 at 8:09
  • Try this order instead in your controller: $this->getCollection()->load(); $pager->setCollection($collection); $this->setChild('pager', $pager); – B00MER Sep 20 '16 at 18:02
3

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.

0

key code of pagination is

       protected $_yourFactory;
       public function __construct(\Magento\Framework\View\Element\Template\Context $context,VendorName\ModuleName\Model\yourFactory $yf,array $data = [])
       {
           $this->_yourFactory=$yf;
           parent::__construct($context, $data);
           $collection = $this->_yourFactory->create()->getCollection()
                ->setOrder('date', 'DESC');
                $this->setCollection($collection);
        }

        protected function _prepareLayout()
        {
        parent::_prepareLayout();
        /** @var \Magento\Theme\Block\Html\Pager */
        $pager = $this->getLayout()->createBlock(
            'Magento\Theme\Block\Html\Pager',
            'pager.identify.next.prev'
        );
        $pager->setLimit(5)
            ->setShowAmounts(true)
            ->setCollection($this->getCollection());
        $this->setChild('pager', $pager);
        $this->getCollection()->load();

        return $this;
        }

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

enter image description here

  • 1
    As notified in my question the collection is merged and then converted to magento collection object. FYI pagination works fine when i pass only one collection. The issue occurs when i merge collection ( Have tried both ways merging using array_merge as well as Union query). – mp196 Sep 19 '16 at 5:37
  • I read more than twice but could not get like "pagination works fine when i pass only one collection" :-). var_dump your merged collection and include in your question. may be it is helpful to debug. – Bilal Usean Sep 19 '16 at 5:50
  • apologies for not including that specific line.. :) and yes let me post the merged collection here. – mp196 Sep 19 '16 at 5:56
  • Please check the updated question bilal.. – mp196 Sep 19 '16 at 6:01
  • any suggestions or solution? – mp196 Sep 20 '16 at 5:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.