Wednesday, 19 January 2011

Xpages - Combobox - set first choice to blank

In Xpages the combobox works different from what you are used to in the Notes form equivalent, the dialog list field.
In Notes if you get the choices using an @DbColumn or @DbLookup, assign no default value and the user does not consciously pick a value, no value is assigned to the field.
But when you do that with a combobox in Xpages, the first value in the list of choices will be assigned to the field.
Usually that is not what I want, I want the user to make a conscious choice. And assigning a default value of a space does not help if that value is not in the list of choices. So you will have to add a blank value as the first option in the list of choices.
I found 2 ways to solve this and it eliminates the need to set a default value.

1. The simplest way is to add a space as the first label-value pair and for the second add the rest of the choices using @DbColumn/@DbLookup.

2. Insert a blank value in the javascript code that retrieves the list of choices using @DbColumn/@DbLookup. This is probably the better method if you also want to include code to do some form of cache. See the code sample below.

 // get the cached result if used before 
var list = sessionScope.<cachename>;
// if no cached values found, create it
if (!list) {
// create an array with 1 blank entry as first entry
var arr = new Array(" ");
var res = @DbLookup("", "<a view>", "<a key>", 2);
// append the retrieved values to the array
var list = arr.concat(res);
sessionScope.<cachename> = list;
return list;


NotesSensei said...

You can actually add a manual entry in the values list and have the formula on second place. This way you don't need the concat which is rather slow.

Big Blue Ocean said...

Use @Text("") as the first value in the list of values.