PermaLink XPages - Example on how to generate dynamic controls for your fields at runtime07/20/2009 12:01 PM
Domino 8.5 - XPages
In my XPages Survey tool example that I demoed at Lotusphere, I showed how to dynamically generate fields at runtime for a document collection. The tool allows an administrator to create survey questions and select the control type and selection values.  Then the tool generates the fields for the questions when the user opens a survey.  The XPage utilizes a repeat control and panel control to accomplish this.  

For today's example I pulled out the specific code that generates the fields,  i.e. checkbox, list, radio button, edit box, etc.,  and simplified it so that it is clearer how this technique works....and just how easy it is to implement.  In the full survey tool example, this feature and technique easily gets lost.

You can download the sample database here.

The real key to this technique is the separation between presentation and data in XPages as compared with traditional Domino development.  With XPages, you can have multiple controls bound to the same Lotus Notes fields.  As long as only one of these controls is rendered at runtime, there is no issue with data integrity.

Below is a screen shot of the example generating a radio button group control :
Image:John's Blog - XPages - Example on how to generate dynamic controls for your fields at runtime


Here's how it is built:

On the DynamicFieldDemo XPage, the fields in the Field Attributes section control how the control is generated.  There is one control in the Field Preview section for each possible control type.  The Required field sets the validation required property on each control to true or false.  The Generate Field button reloads the page.  This is only required for this example because we need to regenerate the controls, which is not something that would be changing for your users.

The XPage layout looks like this:
Image:John's Blog - XPages - Example on how to generate dynamic controls for your fields at runtime


The visibility formula for the "Combo Box" control returns true if the user selected "DropDown" for the Control Type.  In this example I pick the value up from a session variable:
Image:John's Blog - XPages - Example on how to generate dynamic controls for your fields at runtime


The data Binding is set the same for all the controls.  In this example I am not using a Notes form as a datasource in order to keep it simple and instead just using session variables:
Image:John's Blog - XPages - Example on how to generate dynamic controls for your fields at runtime


The Validation is set, in this case for the combo box, from the All Properties->basics->required property:
Image:John's Blog - XPages - Example on how to generate dynamic controls for your fields at runtime

The possible selection values are set by exploding out the choices using a "," as the delimiter:
Image:John's Blog - XPages - Example on how to generate dynamic controls for your fields at runtime

Lastly, the Display Error control will display an error if the field is required and you pushed the Test Validation button.  The really cool thing here is that I have only one Display Error control which I compute what control it should report errors for.  This is accomplished by computing the "Show error messages for" property.  Since the control names I used match the selection names it is easy to compute which control is being displayed:
Image:John's Blog - XPages - Example on how to generate dynamic controls for your fields at runtime

As you can see, this opens up many possibilities.  I'm sure it won't be long before you discover other uses for this technique.

-John

Technorati:
This page has been accessed 2979 times. .
Comments :v

1. Nathan01/12/2010 12:02:02 PM


Can you perhaps give an example on how to compute the values for the checkbox group and radiobutton group controls?




2. John Mackey01/13/2010 05:25:15 AM
Homepage: http://www.jmackey.net


@Nathan. You can look at the source for the computed value for a combobox control and use that as a guideline. I understand that they will be adding in the interface for both radio buttons group control and checkbox group control in one of the next releases.

Here's an example of the computed values for a checkbox group control taken from my survey tool example. Look at the value for this tag "<xp:selectItems>":

<xp:checkBoxGroup id="CheckBox"
value="#{SurveyQuestion.Answer}" layout="pageDirection">
<xp:this.rendered><![CDATA[#{javascript:type=SurveyQuestion.getItemValueString("AnswerType");
return type=="CheckBox";}]]></xp:this.rendered>
<xp:this.required><![CDATA[#{javascript:req=SurveyQuestion.getItemValueString("Required");
return req=="Yes";}]]></xp:this.required>
<xp:selectItems>
<xp:this.value><![CDATA[${javascript:choices=SurveyQuestion.getItemValueString("AnswerChoices");
return @Explode(choices,",");}]]></xp:this.value>
</xp:selectItems>
</xp:checkBoxGroup>




3. Chris Parkhouse01/21/2010 11:20:15 PM


Hi John

Thank you for a great looking sample application in your survey tool. I have loaded it onto a 8.5.1 server and when following through any links I get the following error message
Error 500
HTTP Web Server: Command Not Handled Exception

I did sign the database - though I am on a DDN server so could not get access to check the server configurations

Here's a link to the database on the server
{ Link }

Any ideas of thoughts?

Thanks again

Chris




4. John Mackey01/22/2010 04:05:59 AM
Homepage: http://www.jmackey.net


@Chris, try opening the db in designer and doing a project - clean. This will recompile the XPages. The db was compiled with the 8.5 code stream. That's the first place I would look.

-John




5. Fredrik Norling02/01/2010 06:19:01 AM


Could you dynamically create other stuff in a xpage
Like Hotpots on a image?




Search
Partner with us
Need help on your XPages projects?

Talk to the experts! Read more...
XPage Examples
By Category
About Me
Downloads
My Links
Monthly Archive
Powered by
Blogsphere
Lotus Domino ND7 RSS News Feed RSS Comments Feed Geo URL netcraft RSS Validator Lotus Geek Chris. A. Brandlehner OpenNTF BlogSphere