xpages number converter with combobox in ND9

别等时光非礼了梦想. 提交于 2019-12-11 03:05:29

问题


I'm having trouble using number conversion with numberConvert in a combo box in xpages on a Domino 9 server. This used to work on the 8.5 server.

When I submit the values I get: Validation Error: Value is not valid

I also tried to populate the values with "new javax.faces.model.SelectItem" but that didn't make any difference.

Does anyone know how to use numbers in combo boxes in ND9?

Here is the source (I removed everything unneccesary for this example):

<xp:comboBox id="combo" value="#{viewScope.testfield}">
    <xp:this.converter>
        <xp:convertNumber type="number"></xp:convertNumber>
    </xp:this.converter>
    <xp:selectItem itemLabel="9" id="selectItem1" itemValue="9">
    </xp:selectItem>
    <xp:selectItems>
        <xp:this.value><![CDATA[#{javascript:var arr=new Array("0","1","2"); return arr;}]]></xp:this.value>
    </xp:selectItems>
</xp:comboBox>

<xp:message id="message1" for="combo"></xp:message>

<xp:button value="Label" id="button1">
    <xp:eventHandler event="onclick" submit="true"
        refreshMode="complete">
        <xp:this.action>
            <xp:save></xp:save>
        </xp:this.action>
    </xp:eventHandler>
</xp:button>


回答1:


EDIT:

If you want to select numbers in a ComboBox you have to define comboBox's possible values as an array of numbers, not strings.

<xp:this.beforePageLoad><![CDATA[#{javascript:viewScope.testfield = 1}]]></xp:this.beforePageLoad>

<xp:comboBox id="combo" value="#{testfield}">
    <xp:selectItem itemLabel="0" itemValue="${javascript:0}"></xp:selectItem>
    <xp:selectItem itemLabel="1" itemValue="${javascript:1}"></xp:selectItem>
    <xp:selectItem itemLabel="2" itemValue="${javascript:2}"></xp:selectItem>
</xp:comboBox>

This example works perfect without using a converter if the value is a scope variable.

If you have a flexible number of entries you can use both ways shown in Adibabu Kancharla's answer.

The number converter is necessary for binding to a document's number field. The following example works for me in ND853 and ND9. I had to add integerOnly="true" though:

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
    <xp:this.data>
        <xp:dominoDocument var="document1" formName="NumberTest"
            action="editDocument"
            documentId="477FF8697EE50EDBC1257B710073DDE3">
        </xp:dominoDocument>
    </xp:this.data>
    <xp:comboBox id="combo" value="#{document1.Number}">
        <xp:selectItem itemLabel="0" itemValue="${javascript:0}"></xp:selectItem>
        <xp:selectItem itemLabel="1" itemValue="${javascript:1}"></xp:selectItem>
        <xp:selectItem itemLabel="2" itemValue="${javascript:2}"></xp:selectItem>
        <xp:this.converter>
            <xp:convertNumber type="number" integerOnly="true"></xp:convertNumber>
        </xp:this.converter>
    </xp:comboBox>
    <xp:message id="message1" for="combo"></xp:message>

    <xp:button value="Label" id="button1">
        <xp:eventHandler event="onclick" submit="true"
            refreshMode="complete">
            <xp:this.action>
                <xp:save></xp:save>
            </xp:this.action>
        </xp:eventHandler>
    </xp:button>
</xp:view>



回答2:


Please ensure that field 'testField' is of type 'Number' on the underlying form. I had the same issue on 8.5.3 server. So, i wrote the below code to overcome the issue..

    <xp:selectItems>
     <xp:this.value><![CDATA[#{javascript:
var arr = ['0','1','2']
var comboOptions = [];
for (i = 0; i < arr.length; i++){   
    comboOptions.push(new javax.faces.model.SelectItem(parseFloat(arr[i]), arr[i]))
}
return comboOptions}]]>
 </xp:this.value>
</xp:selectItems>

You can simplify the above code, if you know how to use managed beans. Below is the code for bean.

public class ApplicationSettings implements Serializable{
 private static final long serialVersionUID = 1L;
 private List comboOptions;

 public ApplicationSettings(){
  loadDefaults();
 }

 public void loadDefaults(){
  for(int x = 0; x <= 3; x = x+1){
   SelectItem item = new SelectItem(new Double(x),""+x);
   comboOptions.add(item);
  }
 }

 public List getComboOptions() {
  return comboOptions;
 }
 public void setComboOptions(List comboOptions) {
  this.comboOptions = comboOptions;
 }
}

In faces-configxml, register the managed bean(name: ApplicationSettings, scope:application). Then in your xpage..

<xp:selectItems value="#{ApplicationSettings.comboOptions}"></xp:selectItems>


来源:https://stackoverflow.com/questions/16606410/xpages-number-converter-with-combobox-in-nd9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!