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 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.
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) {
if (this.modal) {
this.modal.html(
$(data).html()
).trigger('contentUpdated');
return;
}
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);
use Magento\Backend\Block\Template;
/** @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 Etales\RequestAQuoteExtender\Controller\Adminhtml\Quote;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\View\Result\Layout;
use Magento\Framework\View\Result\LayoutFactory;
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="Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider">
<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="Magento\Ui\Component\Listing\Columns">
<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="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
<arguments>
<argument name="collections" xsi:type="array">
<item name="etales_requestaquoteextender_customer_grid_data_source" xsi:type="string">
Etales\RequestAQuoteExtender\Model\ResourceModel\Customer\Grid\Collection
</item>
</argument>
</arguments>
</type>
<virtualType name="Etales\RequestAQuoteExtender\Model\ResourceModel\Customer\Grid\Collection"
type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
<arguments>
<argument name="mainTable" xsi:type="string">customer_entity</argument>
<argument name="resourceModel" xsi:type="string">Magento\Customer\Model\ResourceModel\Customer</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?
I have figured the problem out, im feeling really stupid right now but i though adding the fix could help others in the future who is trying to achieve same kind of things (modals, ajax requests with html and javascript in it as response).
So the problem was that i missing the .applyBinding() method on the element where the grid is getting the loaded.
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) {
if (this.modal) {
this.modal.html(
$(data).html()
).applyBindings()
.trigger('contentUpdated');
return;
}
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')
.applyBindings()
.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);
}
};
};
});