Skip to content

Instantly share code, notes, and snippets.

@delgreco
Last active December 15, 2015 04:09
Show Gist options
  • Save delgreco/5199794 to your computer and use it in GitHub Desktop.
Save delgreco/5199794 to your computer and use it in GitHub Desktop.
Here is an example of using a jQuery UI dialog in conjunction with other dynamic actions such as form validation using the jQuery validate plugin. Notice the need to append the form to the dialog (weird, huh?) in the dialog's 'open' event. These two snippets live in the same html file, but are given separately for the coloring.
<!-- portions of an HTML template -->
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="jquery-ui-1.8.13.complete.min.js"></script>
<script type="text/javascript" src="jquery.validate.1.8.1.min.js"></script>
<!-- forms must be kept outside of div to append to dialog -->
<form id="funding_form" method="post" action="<TMPL_VAR NAME=SCRIPT_NAME>">
<input type="hidden" name="request_id" value="<TMPL_VAR NAME=ID>">
<input type="hidden" name="action" value="approveEstimateFunding">
</form>
<div style="display: none;" id="funding_dialog">
<br>
Approve funding for <a href="<TMPL_VAR NAME=SCRIPT_NAME>?action=downloadFile&filename=<TMPL_VAR NAME=ESTIMATE_FILENAME>"><strong>this estimate</strong></a><br><br>
<TMPL_INCLUDE NAME="requests_web/billing_details.tmpl">
<br><br><br>
</div>
function promptForFunding() {
var $dialog = $('<div id="funding_prompt"></div>')
.html( $('#funding_dialog').html() )
.dialog({
autoOpen: false,
show: 'fade',
dialogClass: 'dialog',
height: 525,
width: 800,
modal: true,
title: 'Please Provide a Funding Source',
buttons: {
"Approve Funding": function(data) {
$('#funding_form').submit();
},
"Cancel": function(data) {
$dialog.dialog('close');
}
},
close: function(ev, ui) {
// handle for upper-right X or other dialog closure
$(this).remove();
},
open: function() {
// reinsert the dialog to the form
$('.dialog').appendTo( $('#funding_form') );
// binding in ready() is too soon
$('#recurring_same_as_one-time').change(toggleRecurringCharges);
$("input[name='enc_exists']").change(toggleTelecomEnc);
}
});
$dialog.dialog('open');
$('.ui-dialog-buttonset').css('width', '100%');
// layout the buttons
$('.ui-dialog-buttonpane button:eq(0)').css('float', 'left');
$('.ui-dialog-buttonpane button:eq(1)').css('float', 'right');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment