2

I need to create a simple grid in admin section, which contains all orders filtered by customer's email. I can't figure it out how I can fill the grid with custom data. All the examples found online create a custom table and then populate the grid with the data retrieved by that table. I don't need it. I just want to get all orders filtered by customer's email as I mentioned before. Thank you in advance.

  • Did you got answer ?how can I connect the grid with my custom collection? below answer is not fully this requirement – sumeet bajaj Aug 23 at 13:31
0

I know that it is not as simple that follow a tutorial but I recommend you to check the Magento 2 CMS core module.

You will see how Magento creates CMS block and pages grid.

Then copy/paste and adapt the impacted files to follow your needs.

You will learn more features this way and become more independent.

This is a non exhaustive list of useful files :

  • vendor/magento/module-cms/view/adminhtml/ui_component/cms_block_listing.xml
  • vendor/magento/module-cms/view/adminhtml/ui_component/cms_block_form.xml
  • vendor/magento/module-cms/view/adminhtml/layout/cms_block_edit.xml
  • vendor/magento/module-cms/view/adminhtml/layout/cms_block_index.xml
  • vendor/magento/module-cms/view/adminhtml/layout/cms_block_new.xml
  • vendor/magento/module-cms/view/adminhtml/templates/page/edit/form/renderer/content.phtml
  • vendor/magento/module-cms/Block/Adminhtml/Page/Grid/Renderer/Action/UrlBuilder.php
  • vendor/magento/module-cms/Block/Adminhtml/Block/Edit/*.php
  • vendor/magento/module-cms/Controller/Adminhtml/Block/*.php
  • vendor/magento/module-cms/Model/Block/DataProvider.php
  • vendor/magento/module-cms/Model/Block/Source/IsActive.php
  • vendor/magento/module-cms/Model/ResourceModel/Block/Grid/Collection.php
  • vendor/magento/module-cms/Model/ResourceModel/Block/Relation/Store/ReadHandler.php
  • vendor/magento/module-cms/Model/ResourceModel/Block/Relation/Store/SaveHandler.php
  • vendor/magento/module-cms/etc/di.xml

I hope it will be useful for you.

In order to populate the grid data, create your custom Grid Collection as Datasource or reuse the order grid collection.

<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
    <arguments>
        <argument name="collections" xsi:type="array">
            <item name="cms_page_listing_data_source" xsi:type="string">Magento\Cms\Model\ResourceModel\Page\Grid\Collection</item>
            <item name="cms_block_listing_data_source" xsi:type="string">Magento\Cms\Model\ResourceModel\Block\Grid\Collection</item>
        </argument>
    </arguments>
</type>

<type name="Magento\Cms\Model\ResourceModel\Block\Grid\Collection">
    <arguments>
        <argument name="mainTable" xsi:type="string">cms_block</argument>
        <argument name="eventPrefix" xsi:type="string">cms_block_grid_collection</argument>
        <argument name="eventObject" xsi:type="string">block_grid_collection</argument>
        <argument name="resourceModel" xsi:type="string">Magento\Cms\Model\ResourceModel\Block</argument>
    </arguments>
</type>

For orders in vendor/magento/module-sales/etc/di.xml:799

<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
    <arguments>
        <argument name="collections" xsi:type="array">
            <item name="sales_order_grid_data_source" xsi:type="string">Magento\Sales\Model\ResourceModel\Order\Grid\Collection</item>
            <item name="sales_order_invoice_grid_data_source" xsi:type="string">Magento\Sales\Model\ResourceModel\Order\Invoice\Grid\Collection</item>
            <item name="sales_order_shipment_grid_data_source" xsi:type="string">Magento\Sales\Model\ResourceModel\Order\Shipment\Grid\Collection</item>
            <item name="sales_order_creditmemo_grid_data_source" xsi:type="string">Magento\Sales\Model\ResourceModel\Order\Creditmemo\Grid\Collection</item>
            <item name="sales_order_view_invoice_grid_data_source" xsi:type="string">Magento\Sales\Model\ResourceModel\Order\Invoice\Orders\Grid\Collection</item>
            <item name="sales_order_view_shipment_grid_data_source" xsi:type="string">Magento\Sales\Model\ResourceModel\Order\Shipment\Order\Grid\Collection</item>
            <item name="sales_order_view_creditmemo_grid_data_source" xsi:type="string">Magento\Sales\Model\ResourceModel\Order\Creditmemo\Order\Grid\Collection</item>
        </argument>
    </arguments>
</type>

Feel free to ask some extra details.

  • Thank you very much. I had your same idea (check the core) and I found a solution for what I was looking for. Basically I was declaring my data source in a wrong way, I had to extend this class \Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult – Dougil Oct 3 '18 at 15:03
0

please go thorough this link its helpfull to create grid https://www.mageplaza.com/magento-2-module-development/create-admin-grid-magento-2.html

  • Hi, thanks for response. I've already seen this guide but I can't figure it out how he retrieves the data from the table. Anyway, how can I connect the grid with my custom collection? – Dougil Oct 1 '18 at 14:21

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.