1

How to create a DataProvider that use in my grid?

I'm using uiComponent to create Grid.

1
  • It may be worth specifying that you want to create/define a CUSTOM DataProvider for a UI Component Grid Listing. Sep 17, 2019 at 5:37

2 Answers 2

5

Firstly, You need to create Interface, RepositoryInterface, Model, ResourceModel, Collection and Grid/Collection for your entity.

After that, in your etc/di.xml you need to have the following code:

    <preference for="Vendor\Namespace\Api\Data\TestInterface"
            type="Vendor\Namespace\Model\Test"/>
    <preference for="Vendor\Namespace\Api\TestRepositoryInterface"
            type="Vendor\Namespace\Model\ResourceModel\TestRepository"/>
    <preference for="Vendor\Namespace\Api\Data\TestSearchResultInterface"
            type="Magento\Framework\Api\SearchResults"/>
    <virtualType name="YourProfileGirdFilterPool" 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="YourProfileGirdDataProvider" type="Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider">
         <arguments>
            <argument name="collection" xsi:type="object" shared="false">Vendor\Namespace\Model\ResourceModel\Test\Collection</argument>
            <argument name="filterPool" xsi:type="object" shared="false">YourProfileGirdFilterPool</argument>
          </arguments>
      </virtualType>
      <type name="Vendor\Namespace\Model\ResourceModel\Test\Grid\Collection">
          <arguments>
             <argument name="mainTable" xsi:type="string">your_table_name</argument>
             <argument name="resourceModel" xsi:type="string">Vendor\Namespace\Model\ResourceModel\Test</argument>
          </arguments>
      </type>
      <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
          <arguments>
             <argument name="collections" xsi:type="array">
                <item name="test_listing_grid_data_source" xsi:type="string">Vendor\Namespace\Model\ResourceModel\Test\Grid\Collection</item>
             </argument>
          </arguments>
      </type>

The <preference> are added in the same etc/di.xml file. So, the concrete classes can implement their interfaces.

Now, you need to add the following code in your grid's ui_component xml file along with other necessary nodes/components. You can find M2 devdoc's references here

    <dataSource name="test_listing_grid_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string"> YourProfileGirdDataProvider</argument>
            <argument name="name" xsi:type="string">test_listing_grid_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"/>
                    <!--fix for fulltext search issue after clearing filter-->
                    <item name="storageConfig" xsi:type="array">
                        <item name="indexField" xsi:type="string">entity_id</item>
                    </item>
                </item>
            </argument>
        </argument>
    </dataSource>

Make sure to change your_listing_grid_data_source to match your data_source defined in your di.xml. And change any other instances of test and your to match your module.

3

http://devdocs.magento.com/guides/v2.2/ui_comp_guide/concepts/ui_comp_data_source.html - This guide contains relevant information

0

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