Issue #2129Opened July 11, 2019by nikitha1216 reactions

[Question] how to append a button inside default modal and catch it's click event

Question

I want a form inside a modal (using the default modal ), on click of the submit button make an ajax call. I have created a new component which pops a modal on drag and drop.

I am able to insert the form in the modal

It looks like this :

<img width="1001" alt="Screen Shot 2019-07-11 at 10 22 17 AM" src="https://user-images.githubusercontent.com/36627122/61023023-cf1a4180-a3c5-11e9-8fcb-3da790ff9d61.png">

but I don't know how to track the submit button click. I tried writing OnSubmit() in a Script and added to the modal content, its not working.

`const domc = editor.DomComponents;
  const defaultType = domc.getType('default');
domc.addType("test-type", {
    model: defaultModel.extend(
      {
        defaults: Object.assign({}, defaultModel.prototype.defaults, {
          type      : "test-type",
          id:"popup",
          droppable : false,
          resizable : true,
          tagName:"popup",
        }),
        getAttrToHTML: function() {
          var attr = defaultType.model.prototype.getAttrToHTML.apply(this, arguments);
          delete attr.onmousedown;
          var testmarker = this.get("testmarker");
          if(testmarker)
            attr.testmarker = testmarker;
          return attr;
        }
      }),
    view: defaultType.view.extend({
      tagName: "button",
      events: {
        "dblclick": "openModal",
      },
      initialize: function(o) {
        defaultType.view.prototype.initialize.apply(this, arguments);
        this.listenTo(this.model, "change:testmarker", this.updateMarker);
        this.listenTo(this.model, "dblclick active", this.openModal);
      },
      
      updateMarker: function() {
        var testmarker = this.model.get("testmarker");
        this.$el.attr("testmarker", testmarker);
      },

      openModal: function(e) {
        const modal = editor.Modal;
        modal.open({
          title: '<br>Create Identity<br>',
          content: `
          <div class="container"> 
          <br>
              <form onsubmit="formSubmit()">
              <div class="form-group">
              <label>URL</label>
               <input type="text" class="form-control" id="url" placeholder="http://test-data/" name="url">
              </div>
              <br>
              <div class="form-group">
              <label>Identity </label>
               <input type="text" class="form-control" id="address" placeholder="Enter Identity Address" name="address">
              </div>
              <br>
              <button type="submit" class="btn btn-danger">Submit</button>
              </form>
              <br>
              </div>`,
        });
      },
    })
  });`

Is my approach correct? Should I do it in a different way ?

Answers (3)

giorgiosjamesJuly 12, 201912 reactions

Doing away with the form and just using javascript to create/interface with the modal contents works well. modal.setContent() for appending the button and myButton.onclick = () => {} to catch its click event. Here's some example code:

function openModal() {
  const pfx = editor.getConfig().stylePrefix;
  const modal = editor.Modal;

  const container = document.createElement('div');

  const inputHtml = `<div class="form-group">
  <label>URL</label>
   <input type="text" class="form-control" placeholder="http://test-data/" name="url" id="urlInput">
  </div>
  <br>
  <div class="form-group">
  <label>Identity </label>
   <input type="text" class="form-control" placeholder="Enter Identity Address" name="address" id="idInput">
  </div>`;

  const btnEdit = document.createElement('button');
  btnEdit.innerHTML = 'Submit';
  btnEdit.className = pfx + 'btn-prim ' + pfx + 'btn-import';
  btnEdit.onclick = function() {
    const urlInputElement = document.getElementById('urlInput');
    const idInputElement = document.getElementById('idInput');

    const urlVal = urlInputElement.value;
    const idVal = idInputElement.value;

    // here is where you put your ajax logic
    myAjaxCallFunction(urlVal, idVal);

    modal.close();
  };

  modal.setTitle('Create Identity');
  container.innerHTML = inputHtml;
  container.appendChild(btnEdit);
  modal.setContent(container);
  modal.open();
};
artfJuly 14, 20192 reactions

The point here is that content accepts HTML nodes (not only HTML strings) so you can have any HTMLElement with any attached event (forms with submits, buttons with clicks, etc.) so the approach proposed is totally valid (thanks @giorgiosjames)

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.