In this article I will improve on the solution I posted last week to my form submittal problem and actually provide a more elegant and robust solution.
The problem
I mean to say that Dave Leedy posed a problem to me last Friday which gave me pause for thought. Obviously he had come across this issue before. “Marky have you tried to do a partialRefresh inside of the bootstrap modal dialog?”. I had not and I knew that I had plans to…..
Then it struck me – of course the partialRefresh can’t work in the dialog because the dialog is outside of the form. The partialRefresh relies on the ability to post data back to the server (via the form) and the collect the response and re-draw. Of course it isn’t going to work.
The solution
As I said the other day – the bootstrap dialog is not purposefully removing itself from the form, it is moving itself to the end of the DOM tree. Why? I couldn’t say but it seems to be pretty consistent. So on the assumption it is nothing to do with the “form” I figured the easiest solution would be to put it back *into* the form once it was visible.
As we saw before when the modal is created there are two elements moved to the end – the dialog itself and the faded out background
Using a bit of jQuery magic we select those two DIVs and “append()” them back into the form. Using the append() method they are always added to the end of the target DOM element container. You should add this code to every page where you have a dialog. Add this code to a CSJS library and include it on every page where you have a bootstrap modal.
$(document).ready( function(){ $('.modal').on('shown.bs.modal', function () { $('FORM').append($('.modal-backdrop, .modal-scrollable')); }) })
What does that mean exactly?
- In the ‘shown.bs.modal’ event of the selected $(‘.modal’)
- Get the $(‘FORM’) and .append() into it the two selected $(‘.modal-backdrop, .modal-scrollable’) DIVs
and then you get this
Once the modal is then inside the dialog we can post it to our hearts content.
We can prove that with a simple @Random within the dialog box and a quick code snippet in firebug
We add a “random” class to the computed text so that we can select it
We then load up the dialog
Run the JavaScript to cause a partialRefresh
Et voila a partialRefresh with a dialog. It also works of course with XPage controls and buttons and the like but this was just a simple way to demonstrate how to do it
Conclusion
Once we understand the DOM changes which are being made for a bootstrap dialog we are able to use jQuery to easily manipulate the resulting DOM to be able to make our application work.
ADDENDUM – updated
Please see the jQuery Event Delegation blog post to understand why this approach is limited and needed to be updated….new solution provided
[…] if there haven’t already been enough (too many?) references to Marky, take a look at this post if you have a modal that needs to perform partial […]