1

HI I want to know that i need to create a module that will show the report of sold product and their generated revenue.

so I want to know that can we fetch custom table data and display it in admin grid anyone have any idea about it kindly please provide some guidance.

I think I needs to fetch this table data

sales_order

enter image description here

2
  • This is related to creating a admin grid. Refer here to do the same. Commented Feb 26, 2020 at 9:13
  • hi @pramod have you checked ?
    – Msquare
    Commented Feb 28, 2020 at 10:06

1 Answer 1

2

First create a module using this link click here

OR Download Sample Module

and put name like this

enter image description here

now replace this files

Emipro/Custom/Model/ResourceModel

Job.php

<?php

namespace Emipro\Custom\Model\ResourceModel;

use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class Job extends AbstractDb
{
    protected function _construct()
    {
        $this->_init('sales_order', 'entity_id');
    }
}

Emipro/Custom/etc

di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">


    <!--    For Job     -->
    <virtualType name="EmiproCustomJobFilterPool" type="Magento\Framework\View\Element\UiComponent\DataProvider\FilterPool">
        <arguments>
            <argument name="appliers" xsi:type="array">
                <item name="regular" xsi:type="object">Magento\Framework\View\Element\UiComponent\DataProvider\RegularFilter</item>
                <item name="fulltext" xsi:type="object">Magento\Framework\View\Element\UiComponent\DataProvider\FulltextFilter</item>
            </argument>
        </arguments>
    </virtualType>
    <virtualType name="EmiproCustomJobFilterPool" type="Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider">
        <arguments>
            <argument name="collection" xsi:type="object" shared="false">Emipro\Custom\Model\ResourceModel\Job\Collection</argument>
            <argument name="filterPool" xsi:type="object" shared="false">EmiproCustomJobFilterPool</argument>
        </arguments>
    </virtualType>
    <type name="Emipro\Custom\Model\ResourceModel\Job\Grid\Collection">
        <arguments>
            <argument name="mainTable" xsi:type="string">sales_order</argument>
            <argument name="eventPrefix" xsi:type="string">emipro_custom_job_grid_collection</argument>
            <argument name="eventObject" xsi:type="string">job_grid_collection</argument>
            <argument name="resourceModel" xsi:type="string">Emipro\Custom\Model\ResourceModel\Job</argument>
        </arguments>
    </type>

    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="emipro_custom_job_listing_data_source" xsi:type="string">Emipro\Custom\Model\ResourceModel\Job\Grid\Collection</item>
            </argument>
        </arguments>
    </type>


</config>

Emipro/Custom/view/adminhtml/ui_component

emipro_custom_job_listing.xml

<?xml version="1.0"?>
<!-- https://magento.stackexchange.com/questions/97291/explanation-of-ui-component-grid-in-magento-2 -->
<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">emipro_custom_job_listing.emipro_custom_job_listing_data_source</item>
            <item name="deps" xsi:type="string">emipro_custom_job_listing.emipro_custom_job_listing_data_source</item>
        </item>
        <item name="spinner" xsi:type="string">custom_job_columns</item>
    </argument>
    <dataSource name="emipro_custom_job_listing_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">EmiproCustomJobFilterPool</argument>
            <argument name="name" xsi:type="string">emipro_custom_job_listing_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">entity_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="cacheRequests" xsi:type="boolean">false</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">emipro_custom_job_listing</item>
                    </item>
                </item>
            </argument>
        </bookmark>
        <component name="columns_controls">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="columnsData" xsi:type="array">
                        <item name="provider" xsi:type="string">emipro_custom_job_listing.emipro_custom_job_listing.custom_job_columns</item>
                    </item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/controls/columns</item>
                    <item name="displayArea" xsi:type="string">dataGridActions</item>
                </item>
            </argument>
        </component>

        <filterSearch name="fulltext">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="provider" xsi:type="string">emipro_custom_job_listing.emipro_custom_job_listing_data_source</item>
                    <item name="chipsProvider" xsi:type="string">emipro_custom_job_listing.emipro_custom_job_listing.listing_top.listing_filters_chips</item>
                    <item name="storageConfig" xsi:type="array">
                        <item name="provider" xsi:type="string">emipro_custom_job_listing.emipro_custom_job_listing.listing_top.bookmarks</item>
                        <item name="namespace" xsi:type="string">current.search</item>
                    </item>
                </item>
            </argument>
        </filterSearch>
        <filters name="listing_filters">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="columnsProvider" xsi:type="string">emipro_custom_job_listing.emipro_custom_job_listing.custom_job_columns</item>
                    <item name="storageConfig" xsi:type="array">
                        <item name="provider" xsi:type="string">emipro_custom_job_listing.emipro_custom_job_listing.listing_top.bookmarks</item>
                        <item name="namespace" xsi:type="string">current.filters</item>
                    </item>
                    <item name="templates" xsi:type="array">
                        <item name="filters" xsi:type="array">
                            <item name="select" xsi:type="array">
                                <item name="component" xsi:type="string">Magento_Ui/js/form/element/ui-select</item>
                                <item name="template" xsi:type="string">ui/grid/filters/elements/ui-select</item>
                            </item>
                        </item>
                    </item>
                    <item name="childDefaults" xsi:type="array">
                        <item name="provider" xsi:type="string">emipro_custom_job_listing.emipro_custom_job_listing.listing_top.listing_filters</item>
                        <item name="imports" xsi:type="array">
                            <item name="visible" xsi:type="string">emipro_custom_job_listing.emipro_custom_job_listing.custom_job_columns.${ $.index }:visible</item>
                        </item>
                    </item>
                </item>

                <item name="observers" xsi:type="array">
                    <item name="column" xsi:type="string">column</item>
                </item>
            </argument>
            <filterSelect name="store_id">
                <argument name="optionsProvider" xsi:type="configurableObject">
                    <argument name="class" xsi:type="string">Magento\Cms\Ui\Component\Listing\Column\Cms\Options</argument>
                </argument>
                <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                        <item name="provider" xsi:type="string">${ $.parentName }</item>
                        <item name="imports" xsi:type="array">
                            <item name="visible" xsi:type="string">componentType = column, index = ${ $.index }:visible</item>
                        </item>
                        <item name="dataScope" xsi:type="string">store_id</item>
                        <item name="label" xsi:type="string" translate="true">Store View</item>
                        <item name="captionValue" xsi:type="string">0</item>
                    </item>
                </argument>
            </filterSelect>

        </filters>

        <massaction name="listing_massaction">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="selectProvider" xsi:type="string">emipro_custom_job_listing.emipro_custom_job_listing.custom_job_columns.ids</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/tree-massactions</item>
                    <item name="indexField" xsi:type="string">id</item>
                </item>
            </argument>

            <action name="is_delete">
                <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                        <item name="type" xsi:type="string">delete</item>
                        <item name="label" xsi:type="string" translate="true">Delete</item>
                        <item name="url" xsi:type="url" path="custom/index/massDelete">
                        </item>
                        <item name="confirm" xsi:type="array">
                            <item name="title" xsi:type="string" translate="true">Delete items</item>
                            <item name="message" xsi:type="string" translate="true">Are you sure you wan't to delete selected items?</item>
                        </item>
                    </item>
                </argument>
            </action>

        </massaction>

        <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">emipro_custom_job_listing.emipro_custom_job_listing.listing_top.bookmarks</item>
                        <item name="namespace" xsi:type="string">current.paging</item>
                    </item>
                    <item name="selectProvider" xsi:type="string">emipro_custom_job_listing.emipro_custom_job_listing.custom_job_columns.ids</item>
                </item>
            </argument>
        </paging>
    </container>

    <columns name="custom_job_columns">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="storageConfig" xsi:type="array">
                    <item name="provider" xsi:type="string">emipro_custom_job_listing.emipro_custom_job_listing.listing_top.bookmarks</item>
                    <item name="namespace" xsi:type="string">current</item>
                </item>
                <item name="childDefaults" xsi:type="array">
                    <item name="fieldAction" xsi:type="array">
                        <item name="provider" xsi:type="string">emipro_custom_job_listing.emipro_custom_job_listing.custom_job_columns</item>
                        <item name="target" xsi:type="string">startEdit</item>
                        <item name="params" xsi:type="array">
                            <item name="0" xsi:type="string">${ $.$data.rowIndex }</item>
                            <item name="1" xsi:type="boolean">true</item>
                        </item>
                    </item>
                    <item name="storageConfig" xsi:type="array">
                        <item name="provider" xsi:type="string">emipro_custom_job_listing.emipro_custom_job_listing.listing_top.bookmarks</item>
                        <item name="root" xsi:type="string">columns.${ $.index }</item>
                        <item name="namespace" xsi:type="string">current.${ $.storageConfig.root}</item>
                    </item>
                </item>
            </item>
        </argument>


        <selectionsColumn name="ids">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="resizeEnabled" xsi:type="boolean">false</item>
                    <item name="resizeDefaultWidth" xsi:type="string">55</item>
                    <item name="indexField" xsi:type="string">entity_id</item>
                </item>
                    <item name="sortOrder" xsi:type="number">0</item>
            </argument>
        </selectionsColumn>

        <column name="entity_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">textRange</item>
                    <item name="sorting" xsi:type="string">asc</item>
                    <item name="label" xsi:type="string" translate="true">Id</item>
                    <item name="sortOrder" xsi:type="number">20</item>
                </item>
            </argument>
        </column>

        <column name="state">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string" translate="true">state</item>
                    <item name="sortOrder" xsi:type="number">20</item>
                </item>
            </argument>
        </column>

    </columns>
</listing>

Now Add Simple Column to this listing file. column name must be match with the sales_order table column name.

Column name in table sales_order must be match with column name

<column name="table_column_name">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="filter" xsi:type="string">text</item>
            <item name="label" xsi:type="string" translate="true">Lable Name</item>
            <item name="resizeEnabled" xsi:type="boolean">true</item>
        </item>
    </argument>
</column>

enter image description here

3
  • column name must be match with the sales_order table column name. can you please explain this
    – Pramod
    Commented Feb 27, 2020 at 6:49
  • is there anyway to filter a order grid as to show pending order status only show in the grid Commented Nov 3, 2020 at 11:23
  • open this file Emipro\Custom\Model\ResourceModel\Job\Grid\Collection and add this function public function _beforeLoad() { // add here to filter your status $this->addFieldToFilter("status", "pending"); return $this; }
    – Msquare
    Commented Nov 3, 2020 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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