Yesterday I blogged about using jQuery to create automatic hot links around certain words in your page text. While working on that demo, I tried to use the jQuery Dialog widget. I ran into some trouble and gave up, but returned to it this morning to see if I could get it working.
Unfortunately things didn't work out too well for me. I ran into more than one issue, but I had some great help from the folks on the jQuery team. I'd like to specifically call out Rey Bengo, Richard Worth, and Scott Jehl for helping me out. Things actually got ridiculously easy after their support, but I figured a blog post may help others as well. So let's get started!
The first thing you have to know about the jQuery UI stuff is that it is not included in your core jQuery download. You have to download a few additional files before you start using the UI items. You can start at the jQuery UI Download page and select either the latest stable release, the preview release, or use the custom downloader. If you are a newbie like me, I strongly urge you to select the preview release. 99% of the trouble I had today went away when I simply switched from the latest stable 1.5.x release to the 1.6 RC. Let me repeat this. Do not download 1.5. Life will come to an end - chaos will reign - and you will pull out what remains of your hair. From what I've been told, 1.6 will be released very shortly (very very shortly apparently), so it's not like your working with a beta copy. Once you download the zip, the next thing you will notice is that you don't simply have one simple JS file. Instead you have a complete package of JavaScript, CSS, and HTML files. There are two important folders here. The UI folder is your javascript collection. The themes folder is your skins folder. For my example code I took both folders and copied them to my demo:
Alright, so next we will create a very simple html page. I based my code on the dialog documentation page with one big change. Their demo code runs the dialog immediately when the page loads. I think in almost all cases folks will want to show a dialog based on some event. Here is what I came up with:
2
3 <head>
4 <script src="js/jquery.js"></script>
5 <script src="js/ui/ui.core.js"></script>
6 <script src="js/ui/ui.dialog.js"></script>
7 <script>
8 function showDialog(){
9 $("#example").dialog();
10 return false;
11 }
12 </script>
13
14 </head>
15
16 <body>
17
18 <p>
19 <a href="" onclick="return showDialog()">Show the Dialog</a>
20 </p>
21
22 <div id="example" class="flora" title="This is my title">I'm in a dialog!</div>
23
24 </body>
25 </html>
So how do we fix this? First off, to make the dialog hidden on load, we can simply add a bit of CSS to it:
You can see it in action here: http://www.coldfusionjedi.com/demos/jqueryuitest2/test2.html Don't forget you can view source to see the entire page.
None of this worked for when I was using 1.5, so be sure to use the release candidate of 1.6!
Ok, so with things working well now, I decided to go crazy and try to apply a custom theme. I went to ThemeRoller and grabbed one of the presets - Mint Choc. As before, the zip you get is a bit big and may confuse you. (Well, it confused me at first.) After extracting the zip, the folder you want is jquery-ui-themeroller/theme. I renamed theme to mint and put it within my themes folder. I then changed the CSS to
You can see this in action here: http://www.coldfusionjedi.com/demos/jqueryuitest2/test3.html
All in all, I'm pretty pleased with the UI stuff, once I got past my brain cramps with it. I seem to remember having similar issues with the core jQuery library as well and I'm over that as well, so maybe it's simply something that I have to get used to.
I'll leave off with one more quick tip from Richard Worth. Remember my trouble with hiding the div on page load? He pointed out that you can also dynamically create dialogs with strings:
Comment 1 written by Raymond Camden on 1 February 2009, at 3:56 PM
Comment 2 written by Scott Jehl on 1 February 2009, at 3:59 PM
We'll get this all documented and tutorialized on the UI site once 1.6 final is released.
Also, there are a bunch of resources available that can help minimize confusion for those getting started with jQuery UI. Check the articles, blogs and feeds listed on the UI site's support page: http://jqueryui.com/support
Cheers!
Comment 3 written by Raymond Camden on 1 February 2009, at 3:59 PM
You will also see the theme: Swanky Purse, a name I just absolutely love. Anyway, view source to see what I changed, and I promise a follow up post later.
Comment 4 written by Akira on 2 February 2009, at 8:13 AM
close: function(ev, ui) {
$(this).remove();
}
Comment 5 written by Joel Cox on 2 February 2009, at 8:46 AM
<script>
$(document).ready(function() {
$("#example").dialog({autoOpen:false});
$("#opener").click(function(event){
event.preventDefault();
$("#example").dialog("open");
});
});
</script>
</head>
<body>
<p> <a href="javascript:;" id="opener">Show the Dialog</a> </p>
<div style="display: none;" id="example" title="This is my title">I'm in a dialog!</div>
I had some issues with IE6 (Windows) not functioning correctly when binding to an anchor tag without adding the event.preventDefault() function call.
Comment 6 written by Raymond Camden on 2 February 2009, at 8:48 AM
Comment 7 written by Ralph Whitbeck on 2 February 2009, at 10:21 AM
Comment 8 written by Rey Bango on 2 February 2009, at 10:26 AM
@Ralph: Thank you for the kind words. The jQuery & jQuery UI teams are all about community. We're glad to help.
Comment 9 written by Seth on 2 February 2009, at 10:42 AM
Comment 10 written by Raymond Camden on 2 February 2009, at 10:56 AM
Well, I didn't cover _all_ the options, but you get the idea. ;)
Comment 11 written by Susan on 26 March 2009, at 2:49 AM
Comment 12 written by Jerry on 22 April 2009, at 5:33 AM
Comment 13 written by Raymond Camden on 22 April 2009, at 6:53 AM
Comment 14 written by Mikee on 14 May 2009, at 12:37 AM
There's a better solution here in which you can enable the box to open multiple times AND pass events and options within the dialog() function's parameters:
http://blog.nemikor.com/category/jquery-ui/jquery-...
Comment 15 written by thaianhduc on 14 May 2009, at 3:34 AM
I had small problem with the dialog. I had a table to nest my stuff and a div to show as a dialog. Everything seems work fine except that, the dialog was opened under the table. It looked terrible.
Here is the code:
<div id="viewBookDialog" title="Chi tiet book">
chi tiet ve sach herer test first
</div>
<table style="width:100%;">
....stuff here ....
</table>
The viewBookDialog is support to show the dialog.
Instantate dialog:
$("#viewBookDialog").dialog({
bgiframe: true,
autoOpen: false,
height: 300,
modal: true,
position: 'top'
});
Does anyone have any clue? Please help.
Comment 16 written by Raymond Camden on 14 May 2009, at 6:39 AM
Comment 17 written by thaianhduc on 14 May 2009, at 9:34 PM
Thank for your consideration.
Comment 18 written by Henrik on 21 October 2009, at 5:57 AM
Cheers
Comment 19 written by Amy on 7 November 2009, at 4:54 PM
Comment 20 written by Indialike on 20 January 2010, at 6:33 AM
Thanks for posting.
[Add Comment] [Subscribe to Comments]