[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)
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();
};
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)
@giorgiosjames Thanks, man this(https://github.com/artf/grapesjs/issues/2129#issuecomment-510734779) is work like a charm in my case.
Your answer save a lot of my time :)
Related Questions and Answers
Continue research with similar issue discussions.
Issue #3239
[Question] How to update attribute/properties dynamically from Component Script?
We have a use case where we want to insert charts dynamically using Highcharts Editor. I have created a component, which on drag and drop,...
Issue #1951
[QUESTIONS] change or add event on add component from block
is it possible ?, change the drag and drop event to on click when adding a new component, I want to add components by clicking the block th...
Issue #1409
Component Drag Event
I want to know how can I get that component from which element is dragged and drop to the other component. I need to add a class if compone...
Issue #1143
How to enable progamatic placeholder block insert
Hello grapesjs Team I succesfully created a component with traits and a block to insert it, when i drag n drop the block there is a green l...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.