8

I have a grid UI component which is inside fieldset of some edited form. I need to pass an entity_id from the edit form to grid where an I can filter collection of some items by some value, and grid will show appropriate result. I created the grid component using an insertListing component.

<insertListing name="slide_grid">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="autoRender" xsi:type="boolean">true</item>
                <item name="source" xsi:type="string">slide</item>
                <item name="loading" xsi:type="boolean">true</item>
                <item name="dataScope" xsi:type="string">some_slider_slide_listing</item>
                <item name="externalProvider" xsi:type="string">${ $.ns }.some_slider_slide_listing_data_source</item>
                <item name="ns" xsi:type="string">some_slider_slide_listing</item>
                <item name="externalData" xsi:type="string">id</item>
                <item name="imports" xsi:type="array">
                    <item name="slider_id" xsi:type="string">${ $.provider }:data.entity_id</item>
                </item>
                <item name="exports" xsi:type="array">
                    <item name="slider_id" xsi:type="string">${ $.externalProvider }:params.slider_id</item>
                </item>
            </item>
        </argument>
    </insertListing>

For transfering data to external dataProvider I am using

<item name="exports" xsi:type="array">
                <item name="slider_id" xsi:type="string">${ $.externalProvider }:params.slider_id</item>
            </item>

Inside my external data provider i trying to get the data through request.

$this->request->getParam('slider_id');

But nothing. On frontend, i found that Magento sending ajax request with my parameter but I can't catch this in my DataProvider and filter the collection.

  • The approach I got from Magento 2 core code (For example in products CustomOptions form Modifier). But for some reason it is not working for me. – Mistery Aug 19 '17 at 11:12
  • Did you get any solution of the issue... I am facing same issue, please help if you have solved... – Ashish Raj Sep 27 '18 at 5:49
  • I made a same insertListing tag like you but the ajax request didn't have my parameter in the exports tag .... Did you find the solution? – thanhdv2811 Jan 13 at 16:55
1

For add insert listing by param of parent ui-component we can use below code.

Here externalProvider tag is for add source provider of the listing which we are inserting.

Here imports tag is used for import param of the current form data source

Here exports tag is used for export the current form data params to the listing which going to be insert.

<insertListing name="slide_grid">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="autoRender" xsi:type="boolean">true</item>
            <item name="ns" xsi:type="string">slide_grid</item><-- data source of the inserted listing -->
            <item name="externalProvider" xsi:type="string">colors_one_listing.colors_one_listing_data_source</item><!-- your insert listing data provider source -->
            <item name="imports" xsi:type="array">
                <item name="spd_id" xsi:type="string">${ $.provider }:data.slider_id</item>
            </item>
            <item name="exports" xsi:type="array">
                <item name="slider_id" xsi:type="string">${ $.externalProvider }:params.slider_id</item>
            </item>
        </item>
    </argument>
</insertListing>

Add join with relevant column to the current collection for make it use in two ways:

  1. Filter by grid dataSource > argument name > "dataProvider" > argument name > "data" > item name "config" > item name="filter_url_params" => item name > "slider_id".

For more detail check below code:

<dataSource name="..._listing_data_source">
    <argument name="dataProvider" xsi:type="configurableObject">
        <argument name="class" xsi:type="string">...\...\Ui\DataProvider\...\Grid\...DataProvider</argument>
        <argument name="name" xsi:type="string">..._listing_data_source</argument>
        <argument name="primaryFieldName" xsi:type="string">id</argument>
        <argument name="requestFieldName" xsi:type="string">slider_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="filter_url_params" xsi:type="array">
                    <item name="slider_id" xsi:type="string">*</item>
                </item>
                <item name="storageConfig" xsi:type="array">
                    <item name="indexField" xsi:type="string">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>
  1. Filter the data provider of inserted listing.

In the data provider add filter for this param:

$collection->addFieldToFilter('slider_id', $this->request->getParam('slider_id'));

I like to follow option 1.

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.