Skip to content

Customer grid uiComponent in slide out modal (Admin)

Situation

I want to open a modal in the backend when i click on a button in the admin bar above in the page. In this modal a customer grid uiComponent needs to be shown.

The Problem

The uiComponent is getting loaded through ajax but the data is never rendered.
When i click the button in “Duplicate for other customer” a new slide-out modal will be opened. In the network tab the data for the grid that needs to be parsed will be fetched through AJAX. But the response will never be added to the customer grid and the customer grid will never be shown in the modal. The only thing i see is the spinner during the ajax request for the grid data.

Code

quote.js: This file handle’s the modal.

define([
    'jquery',
    'Magento_Ui/js/modal/modal',
    'mage/url'
], ($, modal, urlBuilder) => {

    return (config, element) => {
        window.keepMultiModalWindow = true;
        window.requestAQuoteExtender = {
            modal: null,
            duplicate: () => {
                this.doRequest(
                    config.duplicateUrl,
                    'POST',
                    {
                        quote_id: config.quote_id
                    }
                ).done((response) => {
                    window.location.assign(response.redirect);
                });
            },
            duplicateOtherCustomer() {
                this.doRequest(config.duplicateOtherCustomerUrl)
                    .done((data, textStatus, transport) => {
                        const dataHtml = $(data);
                        this.openModal(dataHtml[1]);
                    })
            },
            openModal(data) {
console.log('openModal');
console.log(data);
                if (this.modal) {
console.log('modal exists');
                    this.modal.html(
                        $(data).html()
                    ).trigger('contentUpdated');

                    return;
                }

console.log('Create modal')
                var modalOptions = {
                    title: 'Choose customer',
                    modalClass: 'magento',
                    type: 'slide',
                    buttons: [{
                        text: $.mage.__('Close'),
                        class: 'action- scalable back',
                        click: () => {
                            this.closeModal(this);
                        }
                    }],
                    close: () => {
                        this.closeModal(this);
                    }
                }

                this.modal = $(data).modal(modalOptions);
                this.modal.modal('openModal').trigger('contentUpdated');
            },
            closeModal(modal) {
                $('body').trigger('processStop');
                modal.closeModal();
            },
            doRequest: (url, method = 'GET', data = null) => {
                const ajaxObj = {
                    url: url,
                    method: method,
                    data: data,
                    dataType: 'html',
                    showLoader: true
                }

                if (data !== null) {
                    ajaxObj.data = data;
                }

                return $.ajax(ajaxObj);
            }
        };
    };
});

quote.phtml: This file will be added on the admin page where i can click on a button to open the modal.

<?php declare(strict_types=1);

/**
 * Merxtrade - Request A Quote Extender
 *
 * This module adds extra functionality to the Amasty Request A Quote module.
 *
 * Copyright © E-Tales B.V. 2024-present. All rights reserved.
 */

use MagentoBackendBlockTemplate;

/** @var Template $block */
?>
<script type="text/x-magento-init">
{
    "#duplicate": {
        "etalesRequestAQuoteExtender": {
            "duplicateUrl": "<?= $block->getUrl('etales_quoteextender/quote/duplicate') ?>",
            "duplicateOtherCustomerUrl": "<?= $block->getUrl('etales_quoteextender/quote/customergridui') ?>",
            "quote_id": "<?= $this->getRequest()->getParam('quote_id') ?>"
        }
    }
}
</script>

Controller/Adminhtml/Quote/CustomerGridUi.php: This file handle’s the ajax request for the customer grid uiComponent.

<?php

declare(strict_types=1);

namespace EtalesRequestAQuoteExtenderControllerAdminhtmlQuote;

use MagentoFrameworkAppActionHttpGetActionInterface;
use MagentoFrameworkViewResultLayout;
use MagentoFrameworkViewResultLayoutFactory;

class CustomerGridUi implements HttpGetActionInterface
{
    public function __construct(
        private readonly LayoutFactory $layoutFactory,
    ) {
    }

    public function execute()
    {
        /** @var Layout $layoutFactory */
        return $this->layoutFactory->create();
    }
}

view/adminhtml/layout/etales_quoteextender_quote_customergridui.xml: This file inserts the uiComponent for the above controller.

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <container name="content">
            <uiComponent name="etales_requestaquoteextender_customer_grid" />
        </container>
    </body>
</page>

etales_requestaquoteextender_customer_grid.xml: This is the uiComponent that is used.

<?xml version="1.0"?>
<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">
                etales_requestaquoteextender_customer_grid.etales_requestaquoteextender_customer_grid_data_source
            </item>
        </item>
    </argument>
    <settings>
        <spinner>etales_requestaquoteextender_customer_grid_columns</spinner>
        <deps>
            <dep>etales_requestaquoteextender_customer_grid.etales_requestaquoteextender_customer_grid_data_source</dep>
        </deps>
    </settings>
    <dataSource name="etales_requestaquoteextender_customer_grid_data_source" component="Magento_Ui/js/grid/provider">
        <settings>
            <updateUrl path="mui/index/render"/>
        </settings>
        <dataProvider name="etales_requestaquoteextender_customer_grid_data_source"
            class="MagentoFrameworkViewElementUiComponentDataProviderDataProvider">
            <settings>
                <primaryFieldName>entity_id</primaryFieldName>
                <requestFieldName>id</requestFieldName>
            </settings>
        </dataProvider>
    </dataSource>
    <listingToolbar name="listing_top">
        <bookmark name="bookmarks"/>
        <columnsControls name="columns_controls"/>
        <filters name="listing_filters"/>
        <paging name="listing_paging"/>
    </listingToolbar>
    <columns name="etales_requestaquoteextender_customer_grid_columns" class="MagentoUiComponentListingColumns">
        <column name="entity_id" sortOrder="10">
            <settings>
                <filter>text</filter>
                <label translate="true">ID</label>
            </settings>
        </column>
        <column name="company" sortOrder="20">
            <settings>
                <filter>text</filter>
                <label translate="true">Company</label>
            </settings>
        </column>
        <column name="firstname" sortOrder="30">
            <settings>
                <filter>text</filter>
                <label translate="true">Firstname</label>
            </settings>
        </column>
        <column name="lastname" sortOrder="40">
            <settings>
                <filter>text</filter>
                <label translate="true">Lastname</label>
            </settings>
        </column>
        <column name="email" sortOrder="50">
            <settings>
                <filter>text</filter>
                <label translate="true">E-mail address</label>
            </settings>
        </column>
    </columns>
</listing>

di.xml In this file i have created the collection for the customer grid.

<?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="MagentoFrameworkViewElementUiComponentDataProviderCollectionFactory">
    <arguments>
        <argument name="collections" xsi:type="array">
            <item name="etales_requestaquoteextender_customer_grid_data_source" xsi:type="string">
                EtalesRequestAQuoteExtenderModelResourceModelCustomerGridCollection
            </item>
        </argument>
    </arguments>
</type>
    <virtualType name="EtalesRequestAQuoteExtenderModelResourceModelCustomerGridCollection"
                 type="MagentoFrameworkViewElementUiComponentDataProviderSearchResult">
        <arguments>
            <argument name="mainTable" xsi:type="string">customer_entity</argument>
            <argument name="resourceModel" xsi:type="string">MagentoCustomerModelResourceModelCustomer</argument>
        </arguments>
    </virtualType>
</config>

If i add the uiComponent through the layout xml of the page where i want to open the modal it will be correctly rendered on the page. But with my code above it just doesn’t render the grid in the modal but all other things like getting the data for the grid etc is working.

Anybody who can point me in the right direction or sees what i am doing wrong?

Thanks in advance :).

Kindly regards!