jQuery UI Dialog with dynamic content not being centered properly - jquery-ui

I have a jQuery UI Dialog that is displayed when there are form errors.
Heres the code:
function alert_field_errors (errors)
{
$('<div></div>').dialog({
modal: true,
title: "Error",
open: function () {
$(this).html('The following are required:<br /><br />'+errors+'<br />Please complete these in order to continue.');
},
buttons: {
Ok: function () {
$(this).dialog("close");
}
}
});
}
the errors variable would contain something like First Name<br />Last Name<br />Email Address<br />
It works great, except for the fact that it is not centered. It seems to be positioning itself before the content is loaded, so that when it is displayed, the bottom margin of the dialog is closer to the bottom of the window compared to the top margin of the dialog.
I tried using the position option, but nothing seems to work.
Any help would be greatly appreciated.

Heres how I fixed it:
I created an empty div on the page with the id of error-dialog and gave it css display: none;.
Then I changed my code to this:
$('#error-dialog').html('The following are required:<br /><br /><i>'+$errors+'</i><br />Please complete these in order to continue.');
$('#error-dialog').dialog({
modal: true,
title: "Error",
buttons: {
Ok: function () {
$(this).dialog("close");
}
}
});
Now it is populating the dialog with content before display it, therefore allowing it to center itself properly.

Related

Jquery ui dialog buttons missing on first display of dialog

I am displaying a modal dialog to get "delete this service" or "cancel" options. When I first click on the button that opens the dialog the two buttons are missing. The red x is icon is displayed in the upper right corner and displays the help text without having to hover over the icon.
If I close it and reopen the dialog both buttons appear as they should. I'm not seeing any errors in the console.
I'm using jquery 3.1.1 and jquery ui 1.12.1. I'm calling the dialog out of a toolbar built in paramquery grid.
$("#dialog-confirm").dialog({
resizable: false,
height: "auto",
width: 400,
modal: true,
buttons: {
"Delete this service": function () {
var serviceid = grid.getRecId({rowIndx: rowIndx});
$.ajax($.extend({}, ajaxObj, {
context: grid,
dataType: 'text',
url: "/utilities/ajax_delete_service.php",
data: {serviceid: serviceid},
success: function () {
$("#services_grid").pqGrid("refreshDataAndView");
},
error: function () {
this.removeClass({rowIndx: rowIndx, cls: 'pq-row-delete'});
}
}));
$(this).dialog("close");
},
Cancel: function () {
grid.removeClass({rowIndx: rowIndx, cls: 'pq-row-delete'});
$(this).dialog("close");
}
}
});
UPDATED: I've created a fiddle that shows the problem. Run the fiddle, click on the top row of the grid and select "Delete Service" The buttons don't show. close the dialog. Click on "Delete Service" again and the buttons show.
The function for displaying the dialog is at the top of the fiddle.
JSFiddle

how do you open a jquery ui dialog window in parent/opener window?

I am trying to get a jquery ui dialog to open in a parent window from a child window. In other words, as the child can only have the dialog popup only with in its window's constraints, I would like to be able to use the contents of the dialog's popup in the constraints of the parent window.
I was trying to use a window.opener to tie in a link, but wasn't having much luck.
Here's the code that opens the window from a Backbone View HTML page:
popoutWindow = window.open("campaign/genericPopout.aspx", "search", "width=800, height=600, toolbar=0, status=0, menubar=0, scrollbars=1, resizable=1", true);
inside the genericPopout.apsx's call to a file that builds, with Backbone View, with this command,
new popupSearchView();
it calls the following Backbone View:
var popupSearchView = Backbone.View.extend({
events: {
"click #closeBtn" : "closePopupSerachView"
},
initialize: function() {
this.template = _.template("<div id='searchPopupView'>" +
"<div style='width: 100%; text-align: center;'>Hello World</div>" +
"<div style='text-align: center;'><input type='button' id='closeBtn' value='Close' /></div>" +
"</div>"
);
this.render();
},
render: function () {
formString = this.template();
$(formString).dialog({
autoOpen: true,
height:460,
width: 350,
title: "Search",
modal: false
})
return this;
},
closePopupSearchView: function () {
$(this).dialog("close");
}
});
It opens the popup dialog fine within the newly opened window, but it doesn't seem to work. In the parent window HTML, I have a div devoted to this dialog:
<div id="popupSearchViewDiv"></div>
and I tried setting the Backbone el to this div by this:
el: $("popupSearchViewDiv", window.opener.document)
yet this does not seem to work. Any thoughts on how I can accomplish this?
Thank You
I came up with a way to do this. I'm not sure how efficient it is, but it may be the only way.
In the opener window, I placed the code jquery-ui dialog:
function openSearchViewPopup() {
$("#popupSearchViewDiv").dialog({
autoOpen: true,
height:460,
width: 350,
title: "Search",
modal: false
}
With a predefined DIV in the index.aspx of:
<div id="popupSearchViewDiv"></div>
From the window opened from the index.aspx that holds the above function code, I call the function like this:
render: function () {
var htmlToWrite = this.template();
$(window.opener.docuemnt.getElementById("popupSearchViewDiv").append(htmlToWrite);
$window.opener.openSearchViewPopup();
}
This seems to allow the jquery-ui dialog popup to open in the window.opener window and to be used there.

jQuery UI - Button inside dialog keeps getting the focus

So I have a jQuery UI dialog with some buttons inside (full example here => http://jsfiddle.net/VLr5G/3/):
<div id="test">
<button>Btn 1</button>
<button>Btn 2</button>
<button>Btn 3</button>
</div>
What I want to do is force the focus on the "Close" button - I have tried applying the following code when the dialog opens:
open: function() {
$(this).parents('.ui-dialog-buttonpane button:eq(0)').focus();
}
Unfortunately the focus always keeps getting on the first button inside the dialog. Is this a bug, or am I missing something ?
Thanks a lot in advance for your help !
UPDATE
Okay so the answer from Stanley works fine with my first example... But try to change the version of jQuery UI => http://jsfiddle.net/VLr5G/10/
From what I could find so far, it worked until jQuery UI 1.10.0.
You are not getting the close button correctly.
You should do this instead:
$(document).ready(function() {
$('#test').dialog({
buttons: {
'Close': function() {$(this).dialog('close');}
},
open: function() {
$(this).parent().find('.ui-dialog-buttonpane button:eq(0)').focus();
}
});
});
Working jsfiddle: http://jsfiddle.net/GG7EP/2/
UPDATE
To make it work with jQuery 1.10.0 or above, call the button's focus function in focus event
$(document).ready(function() {
$('#test').dialog({
buttons: {
'Close': function() {$(this).dialog('close');}
},
focus: function() {
$(this).parent().find('.ui-dialog-buttonpane button:eq(0)').focus();
}
});
});
JsFiddle: http://jsfiddle.net/V3P4t/

JQuery UI Dialog Modal - Re-showing dialog can't edit text

I have a div:
<div id="mproEmailAccountPopUp"></div>
I declare a Dialog
var popupdiv = $( '#mproEmailAccountPopUp' );
popupdiv.dialog({
width: 650,
autoOpen: false,
modal: false
});
I then get some html form stuff (this works fine) from a server and update the dialog content:
function ShowEdit(accountId) {
$.ajax({
type: "POST",
async: false,
url: "<%=EmailHandler %>?action=GetEmailAccountEdit",
data: { accountId: accountId },
success: function (result) {
alert("Success GetEmailAccountEdit");
popupdiv.html(result);
popupdiv.dialog("open");
//popupdiv.dialog( "moveToTop" )
}
});
}
This works beautifully the first time, but if you press the close button, and then recall the function. The dialog is updated with the new html, but you can't type in ANY of the text boxes. I have narrowed it down to the modal overlay (as it works with modal:false). The z-index seems to be 1 less than the modal form, and I have even deleted (via firebug) the modal overlay div, but I can still not edit the text boxes.
If I turn modal:false then it works fine, modal:true is causing these problems.
As you can see, i have tried the "moveToTop" method, no effect.
I have also tried destroying the popup, and reinitialising it entirely from within the ShowEdit function.
Anyone have any clue about what I can do to fix this?

jQueryUI Modal open event & MVC.NET causing error ".dialog is not a function"

I hope someone can help as I'm at my wits end with this. What I'm trying to do is this.
"Draggable" Item is Dropped into a "Droppable" area (this works)
This posts the id of the item to my controller which returns the type of item it is (this works)
I pass the returned item name to a function which opens a modal and renders a partial view in the modal depending on the particular item.
The last bit is where the issue is. All the steps above work fine, the modal is popped and the partial view is rendered to the modal. But the button to close the dialog throws the error ".dialog is not a function" and after closing the modal using the 'x' in the corner subsequent attempts to open the modal will not work throwing a similar error.
Here's the example I'm working with to try get this working.
$(function () {
$('.draggable').draggable({ containment: '#imageboundry', revert: 'valid' });
$('#droppable').droppable({
drop: function (event, ui) {
$.ajax({
type: "POST",
url: '/Home/AddToCart/' + $(ui.draggable).attr("id"),
success: function (data) {
getItemType(data);
}
});
}
});
});
function getItemType(itemName) {
$('#dialogs').dialog({
open: function () {
$(this).load("AdditionalContent", { itemName: itemName }, function () {
alert("This happened");
});
},
modal: true,
resizable: false,
title: itemName,
width: 400,
autoOpen: false,
buttons: {
"Confirm": function () {
$(this).dialog('close');
}
}
});
}
This is my controller which returns the partial view to the modal
public PartialViewResult AdditionalContent(string itemName)
{
return PartialView("_" + itemName + "Attributes");
}
The close button works once I take out the open: function () { ... } bit and I can reopen the modal again and again but once I put this back in the error gets thrown. This is obviously the cause but cannot for the life of me figure out why.
Thanks in advance for your help and sorry for the very long post.
UPDATE:
I've attempted initializing the modal in document.ready and call it from my "drop" function in the first main function. From doing this I've narrowed it down to this line of code which loads the partial view from my controller. Without this line the functionality works. Any ideas on what is wrong with this.
$(this).load("AdditionalContent", { itemName: itemName }
I figured it out. Included in the partial views was a second call to the jQuery library which was added automatically when the view was created. Plus it was version 1.4.4 while I was using 1.5.1 straight from Google in the _Layout page. The second jQuery library was obviously breaking the functionality as there was a conflict between the two versions. It wasn't until I watched the FireBug console that I noticed the second loading of the 1.4.4 library.
Thanks to all for your help, feel like a bit of an idiot but lesson learned..... for now
could try this but i am not sure
$('#dialogs').dialog({
var self = this;
open: function () {
$(self).load("AdditionalContent", { itemName: itemName }, function () {
alert("This happened");
});
},

Resources