Magento 2 Form UI Component with AJAX Fieldset

ajaxmagento2uicomponent

I see that in the latest versions of Magento 2, the add/edit form for categories, blocks and pages are generated via a ui component form xml.
Here is the example for cms pages.
I want to use that for my own module like the big boys do.
I got most of the things figured out, but I wasn't able to add a fieldset that retrieves its contents via AJAX.
For example, I want to associate my custom entity "many to many" with the products, so I need a fieldset that contains nothing at first, but when is clicked it will retrieve the list of products via an ajax call.
I don't need the actual products grid code. All I need is a way to do an ajax call when clicking the fieldset header.

Best Answer

All I need is a way to do an ajax call when clicking the fieldset header.

Currently Fieldset Ui Component doesn't suport such ability: click on fieldset -> load content via AJAX .

1) You can implement it by yourself

Take a look on fieldset.js. It can be easily extended with custom behavior. In same maner as fieldset.js extends collapsible.js.

2) You can use InsertListing Ui Component

As was pointed above its js part is a insert-listing.js file.

Sample can be used like this:

<insertListing name="testListingInsert">
    <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">cms_page_listing</item>
        </item>
    </argument>
</insertListing>

Where to put this?

In any Form Ui Component under <fieldset>'s or <container>'s.

What does component do?

It uses namespace ns as a locator for some Ui Component, loads it, and instantiate in place.

cms_page_listing is a name of configured Listing Ui Component.

Related Topic