Maryam
Maryam

Reputation: 71

Xpages, Compute combo box value

I have a combo box its values are 10, 20, 30, 40, 50 when I create a document and display it in a view the number is appeared but I want to display its labels (labels are text) not numbers. Can someone please help me find out how to solve this issue. Thanks

Upvotes: 0

Views: 1623

Answers (2)

Chris Toohey
Chris Toohey

Reputation: 146

I tend to add values of a given checkbox into a Resource Bundle, and then access that Resource Bundle both from the xp:selectItems and the xp:viewColumn values.

First, I'll create a Resource Bundle which I set as "label", with the following values:

itemsList_status=Draft|0,Published|1,Archived|-1
itemsList_delimiter=,

Here's an example of the xp:selectItems for an xp:combobox:

<xp:selectItems>
 <xp:this.value>
  <![CDATA[#{javascript:return getSelectItems('status');}]
 </xp:this.value>
</xp:selectItems>

Here's my getSelectItems() SSJS function:

var getSelectItems : function(key) {
 try {
    var returnObj = new Array();
    var items = null;
    switch(key) {
    case "status" : 
        items = @Text(label.itemsList_status).split(label.itemsList_delimiter);
        break
    }
    for( var n=0, l=items.length; n < l; n++) {
        returnObj.push(items[n]);
    }
    return returnObj;
 } catch(e) {
    print(database.getTitle() + " SSJS Error for getSelectItems()");
    print(e.toString());
 }
}

Run this function, and it'll return 3 options that read Draft, Published, and Archived while actually storing 0, 1, and -1 respectively. The problem that you're running into - and what using this abstracted method of storing your labels:values in a Resource Bundle + SSJS to address - is when you go to read the value but actually want to display the label...

Here's my getSelectedValue() function:

var getSelectedValue : function(key, thisValue) {
 try {
    var returnObj = new Array();
    var items = null;
    switch(key) {
    case "status" : 
        items = @Text(label.itemsList_status).split(label.itemsList_delimiter);
        break
    }
    var s = "";
    var l = "";
    for( var n=0, i=items.length; n < i; n++) {
        if(items[n].indexOf("|") == -1) {
            s = items[n];
            l = items[n];
        } else {
            s = items[n].split("|").pop();
            l = items[n].split("|").shift();
        }
        if(thisValue == s) {
            return l;
        }
    }
    return thisValue;

 } catch(e) {
    print(database.getTitle() + " SSJS Error for getSelectedValue()");
    print(e.toString());
 }
}

Now you'd pass the current value and the desired label:value Resource Bundle item handle, and this function spits out the correlating label. Here is how I use it in an xp:viewColumn:

<xp:viewColumn
 id="viewColumn_status">
 <xp:this.value><![CDATA[#{javascript:return ""}]]></xp:this.value>
  <xp:text
   value="#{javascript: return         
    getSelectedValue('status',thisEntry.getDocument().getItemValueString('status'))}" />
 <xp:viewColumnHeader
  value="#{label.viewColumnHeader_posts_status}"
  id="viewColumnHeader_status">
 </xp:viewColumnHeader>
</xp:viewColumn>

... and that's about it - the Resource Bundle entry to "itemList_status" acts as the authoritative master for the label:value pairs, so you just check that for the input building and deconstruct it for the labeling of stored values.

Hope this helps!

Upvotes: 2

Oliver Busse
Oliver Busse

Reputation: 3395

Using aliases is analog to the usage in a normal form. Use a formula item for the values:

return ["- Please select -|", "ten|10", "twenty|20", "thirty|30"];

But keep in mind that the values that is stored is always TEXT and not a number.

Upvotes: 1

Related Questions