Building a Form in a Modal Panel with YUI
September 19th, 2007Building a Modal Filtering List with YUI
In this tutorial we will build a simple form and embed it in a modal panel.
Building the form
There are two approaches we could take towards building the form : dynamic or static. A dynamically loaded interface is built by the javascript executed on page load, a static interface lurks hidden inside the html source and is displayed when needed. For the sake of simplicity, we will be using a static interface in this tutorial.
Our form is quite simple. It consists of 3 tiers of options and a submit button:
<div id="filterpanelform" style="display:none"> <fieldset><legend>Type</legend> <input name="type" value="berry" type="checkbox">Berry <input name="type" value="citrus" type="checkbox">Citrus <input name="type"value="drupe" type="checkbox">Drupe <input name="type" value="Melon" type="checkbox">Melon </fieldset> <fieldset><legend> Size </legend> <input name="size" value="small" type="checkbox">Small <input name="size" value="medium" type="checkbox">Medium <input name="size" value="large" type="checkbox">Large </fieldset> <fieldset><legend> Color </legend> <input name="color" value="red" type="checkbox">Red <input name="color" value="orange" type="checkbox">Orange <input name="color" value="yellow" type="checkbox">Yellow <input name="color" value="green" type="checkbox">Green <input name="color" value="purple" type="checkbox">Purple <input name="color" value="pink" type="checkbox">Pink </fieldset> <button value="Filter" name="doFilter" type="button"id="doFilter">Filter</button> </div>
Notice the style=”display:none;” on the containing div? This is to hide the contents from view until our user calls the modal dialog.
Building the Modal Interface
Now we need to build a YUI panel to contain the filtering interface. Unlike the form elements, the panel we will build dynamically. The panel will be created on page load, but will remain hidden until called for.
// Instantiate a Panel from script
YAHOO.example.container.FormPanel = new YAHOO.widget.Panel("formPanel", { width:"320px", visible:false, draggable:true, close:true} );
YAHOO.example.container.FormPanel.setHeader("Form Panel");
//populate the panel with the form we've hidden in the HTML
YAHOO.example.container.FormPanel.setBody( document.getElementById("filterpanelform").innerHTML );
YAHOO.example.container.FormPanel.render("container");


Previous Post
Next Post

4 Comments
September 20th, 2007 at 9:56 am
Why did you use the hidden div instead of simply creating the panel directly from source?
September 20th, 2007 at 9:58 am
I don’t like the way YUI panels may flash unstyled content if you do that. The javascript is executed after the page is rendered. If I’m building a form dynamically, this results in the panel flashing on page load.
September 21st, 2007 at 10:57 am
[...] For a quick refresher, I recommend you read: http://www.ajaxbestiary.com/2007/09/19/building-a-form-in-a-modal-panel-with-yui/ [...]
May 28th, 2009 at 10:16 am
Thanks for the article, it helped me deal with setting the panel’s body content. What I really wanted was to have a javascript function in the panel body that makes a web service call for the content but luckily I can get away with dumping the contents in a hidden div for the time being.
Thanks again.
Leave a Reply