0

I have not any phtml file to set base url.

How can i call ajax form admin side in customjs magento2

form.xml

<field name="type">
    <argument name="data" xsi:type="array">
        <item name="options" xsi:type="object">Ktpl\BannerManagement\Model\Config\Source\Type</item>
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string">Type</item>
            <item name="visible" xsi:type="boolean">true</item>
            <item name="dataType" xsi:type="string">select</item>
            <item name="formElement" xsi:type="string">select</item>
            <item name="dataScope" xsi:type="string">type</item>
            <item name="component" xsi:type="string">Ktpl_BannerManagement/js/form/element/element</item>
        </item>
    </argument>
</field>

I am using custom js in form using ui component

In custom js

       define([
        'underscore',
        'uiRegistry',
        'Magento_Ui/js/form/element/select',
        'mage/url'
    ], function (_, uiRegistry, select, url) {
        'use strict';
        return select.extend({

            initialize: function (){
                var obj = this;
                var type= this._super().initialValue;
                this.makevisilibily(type);

                var defaultTemplate=uiRegistry.get('banner_form.areas.general.general.default_template');

                this.displayTemplateImage(defaultTemplate.value());

                defaultTemplate.on('update',function(){
                   obj.displayTemplateImage(defaultTemplate.value());
                })
                return this;

            },

            /**
             * On value change handler.
             *
             * @param {String} value
             */
            onUpdate: function (value) {
                this.makevisilibily(value);
                return this._super();
            },

            makevisilibily: function (value) {

                var field1 = uiRegistry.get('banner_form.areas.general.general.default_template');
                var field2 = uiRegistry.get('banner_form.areas.general.general.default_image');
                var field3 = uiRegistry.get("banner_form.areas.general.general.content");

                if (value == 1) {
                    field1.visible(true);
                    field2.visible(true);
                    //field3.visible(true);
                } else {
                    field1.visible(false);
                    field2.visible(false);
                   // field3.visible(false);
                }

                return this._super();
            },
              displayTemplateImage: function (defaultTemplate) {

                 var requestUrl = url.build('bannermanagement/banner/defaultTemplatePath');

                      jQuery.ajax({
                        url: requestUrl,
                        type: 'POST',
                        showLoader: true,
                        data: {
                            defaultTemplate: defaultTemplate,
                        },
                        /**
                         * @param {*} data
                         */
                        success: function (data) {
                          jQuery("#mp-demo-image").attr("src",data.imageSrc);
                        },
                        error: function (response) {
                            console.log(JSON.parse(JSON.stringify(response.responseText)));
                        }  
                    });
            }
        });
    });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.