Closed nikitha12 closed 5 years ago
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();
};
Thank you @giorgiosjames . I have one more doubt. To style the default modal I am using bootstrap. I tried adding the stylesheet to the innerHTML of container div but its changing the style of all the blocks.
Should I use selector for adding and removing style to the Modal?
Found the solution. Thank you
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 :)
Thankyou @giorgiosjames .Code really works good in my usecase.
Doing away with the form and just using javascript to create/interface with the modal contents works well.
modal.setContent()
for appending the button andmyButton.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(); };
I tried this code in angular. It set content as [object HTMLDivElement] . Can someone help me?
Hey @niveth09,
If I had to guess, I'd say you may be accidently assigning an element object to container.innerHTML
rather than a string, as in the example. I would recommend changing your implementation to ensure the inputHtml
variable is a string representing valid HTML or you can use container.appendChild(inputHtml)
like we do with the button if it's already an element object in your code.
You may also want to use Angular/JQuery API to interact with the DOM instead, depending on your implementation. Something like in this fiddle.
If the above doesn't answer your question, I would recommend posting a question thread on Stack Overflow with a reproducible example and Angular+GrapesJS tags as you're unlikely to get further support on this closed Github Issue.
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.
It looks like this :