Object not passed to jQuery Dialog OnSubmit handler - javascript

I have the following HTML:
<div id="referenceResolverDialog" title="Resolve IDs">
<p>Please fill in a comma separated list of IDs.</p>
<form>
<fieldset>
<textarea name="referenceResolverTextArea" id="referenceResolverTextArea" rows="6" cols="54"></textarea>
</fieldset>
</form>
</div>
I created the jQuery dialog as follow:
var configParams = {
// some config properties
textAreaSelector: '#referenceResolverTextArea',
// some other config properties
}
var form, dialog;
dialog = $(configParams.dialogSelector).dialog({
autoOpen: false,
height: 220,
width: 350,
modal: true,
buttons: {
"Resolve": ResolveDialogData,
Cancel: function () {
dialog.dialog("close");
}
},
close: function () {
form[0].reset();
dialog.dialog("close");
}
});
form = dialog.find("form").on("submit", function (event) {
event.preventDefault();
ResolveDialogData(configParams);
});
The problem is that configParams is passed as a new object, not the object I have already. In the ResolveDialogData() method I have as follow:
function ResolveDialogData(configParams) {
alert(configParams); // returns [object Object]
alert(configParams.textAreaSelector); // returns undefined
}
What am I doing wrong?

If you're hitting that callback from your "Resolve" button, your configParams parameter will represent the Event object passed by your dialog.
Change
buttons: {
"Resolve": ResolveDialogData,
Cancel: function () {
dialog.dialog("close");
}
},
to
buttons: {
"Resolve": function(e) { // e is the object you were ending up with before
ResolveDialogData(configParams);
},
Cancel: function () {
dialog.dialog("close");
}
},

Related

How to pass Model to a Controller from a confirm Dialog written with JavaScript

I have a HTML page which has a model as follows:
#model ViewModel.Ekranlar.ModelVM
I call a Confirm Dialog written with JavaScript in the html page as in the following:
<script type="text/javascript">
$(document).ready(function () {
$(".confirmDialog").on("click", function (e) {
// e.preventDefault(); use this or return false
var url = $(this).attr('href');
$("#dialog-confirm").dialog({
autoOpen: false,
resizable: false,
height: 170,
width: 350,
show: { effect: 'drop', direction: "up" },
modal: true,
draggable: true,
buttons: {
"OK": function () {
$(this).dialog("close");
window.location = url;
}, "Cancel": function () {
$(this).dialog("close");
}
}
});
$("#dialog-confirm").dialog('open');
return false;
});
}); </script>
And the controller is as in the following:
public ActionResult DUDBaskaniBuro2GidenIptal(ModelVM model)
How can I pass ModelVM model to the Action in the controller?
Thanks for your helps.
Though not sure if you are using jquery modal or your custom one, but:
1) If you want to do full page postback, you may make the 'OK' button a 'submit' button type so that on click it will postback to the url mentioned in the form tag.
<input type="submit" value="OK" .../>
2) If you want to do partial postback, you may use something like:
"OK": function () {
$.post('#Url.Action("ActionMethod","ControllerName")',
<captured values via jquery/javascript as json>, //E.g. {"Id":1,"Name":"My Name"}
function(data){
//Code you want to do here
$(this).dialog("close"); //Just an example
window.location = data.url; //Just an example
});
}

How to create my own alert function with callback?

I have a Alert function defined as follow:
this.Alert = function(stitle, message) {
var myself = this;
if(window.alertDiv == undefined) {
window.alertDiv = $('<div></div>');
window.alertDiv.dialog({
modal: true,
autoOpen: false,
title: stitle,
height:300,
buttons: {
Ok: function() {
$(this).dialog('close');
}
}
});
}
window.alertDiv.dialog('option','title', stitle);
if(!window.alertDiv.dialog('isOpen')) {
window.alertDiv.dialog('open');
window.alertDiv.html(message);
} else {
window.alertDiv.html(window.alertDiv.html() + '<div style="text-align:center;">----------</div><br /><br />' + message);
window.alertDiv.css('height','300px');
}
}
Now I need create another Alert function with the same definition but with a callback. Does anybody how do I create the second Alert function by calling the current one while adding callback?
Accept a callback as a parameter, then execute said callback when you intend for it to be executed.
this.Alert = function(stitle, message, callback) {
and then:
window.alertDiv.dialog({
modal: true,
close: callback,

Why won't my jQuery-ui modal dialog display two custom buttons?

I have a generic Javascript function for displaying a jQuery-ui modal dialog with two buttons -- essentially "Continue" and "Cancel", though the text varies. I'm calling it in three places in my application. What's happening is that only the second button, the "Cancel" button is being displayed. Here's the function: (String.Format is an external function I always use since Javascript doesn't have one built-in - I know it isn't the problem.)
function DisplayModalDialog(titleText, bodyText, continueText, cancelText) {
//add the dialog div to the page
$('body').append(String.Format("<div id='theDialog' title='{0}'><p>{1}</p></div>", titleText, bodyText));
//create the dialog
$('#theDialog').dialog({
width: 400,
height: "auto",
modal: true,
resizable: false,
draggable: false,
close: function (event, ui) {
$('body').find('#theDialog').remove();
$('body').find('#theDialog').destroy();
},
buttons: [
{
text: continueText,
click: function () {
$(this).dialog('close');
return true;
},
text: cancelText,
click: function () {
$(this).dialog('close');
return false;
}
}]
});
return false;
}
And here's a snippet showing how I'm calling it:
if(CheckFormDataChanged() {
var changeTitle = "Data has been changed";
var changeText = "You have updated information on this form. Are you sure you wish to continue without saving?";
var changeContinue = "Yes, continue without saving";
var changeCancel = "No, let me save";
if (DisplayModalDialog(changeTitle, changeText, changeContinue, changeCancel)) {
if (obj) obj.click();
return true;
}
}
What's wrong with my function (or the call)?
UPDATE: Here's what I'm working with now. I realized that on one of the modal dialogs I didn't need a cancel button, just an acknowledge button:
function DisplayModalDialog(titleText, bodyText, continueText, cancelText, suppressCancel) {
var def = new $.Deferred();
//add the dialog div to the page
$('body').append(String.Format("<div id='theDialog' title='{0}'><p>{1}</p></div>", titleText, bodyText));
//create the button array for the dialog
var buttonArray = [];
buttonArray.push({ text: continueText, click: function () { $(this).dialog('close'); def.resolve(); } });
if (!suppressCancel) {
buttonArray.push({ text: cancelText, click: function () { $(this).dialog('close'); def.reject(); } });
}
//create the dialog
$('#theDialog').dialog({
... dialog options ...
close: function (event, ui) { $('body').find('#theDialog').remove(); },
buttons: buttonArray
});
return def.promise();
}
And the usage:
DisplayModalDialog(changeTitle, changeText, changeContinue, changeCancel, false)
.done(function () { if (obj) obj.click(); return true; })
.fail(function () { return false; });
Just to give you some context, obj is an ASP.Net Button being passed to the client-side function; if the function returns true, the server-side OnClick event is triggered; if false, it isn't. In this case, the server-side OnClick advances to the next tab in a TabContainer (among other things). What's happening is that it's moving to the next tab anyway, even though I'm returning false in the fail() function.
Your curly braces are off:
[{
text: continueText,
click: function () {
$(this).dialog('close');
return true;
}
}, {
text: cancelText,
click: function () {
$(this).dialog('close');
return false;
}
}]
As you have it, you only have one object in your buttons array.
I can't tell yet why the button doesn't display EDIT, ah, yes I can, there's a missing curly brace.
What I can tell you that your return lines simply won't work.
The dialog box gets displayed, your function returns immediately, and processing continues, so the click callback return values are completely ignored.
What you can do instead is return a promise:
function DisplayModalDialog(titleText, bodyText, continueText, cancelText) {
var def = $.Deferred();
...
buttons: [
{
text: continueText,
click: function () {
$(this).dialog('close');
def.resolve();
}
},
{ // ah - here's your button bug - a missing brace
text: cancelText,
click: function () {
$(this).dialog('close');
def.reject();
}
}
...
return def.promise();
}
with usage:
DisplayModalDialog(changeTitle, changeText, changeContinue, changeCancel)
.done(function() {
// continue was clicked
}).fail(function() {
// cancel was clicked
});

wait till jquery dialog box returns

I need to open a popup window on clicking a button and used jquery dialog for this.
$(document).ready(function(){
$("#dialog-form").dialog({
autoOpen : false,
height : 300,
width : 350,
modal : true,
buttons : {
"Add" : function() {
$("#tag1").text($("#textArea").val());
$(this).dialog("close");
},
Cancel : function() {
$(this).dialog("close");
}
},
close : function() {
$("#textArea").val("");
}
});
});
function openWindow(){
$("#dialog-form").dialog("open");
statement1;
statement2;
}
<button id="add" onclick="openWindow()">Add</button>
problem over here is when i click the button dialog box is opened, but before even i enter some text in the dialog box statement1 and statement2 are getting executed and then focus is coming to the dialog box.
How can i make the statement1 and statement2 to execute only after dialog box returns?
I don't want to add the statement1 and statement2 to the "Add" function. The reason for not adding the statements in the "Add" function is because i have multiple buttons and each of these should first open the dialog box and then will execute different set of statements.
Easy fix would be to use the close callback:
$(document).ready(function () {
$("#dialog-form").dialog({
autoOpen: false,
height: 300,
width: 350,
modal: true,
buttons: {
"Add": function () {
$("#tag1").text($("#textArea").val());
$(this).dialog("close");
},
Cancel: function () {
$(this).dialog("close");
}
},
close: function () {
$("#textArea").val("");
//statement1 -- won't fire until dialog is closed
//statement2 -- won't fire until dialog is closed
}
});
});
function openWindow() {
$("#dialog-form").dialog("open");
}
Another thing to consider would be $.Deferred
I have an example for you:
$(".selector").click(function () {
var dialog = $('<div title="Title"></div>').dialog({
open: function (event, ui) {
$.ajax({
url: 'www.google.com.br',
cache: false,
success: function (html) {
$(dialog).html(html);
},
error: function () {
$(dialog).remove();
alert("Some Error MSG");
}
});
},
close: function () {
$(dialog).remove();
},
resizable: false,
width: 500,
modal: true
});
});
In this case, the dialog is receiving the HTML result, only after it opens.
This can be achieved by the following way:-
$('#mydialog').dialog("open");
$('#mydialog').load('serverURL',server_data_variable, function() {
myfunction();
});
This will execute the function once the dialog loading is done.It will register the callback to be executed post dialog done.The variable server_data_variable is optional and is supposed to be used only if user wants to send some data otherwise it can be skipped as.well.
Solution 1: (same as solution from Aaron Blenkush)
$("#dialog-form").dialog({
autoOpen: false,
height: 300,
width: 350,
modal: true,
buttons: {
"Add": function () {
$("#tag1").text($("#textArea").val());
$(this).dialog("close");
//statement1 -- will fire only if "add" button is clicked
},
Cancel: function () {
$(this).dialog("close");
}
},
close: function () {
$("#textArea").val("");
//statement1 -- will fire after dialog is closed
}
});
Solution 2 is to make a promise:
const dialogPromise = new Promise(function (resolve, reject) {
$("#dialog-form").dialog({
autoOpen: false,
height: 300,
width: 350,
modal: true,
buttons: {
"Add": function () {
$("#tag1").text($("#textArea").val());
$(this).dialog("close");
resolve(true);
},
Cancel: function () {
$(this).dialog("close");
resolve(false);
}
},
close: function () {
$("#textArea").val("");
}
});
});
const addClicked = await dialogPromise;
Call callback function after "open" clause in the dialog setup.
modal: true,
resizable: false,
resize: 'auto',
close: dialogCloseFunction,
**open: function(){if(itemid) {showDailogSides(itemid);}** if(!siteParams[3]) {$(".detailSideClass").hide(); $(".addToChartinDialog").hide();}},
//hide: {effect: "fadeOut", duration: 5000}
show: { effect: "fade", duration: 1000 } //drop blind fade fold slide clip

Function doesn't work at second call

i have this function
function notify()
{
alert('oo');
$("#SuccessNotification").dialog({
bgiframe: true,
modal: true,
title: 'success',
buttons: {
Ok: function() {
$(this).dialog('close');
}
}
});
}
the alert works each time this function is called but the dialog is getting poped out only first time
You have to call the dialog with the open argument like this:
function notify()
{
alert('oo');
var elem = $("#SuccessNotification")
elem.dialog({
bgiframe: true,
modal: true,
title: 'success',
buttons: {
Ok: function() {
$(this).dialog('close');
}
}
});
elem.dialog('open');
}

Categories