2

I am trying to make a grid on the admin panel with a custom collection (custom dataSource).

The problem I am facing is that it doesnt paginate properly (all records are in one page but the pagination component is shown in the grid). Also I cannot export it as CSV, it gives error:

Exception #0 (Exception): Not registered handle myvendor_orderreport_listing_data_source

enter image description here

My Controller looks like this:

    <?php
namespace MyVendor\Reports\Controller\Adminhtml\Orderreport;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\View\Result\PageFactory;

/**
 * Class Index
 * @package MyVendor\Reports\Controller\Adminhtml\Orderreport
 */
class Index extends Action implements HttpGetActionInterface
{
    /**
     * @var string
     */
    const ADMIN_RESOURCE = 'MyVendor_Reports::myreports_order_report';

    /**
     * @var PageFactory
     */
    public $resultPageFactory;

    /**
     * Index constructor.
     * @param Context $context
     * @param PageFactory $resultPageFactory
     */
    public function __construct(Context $context, PageFactory $resultPageFactory)
    {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }

    /**
     * Order Report Grid page
     *
     * @return \Magento\Framework\View\Result\Page
     */
    public function execute()
    {
        $resultPage = $this->resultPageFactory->create();
        $resultPage->setActiveMenu(self::ADMIN_RESOURCE);
        $resultPage->getConfig()->getTitle()->prepend(__('Order Report'));

        return $resultPage;
    }
}

ui Data Provider:

    <?php

namespace MyVendor\Reports\Ui\DataProvider\Listing;

use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\Search\SearchCriteriaBuilder;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\View\Element\UiComponent\DataProvider\Reporting;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Magento\Store\Model\StoreManager;

/**
 * Class OrderReportListing
 * @package MyVendor\Reports\Ui\DataProvider\Listing
 */
class OrderReportListing extends \Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider
{
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    private $_storeManager;

    /**
     * @var \Magento\Sales\Model\ResourceModel\Order\Collection
     */
    private $_collection;

    /**
     * OrderReportListing constructor.
     * @param CollectionFactory $collectionFactory
     * @param StoreManager $storeManager
     * @param string $name
     * @param string $primaryFieldName
     * @param string $requestFieldName
     * @param Reporting $reporting
     * @param SearchCriteriaBuilder $searchCriteriaBuilder
     * @param RequestInterface $request
     * @param FilterBuilder $filterBuilder
     * @param array $meta
     * @param array $data
     */
    public function __construct(
        CollectionFactory $collectionFactory,
        StoreManager $storeManager,
        $name,
        $primaryFieldName,
        $requestFieldName,
        Reporting $reporting,
        SearchCriteriaBuilder $searchCriteriaBuilder,
        RequestInterface $request,
        FilterBuilder $filterBuilder,
        array $meta = [],
        array $data = []
    ) {
        $this->_storeManager = $storeManager;
        $this->_collection = $collectionFactory->create();
        parent::__construct(
            $name,
            $primaryFieldName,
            $requestFieldName,
            $reporting,
            $searchCriteriaBuilder,
            $request,
            $filterBuilder,
            $meta,
            $data
        );
    }

    public function getData()
    {
        $this->_collection
            ->addFieldToFilter('created_at',
                array(
                    'from' => date("Y-m-d H:s:i", strtotime('2020-01-01' . ' 23:59:59')),
                    'to' => date("Y-m-d H:s:i", strtotime('2020-07-02' . ' 23:59:59'))
                ));

        $storeIds = array();
        $allowedStoreIds = array_keys($this->_storeManager->getStores());
        $storeIds = array_intersect($allowedStoreIds, $storeIds);
        if (empty($storeIds)) {
            $storeIds = $allowedStoreIds;
        }
       $storeIds = array_values($storeIds);
        $this->_collection->addFieldToFilter('store_id', array('in' => $storeIds));

        $this->_collection->getSelect()->join(
            array('order_payment' => 'sales_order_payment'),
            'order_payment.parent_id=main_table.entity_id'
        )->columns(array('increment_id', 'base_tax_amount', 'base_shipping_amount', 'order_currency_code', 'grand_total', 'status'))
            ->where('order_payment.cc_trans_id is not null');


        foreach ($this->_collection->getItems() as $item) {
            $item['order_currency_code3'] = $item['order_currency_code'];
            $item['order_currency_code2'] = $item['order_currency_code'];
        }

        return $this->_collection->toArray();
    }

}

Layout (app/code/MyVendor/Reports/view/adminhtml/layout/myreports_orderreport_index.xml):

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  layout="admin-1column"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="styles"/>
    <body>
        <referenceContainer name="content">
            <uiComponent name="myvendor_orderreport_listing"/>
        </referenceContainer>
    </body>
</page>

uiComponent:

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
 
    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="provider" xsi:type="string">myvendor_orderreport_listing.myvendor_orderreport_listing_data_source</item>
            <item name="deps" xsi:type="string">myvendor_orderreport_listing.myvendor_orderreport_listing_data_source</item>
        </item>
        <item name="spinner" xsi:type="string">myvendor_orderreport_listing_columns</item>
    </argument>

    <dataSource name="myvendor_orderreport_listing_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">MyVendor\Reports\Ui\DataProvider\Listing\OrderReportListing</argument>
            <argument name="name" xsi:type="string">myvendor_orderreport_listing_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">increment_id</argument>
            <argument name="requestFieldName" xsi:type="string">id</argument>
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
                    <item name="update_url" xsi:type="url" path="mui/index/render"/>
                    <item name="storageConfig" xsi:type="array">
                        <item name="indexField" xsi:type="string">increment_id</item>
                    </item>
                </item>
            </argument>
        </argument>
        <argument name="data" xsi:type="array">
            <item name="js_config" xsi:type="array">
                <item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
            </item>
        </argument>
    </dataSource>

    <container name="listing_top">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="template" xsi:type="string">ui/grid/toolbar</item>
                <item name="stickyTmpl" xsi:type="string">ui/grid/sticky/toolbar</item>
            </item>
        </argument>
        <bookmark name="bookmarks">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="storageConfig" xsi:type="array">
                        <item name="namespace" xsi:type="string">myvendor_orderreport_listing</item>
                    </item>
                </item>
            </argument>
        </bookmark>
        <paging name="listing_paging">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="storageConfig" xsi:type="array">
                        <item name="provider" xsi:type="string">myvendor_orderreport_listing.myvendor_orderreport_listing.listing_top.bookmarks</item>
                        <item name="namespace" xsi:type="string">current.paging</item>
                    </item>
                    <item name="selectProvider" xsi:type="string">myvendor_orderreport_listing.myvendor_orderreport_listing.myvendor_orderreport_listing_columns.ids</item>
                </item>
            </argument>
        </paging>
        <exportButton name="export_button"/>
    </container>

    <columns name="myvendor_orderreport_listing_columns">
        <selectionsColumn name="ids">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="indexField" xsi:type="string">increment_id</item>
                </item>
            </argument>
        </selectionsColumn>
        <column name="increment_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortable" xsi:type="string">false</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="align" xsi:type="string">left</item>
                    <item name="label" xsi:type="string" translate="true">Name</item>
                </item>
            </argument>
        </column>
        <column name="order_currency_code">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortable" xsi:type="string">false</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="align" xsi:type="string">left</item>
                    <item name="label" xsi:type="string" translate="true">CurrencyIsoCode</item>
                </item>
            </argument>
        </column>
        <column name="base_shipping_amount">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortable" xsi:type="string">false</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="align" xsi:type="string">left</item>
                    <item name="label" xsi:type="string" translate="true">ECS__Shipping_Total__c</item>
                </item>
            </argument>
        </column>
        <column name="order_currency_code2">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortable" xsi:type="string">false</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="align" xsi:type="string">left</item>
                    <item name="label" xsi:type="string" translate="true">CurrencyIsoCode</item>
                </item>
            </argument>
        </column>
        <column name="base_tax_amount">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortable" xsi:type="string">false</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="align" xsi:type="string">left</item>
                    <item name="label" xsi:type="string" translate="true">ECS__Tax_Total__c</item>
                </item>
            </argument>
        </column>
        <column name="class_name">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortable" xsi:type="string">false</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="align" xsi:type="string">left</item>
                    <item name="label" xsi:type="string" translate="true">CustTaxGrp</item>
                </item>
            </argument>
        </column>
        <column name="customer_taxvat">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortable" xsi:type="string">false</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="align" xsi:type="string">left</item>
                    <item name="label" xsi:type="string" translate="true">CustVATNum</item>
                </item>
            </argument>
        </column>
        <column name="order_currency_code3" >
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortable" xsi:type="string">false</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="align" xsi:type="string">left</item>
                    <item name="label" xsi:type="string" translate="true">CurrencyIsoCode</item>
                </item>
            </argument>
        </column>
        <column name="grand_total">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortable" xsi:type="string">false</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="align" xsi:type="string">left</item>
                    <item name="label" xsi:type="string" translate="true">ECS__Total_Order_Amount__c</item>
                </item>
            </argument>
        </column>
        <column name="status">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortable" xsi:type="string">false</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="align" xsi:type="string">left</item>
                    <item name="label" xsi:type="string" translate="true">Status</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

Any suggestion is appreciated Thanks

Your Answer

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

Browse other questions tagged or ask your own question.