0

I have a problem with an extension and it is that it returns the SAP data sorted in ascending order and I need them in descending order.

The list is obtained from a helper that passes as a parameter the value of

_request-> getParam ('sort')

That value comes from the following line:

 protected function _prepareLayout()
{
    $this->pageConfig->getTitle()->set('SAP Online - ' . ucfirst(__($this->type)));


    if ($this->_request->getParam('filter') === '' && $this->_request->getParam('form_key') !== '') { 
        $this->_backendSession->unsetData('sap_gridfilter_' . $this->type);
    } else if ($this->_request->getParam('filter')) {
        $this->_backendSession->setData('sap_gridfilter_' . $this->type, $this->_request->getParams()); // 
    } else if (!!($this->_backendSession->getData('sap_gridfilter_' . $this->type))) {  // no tenemos params para filtrar, pero teniamos algo de antes
        $this->_request->setParams($this->_backendSession->getData('sap_gridfilter_' . $this->type));
    }

    $this->pageNumber = self::DEFAULT_PAGE_NUMBER;
    $this->itemsPage = self::DEFAULT_ITEMS_PAGE;

    if ($this->_request->getParam('page')) {
        if (!filter_var($this->_request->getParam('page'), FILTER_VALIDATE_INT)) {
            throw new \Exception('Page Number must be a number');
        }
        $this->pageNumber = $this->_request->getParam('page') - 1; //SAP WS pages start for 0
    }
    if ($this->_request->getParam('limit')) {
        $this->itemsPage = $this->_request->getParam('limit');
    }
    if ($this->_request->getParam('sort')) {
        $this->orderBy = $this->_request->getParam('sort');
    }
    if ($this->_request->getParam('dir')) {
        $this->orderDirection = $this->_request->getParam('dir');
    }

    return parent::_prepareLayout();
}

How do I change the value?

Method:

public function getRmaList()
{
    $data = $this->ws->getRmaCount
    (
        $this->fromRmaCode,
        $this->toRmaCode,
        $this->fromWebOrder,
        $this->toWebOrder,
        $this->fromRmaDate,
        $this->toRmaDate,
        $this->cardcode,
        $this->cardname,
        $this->repSales,
        $this->rmaStatus,
        $this->fromReturn,
        $this->toReturn,
        $this->motiveCode,
        $this->fromDelivery,
        $this->toDelivery
    );
    $count = ($data) ? json_decode($data, true) : null;
    $totalRecords = ($data && $count && $count['count']) ? $count['count'] : 0;

    // Update page if overflow page
    if ($this->itemsPage * ($this->pageNumber + 1) > $totalRecords && $this->pageNumber > 0) {
        $this->pageNumber = ceil($totalRecords / $this->itemsPage) - 1;
    }

    $docs = $this->ws->getRmaList
    (
        $this->getLocale(),
        $this->fromRmaCode,
        $this->toRmaCode,
        $this->fromWebOrder,
        $this->toWebOrder,
        $this->fromRmaDate,
        $this->toRmaDate,
        $this->cardcode,
        $this->cardname,
        $this->repSales,
        $this->rmaStatus,
        $this->fromReturn,
        $this->toReturn,
        $this->motiveCode,
        $this->fromDelivery,
        $this->toDelivery,
        $this->pageNumber,
        $this->itemsPage,
        $this->orderBy,
        $this->orderDirection
    );

    $this->docs = $this->collectionFactory->create();
    $this->docs->setTotalRecords($totalRecords);
    $this->docs->setPageSize($this->itemsPage);
    $this->docs->setCurPage($this->pageNumber + 1);


    $data = ($docs) ? json_decode($docs, true) : null;
    if ($data) {
        foreach ($data as $row) {
            $rowObj = $this->dataObjectFactory->create();
            $rowObj->setData($row);
            $this->docs->addItem($rowObj);
        }
    }

    return $this->docs;
}
  • 1
    $this->orderBy = "ASC" or "DESC" ....update Full code to work – MR.R DNATH Jun 15 '18 at 10:44
  • @MR.RDNATH added the method where the list is obtained, it costs to find it. – chdzma Jun 15 '18 at 11:28
  • $this->orderBy -- this must be an property , not a function check the initial value set in class file. it must be write like this protected $orderBy="Initial Value"; Find this. – MR.R DNATH Jun 15 '18 at 11:37
  • @MR.RDNATH I had put it, now I have put the whole method – chdzma Jun 15 '18 at 13:12
0

As per your requirement, i think you want to show pagination.... Try below code.....

 public function _prepareLayout() {
        $this->pageConfig->getTitle ()->set(__('Leader Data'));
        parent::_prepareLayout ();


        //echo $this->getleaderCollection()->getSelect()->__ToString();exit();
        if ($this->getleaderCollection()) {
            $pager = $this->getLayout()->createBlock('Magento\Theme\Block\Html\Pager','my.custom.pager');
            $pager->setAvailableLimit([5=>5,10=>10,20=>20,50=>50,100=>100]);
            $pager->setCollection($this->getleaderCollection());
            $this->setChild('pager', $pager);
        }
        return $this;


    }



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

         //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') : 5;

        $company = ($this->getRequest()->getParam('company'))?$this->getRequest()->getParam('company') : '';
        $country = $this->getRequest()->getParam('country')?$this->getRequest()->getParam('country') : 'UAE';
        $sectors = $this->getRequest()->getParam('sectors')?$this->getRequest()->getParam('sectors') : '';
        $ownership = $this->getRequest()->getParam('ownership')?$this->getRequest()->getParam('ownership') : '';

        if($company):
            $filterarray =    array(
                                array('like'=>'%'.$company.'%'), 
                                array('eq'=>''.$country.''), 
                                array('eq'=>''.$sectors.''), 
                                array('eq'=>''.$ownership.'')
                             );
        else:

            $filterarray = array(
                                array('eq'=>''.$company.''),
                                array('eq'=>''.$country.''), 
                                array('eq'=>''.$sectors.''), 
                                array('eq'=>''.$ownership.'')
                             );
        endif;



        $this->_filterdata = $this->_leadercollection->create()->getCollection()->addFieldToSelect('*')->addFieldToFilter(
                        array('company_name', 'country','sectors','ownership'),
                        $filterarray
            )->setOrder(
                'id',
                'asc'
            );




        $this->_filterdata->setPageSize($pageSize);
        $this->_filterdata->setCurPage($page);

        return $this->_filterdata;
    }

Change the collection and param value as per your requirement. Also display pager need to call in phtml file

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

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.