如何在comboBox中显示选定的数值(How to display selected numeric values in a comboBox)

编程入门 行业动态 更新时间:2024-10-28 09:25:44
如何在comboBox中显示选定的数值(How to display selected numeric values in a comboBox)

笔记形式中的数字字段连接到xpage中的组合框。 来自组合框的值是一个十进制值列表,1,02,103等,但它们作为文本存储在关键字文档中

组合框的类型是“字符串”(没有转换器),但是如果使用转换器将其更改为“十进制”,似乎并不重要。

逗号是瑞典的小数点分隔符

我使用@dbLookup获取关键字值

像这样的东西。 @DbLookup(分贝, “vwLookupCat”, “奖励”, “关键字”)

保存文档时,关键字文档中的值将保存为数字值。 但是组合框不再显示正确的选定值(在编辑模式下),并且在更改时显示验证错误,因为该值现在是数字,而不是像组合框关键字值中的文本。 或者因为该字段最初是一个文本字段,但保存时它是一个数字字段。

<xp:comboBox id="comboBox7" value="#{doc.bonus}"> <xp:this.converter> <xp:convertNumber type="number"></xp:convertNumber> </xp:this.converter> <xp:eventHandler event="onchange" submit="true" refreshMode="partial" refreshId="tbl"></xp:eventHandler> <xp:selectItem itemLabel="Välj Bonus" itemValue="0"></xp:selectItem> <xp:selectItems> <xp:this.value><![CDATA[#{javascript:@DbLookup(db,"vwLookupCat","BONUS","KeyWord")}]]></xp:this.value> </xp:selectItems> </xp:comboBox> 请注意,我没有在组合框上使用任何验证器 我无法将关键字字段类型更改为数字,因为这在其他许多地方都有用到 如果不帮助使用@Text(@Text(@DbLookup(db,“vwLookupCat”,“BONUS”,“KeyWord”)))

我应该如何解决这个问题?

提前致谢

托马斯

在这里似乎没有解决方案是类似的问题

I have a numeric field in a Notes form connected to a combobox in the xpage. the values from the combobox is a list of decimal values, 1,02, 1,03 etc but they are stored as text in the keyword documents

The combobox is of type "string" (no converters) but it doesn't seem to matter if I change it to a "decimal" using converters.

comma is a decimal separator in Sweden

I fetch the keyword values using a @dbLookup

something like this. @DbLookup(db,"vwLookupCat","BONUS","KeyWord")

When I save the document the values from the keyword document is saved as a numric value as it should. but the combobox is no longer showing the correct selected value (in edit mode) and displayes a validation error when I change it because the value is now numeric and not text as in the combobox keyword values. or because the field was initially a text field, but when saved it is a numeric field.

<xp:comboBox id="comboBox7" value="#{doc.bonus}"> <xp:this.converter> <xp:convertNumber type="number"></xp:convertNumber> </xp:this.converter> <xp:eventHandler event="onchange" submit="true" refreshMode="partial" refreshId="tbl"></xp:eventHandler> <xp:selectItem itemLabel="Välj Bonus" itemValue="0"></xp:selectItem> <xp:selectItems> <xp:this.value><![CDATA[#{javascript:@DbLookup(db,"vwLookupCat","BONUS","KeyWord")}]]></xp:this.value> </xp:selectItems> </xp:comboBox> Note that I am not using any validators on the combobox I can't change the keyword field type to numeric as this is used on many other places if doesn't help to use @Text (@Text(@DbLookup(db,"vwLookupCat","BONUS","KeyWord")))

How should I tackle this problem?

thanks in advance

Thomas

Seem to be a similar question here without solution

最满意答案

我可以想出两种方法可以解决这个问题,这两种方法都需要你使用豆类,希望你已经熟悉了这个概念。

在概念上是正义的

转换器保持在原来的位置。 毕竟你想要处理一个号码,你正在保存一个号码。 转换器指示框架将来自POST的返回字符串值转换为数字,这就是您想要的,因为与组件绑定的目标字段也被保存为数字。

问题是将此值与用于填充选项的值列表匹配。 为什么? 这些值不是数字。

解决方案是自定义构建选项,而不是让框架从dblookup返回的字符串值数组中进行自动装箱。

我很难写ssjs +公式,但是调用应该是这样的:

<xp:selectItems value="${javascript:myBeanName.getSelectItems(@DbLookup(db,"vwLookupCat","BONUS","KeyWord"))}"> </xp:selectItems>

bean方法:

public List<SelectItem> getSelectItems(String[] values) { List<SelectItem> options = new ArrayList<SelectItem>(); for (String value : values) { options.add(new SelectItem(Double.valueOf(value), value)); } return options; }

通过这样做,您可以创建具有可比较值的选项。

剩下的唯一问题就是IBM提供的完全违反直觉的转换器。 因为你不知道在内部选择“数字”的方式,它是一个Integer ,一个Double ,一个BigDecimal等......你被困在比确定性更多的不确定性中。 我有自己的号码转换器,但是因为我知道IBM的工作原理,所以我认为你可以通过为转换器指定一个附加参数来避开这个问题。

<xp:this.converter> <xp:convertNumber type="number" integerOnly="true" /> </xp:this.converter>

我知道,我知道, integerOnly会让你认为它会将值转换为Integer 。 它不会,它会转换为Double 。 幸运的你! 想象一下你需要一个Integer!

概念上很糟糕

另一种方法是将组合框绑定到视图范围变量。 您将在页面加载时使用字符串转换的文档值初始化该变量,然后使用该值。 在保存时,您将读取视图范围变量,将其转换回数字并在保存之前将该数字推送到doc字段。

I can think of 2 ways you can go about it, both of which require you to make use of beans, hopefully you should already be familiar with the concept.

The conceptually righteous

The converter stays where it is. After all you want to deal with a number, you are saving a number. The converter instructs the framework to convert the returning string value from the POST into a number, and that is what you want, since also the destination field bound with the component is saved as a number.

The problem is matching such value with the list of values used to populated the options. Why? Those values are not numbers.

The solution is custom building the options rather than letting the framework doing the autoboxing from the array of string values returned from dblookup.

It pains me to write ssjs+formula but the call should be something like this:

<xp:selectItems value="${javascript:myBeanName.getSelectItems(@DbLookup(db,"vwLookupCat","BONUS","KeyWord"))}"> </xp:selectItems>

The bean method:

public List<SelectItem> getSelectItems(String[] values) { List<SelectItem> options = new ArrayList<SelectItem>(); for (String value : values) { options.add(new SelectItem(Double.valueOf(value), value)); } return options; }

By doing this you are creating options with comparable values.

The only problem remaining is the utterly counterintuitive converter provided by IBM. Because you don't know what choosing 'number' does internally, whether it will be a Integer, a Double, a BigDecimal etc... you're stuck with more uncertainties than certainties. I have my own number converter but since I know how the IBM one works I think you can get away with the problem by specifying an additional param to the converter.

<xp:this.converter> <xp:convertNumber type="number" integerOnly="true" /> </xp:this.converter>

I know, I know, integerOnly makes you think it will convert the value to Integer. It doesn't, it converts to Double. Lucky you! Imagine you needed an Integer!

The conceptually crappy

The other approach is to bind the combobox to a view scoped variable. You would initialize the variable with the string converted doc value at page load and then work with that. At save time you would read the view scoped variable, convert it back to number and push the number to the doc field before saving it.

更多推荐

本文发布于:2023-07-26 19:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1280239.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数值   如何在   comboBox   display   numeric

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!