1

I have displayed my one custom table in a grid format. Now, I want to display one other custom table in a grid format (Under different menu). But I am little bit confused, how to add code in di.xml file

Here is my di.xml which I have created for my first grid :

app/code/Ved/Mymodule/etc/di.xml :

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <virtualType name="Ved\Mymodule\Model\ResourceModel\News\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
        <arguments>
            <argument name="mainTable" xsi:type="string">ved_zipcodes</argument> <!-- here ved_zipcodes this is the db table name -->
            <argument name="resourceModel" xsi:type="string">Ved\Mymodule\Model\ResourceModel\News</argument> <!-- Path -->
        </arguments>
    </virtualType>
    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <!-- Here name value, we will use in mymodule_my_grid.xml layout file -->
                <item name="mymodule_news_grid_data_source" xsi:type="string">Ved\Mymodule\Model\ResourceModel\News\Collection</item> <!-- Collection path -->
            </argument>
        </arguments>
    </type>
</config>


How should I update my di.xml for displaying second grid in admin using the same custom module?

Any help would be highly appreciated. Thank you.

  • did you got any solution? – Piyush Jun 7 '17 at 13:18
  • No, I did not get. Let me know if you find anything. – vedu Jun 8 '17 at 5:15
1

To add second grid in same module you have to add collection data of second grid as second item of CollectionFactory node's collections argument. So in your case CollectionFactory will look like this -

<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <!-- Here name value, we will use in mymodule_my_grid.xml layout file -->
                <item name="mymodule_news_grid_data_source" xsi:type="string">Ved\Mymodule\Model\ResourceModel\News\Collection</item> <!-- Collection path -->
                <item name="mymodule_secondmodel_grid_data_source" xsi:type="string">Ved\Mymodule\Model\ResourceModel\SecondModel\Collection</item> <!-- Collection path added for second grid -->
            </argument>
        </arguments>
    </type>

The other nodes in the di.xml can be repeated for the second grid with change in unique_name for nodes (e.g - FilterPool, DataProvider, SerarchResult nodes) ,listing_file_name, Collection and ResourceModel path respectively different for each grid.

Here is the example of my di.xml having two grid -

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

    <!-- This nodes below are for first grid -->
    <virtualType name="firstGridFilterPool" type="Magento\Framework\View\Element\UiComponent\DataProvider\FilterPool"> <!-- here firstGridFilterPool is unique name for filterpool node and it is later user as Dataprovider node's filerPool argument's value -->
        <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="FirstGridDataProvider" type="Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider"> <!-- here FirstGridDataProvider is unique name for Dataprovider node  -->
        <arguments>
            <argument name="collection" xsi:type="object" shared="false">Namespace\Modulename\Model\ResourceModel\FirstGridModel\Collection</argument>
            <argument name="filterPool" xsi:type="object" shared="false">firstGridFilterPool</argument>
        </arguments>
    </virtualType>
    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="first_grid_data_source" xsi:type="string">Namespace\Modulename\Model\ResourceModel\FirstGridModel\Grid\Collection</item> <!-- Add Collection data for First Grid -->
                <item name="second_grid_data_source" xsi:type="string">Namespace\Modulename\Model\ResourceModel\SecondGridModel\Grid\Collection</item> <!-- Add Collection data for Second Grid -->
            </argument>
        </arguments>
    </type>
     <virtualType name="Namespace\Modulename\Model\ResourceModel\FirstGridModel\Grid\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
        <arguments>
            <argument name="mainTable" xsi:type="string">first_grid_tablename</argument>
            <argument name="resourceModel" xsi:type="string">Namespace\Modulename\Model\ResourceModel\FirstGridModel</argument>
        </arguments>
    </virtualType>

<!-- This nodes below are repeated for Second grid -->
    <virtualType name="SecondGridFilterPool" type="Magento\Framework\View\Element\UiComponent\DataProvider\FilterPool"> <!-- here SecondGridFilterPool is unique name for filterpool node and it is later user as Dataprovider node's filerPool argument's value -->
        <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="SecondGridDataProvider" type="Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider"> <!-- here SecondGridDataProvider is unique name for Dataprovider node  -->
        <arguments>
            <argument name="collection" xsi:type="object" shared="false">Namespace\Modulename\Model\ResourceModel\SecondGridModel\Collection</argument>
            <argument name="filterPool" xsi:type="object" shared="false">SecondGridFilterPool</argument>
        </arguments>
    </virtualType>

     <virtualType name="Namespace\Modulename\Model\ResourceModel\SecondGridModel\Grid\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
        <arguments>
            <argument name="mainTable" xsi:type="string">second_grid_tablename</argument>
            <argument name="resourceModel" xsi:type="string">Namespace\Modulename\Model\ResourceModel\SecondGridModel</argument>
        </arguments>
    </virtualType>
</config>

Hope it will help you.

0

You can do this by following code

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
        <arguments>
            <argument name="collections" xsi:type="array">
                <item name="first_data_source" xsi:type="string">Vendor\Module\Model\ResourceModel\First\Grid\Collection</item>
                <item name="second_data_source" xsi:type="string">Vendor\Module\Model\ResourceModel\Second\Grid\Collection</item>
            </argument>
        </arguments>
    </type>
    <!-- First -->
    <virtualType name="Vendor\Module\Model\ResourceModel\First\Grid\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
        <arguments>
            <argument name="mainTable" xsi:type="string">first_table_name</argument>
            <argument name="resourceModel" xsi:type="string">Vendor\Module\Model\ResourceModel\First</argument>
        </arguments>
    </virtualType>
    <!-- Second -->
    <virtualType name="Vendor\Module\Model\ResourceModel\Second\Grid\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
        <arguments>
            <argument name="mainTable" xsi:type="string">second_table_name</argument>
            <argument name="resourceModel" xsi:type="string">Vendor\Module\Model\ResourceModel\Second</argument>
        </arguments>
    </virtualType>
</config>

Hope it helps.

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.