自定义收集Magento 2中的分页

问题描述 投票:0回答:0

我是以一个对象的形式从第三方获取数据,我正在强行将一个对象转换为数组和集合。

我想应用数据分页,我试过使用核心分页逻辑,并将其设置为Magento 2,但有时计数不能正常工作,如如果没有数据存在,分页仍然显示页码下拉。

这是我做的代码。

protected function _prepareLayout()
    {
        parent::_prepareLayout();
        $this->pageConfig->getTitle()->set(__('Coupons'));


        if ($this->getCouponBookOrderHistory()) {
            $pager = $this->getLayout()->createBlock(
                'Magento\Theme\Block\Html\Pager',
                'test.news.pager'
            )->setAvailableLimit(array(10=>10,15=>15))->setShowPerPage(true)->setCollection(
                $this->getCouponBookOrderHistoryTotal()
            );
            $this->setChild('pager', $pager);
            $this->getCouponBookOrderHistory()->load();
        }
        return $this;
    }

    public function getPagerHtml()
    {
        return $this->getChildHtml('pager');
    }
    public function getCouponBookOrderHistory()
    {
        $customerId = $this->customer->getCustomer()->getAcwCustomerId(); 
        /*echo '<pre>';
        print_r($customerId);*/
        $result = array();

        $result = $this->coupons->getCouponBooks($customerId);

        //63498
        $result = (array)$result;
        //get values of current page
        $page=($this->getRequest()->getParam('p'))? $this->getRequest()->getParam('p') : 1;
        //get values of current limit
        $pageSize=($this->getRequest()->getParam('limit'))? $this->getRequest()->getParam('limit') : 10;

        $total = count( $result ); //total items in array    
        $limit = $pageSize; //per page    

        $totalPages = ceil( $total/ $limit ); //calculate total pages
        $page = max($page, 1); //get 1 page when $_GET['page'] <= 0
        $page = min($page, $totalPages); //get last page when $_GET['page'] > $totalPages
        $offset = ($page - 1) * $limit;
        if( $offset < 0 ) $offset = 0;
        $result = array_slice( $result, $offset, $limit );

        $master = array();
        array_push($master, $result);
        $collection=$this->_collectionFactory->create();
        // print_r($master); die;

        foreach($master as $row){
            $varienObject = new \Magento\Framework\DataObject(); 
            $varienObject->setData((array)$row);
            $collection->addItem($varienObject);
        }
        return $collection;
    }
public function getCouponBookOrderHistoryTotal()
    {
        $customerId = $this->customer->getCustomer()->getAcwCustomerId(); 
        /*echo '<pre>';
        print_r($customerId);*/
        $result = array();
        $result = $this->coupons->getCouponBooks($customerId);
        //63498


        $result = (array)$result;
        $master = array();
        array_push($master, $result);
        $collection=$this->_collectionFactory->create();
        // print_r($master); die;

        foreach($master as $row){
            $varienObject = new \Magento\Framework\DataObject(); 
            $varienObject->setData((array)$row);
            $collection->addItem($varienObject);
        }
        return $collection;
    }

在我的phtml文件中。

<?php if ($block->getPagerHtml()): ?>
    <div class="order-products-toolbar toolbar bottom"><?php echo $block->getPagerHtml(); ?></div>
<?php endif ?>

我创建了2个功能 getCouponBookOrderHistoryTotal() 用于获取总的正确计数,所以分页工作,其他使用array_slice从数组中获取数据,但我缺少一些东西。

谁能帮帮我,我怎么能直接把这个转换为应用过滤器的集合,我知道我试过这样。

// $collection->setPageSize($totalPages);
// $collection->setCurPage($page);

但它仍然没有适用于过滤器以及。

php magento magento2
© www.soinside.com 2019 - 2024. All rights reserved.