TeamSpace
Quality vs. quantity Microphone
Home Who We Are What We Do What We've Done Why Work For Us Blog Contact Us

Populate XPage combobox choices from notes multi-value field

Bob Laing - January 22 2013 04:30:00 PM


I try to avoid hard coding web select options whenever possible, preferring to create some type of configuration document to hold select options.  This means that when the choices change at some point in the future, only the configuration document that holds the values needs to be changed, and not the code.

Typically the configuration document will be a simple document that contains a description of the page, which will be used as the key to retrieve the values, along with a multi-value field that contains what will be displayed as the choices in the XPage select field.

In the following example I'll use a comboBox Core Control and fill the select values from a configuration document.

To start off, the XPage will include the following code to display a label for the comboBox field.

<xp:label for="date-range-id" value="Class Date Range:"></xp:label>


Next, include code to display the combo box on the XPage.  If the XPage is editing an existing document, the existing field value (in this case field classdaterange) will be displayed as selected on the page.  The selected items are retrieved from Server Side JavaScript which is contained in an SSJS script library (app_settings.jss). This function is called getKWValues and is passed the key value to identify the document to retrieve the correct values from.  In this case, the key value is called class_date_ranges.

<xp:comboBox id="date-range-id">
   <xp:this.value><![CDATA[#{compositeData.document.classdaterange}]]></xp:this.value>
   <xp:selectItems>
           <xp:this.value><![CDATA[#{javascript:getKWValues('class_date_ranges');}]]></xp:this.value>
   </xp:selectItems>
</xp:comboBox>


The SSJS script library is declared to the XPage as a Resource:

<xp:this.resources>
   <xp:script
           src="/app_settings.jss"
           clientSide="false">
   </xp:script>
</xp:this.resources>


And the script library contains the getKWValues function that returns a Java vector to the comboBox control.  The function will save the keywords in a session scope variable so if the lookup is reused, the session scope variable is checked first.

function getKWValues(category:string):java.util.Vector {
   try {

           var lookupKey:string = category;
           
           // check if the keywords are already retrieved
           var keywords:java.util.Vector = sessionScope.get(lookupKey);        
           if (null != keywords) return keywords;
   
           // get view to lookup keywords
           var lookupView:NotesView = database.getView('keywords.lookup');
           lookupView.refresh();
           lookupView.setAutoUpdate(false);
           
           // get keywords profile values
           var profile:NotesDocument = lookupView.getDocumentByKey(lookupKey, true);
           if (null != profile) {
                   keywords = profile.getItemValue('Keywords')
           } else {
                   keywords = null;
           }
           
           // update keywords cache
           sessionScope.put(lookupKey, keywords);
           
           return keywords;
   
   } catch(e) {
           errorHandler('getKWValues', e)
           return null;
   }
}


And that's it -- if the values need to change in the combo box, it's a simple matter of editing the configuration document rather than changing code in the XPage.