本文介绍了单击一个按钮,Xpages 更多字段(无限制)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 x 开始.xpage 应用程序上的字段(在我的应用程序中,我有一对文本数据字段和数字数据字段)(比如 10 对),然后当用户单击更多字段"时,我希望更多对动态出现而没有完整的刷新页面,但想无限没有.字段(只要页面不崩溃),然后我想提交所有这些字段的表单和数据.实现此目的的最佳方法是什么?

I would like to start with x no. of fields (in my app I have a pair of textual data field and numeric data field) on a xpage application (say 10 pairs) and then when the user clicks on "more field", I want more pairs to appear dynamically without a full refresh on the page, but would like unlimited no. of fields (as long as the page doesn't crash) and then I would like to submit the form and the data for all those fields. What's the best way to implement this?

推荐答案

通常,字段使用点表示法绑定到文档数据源:

Usually, fields are bound to a document data source using dot notation:

<inputText value="#{contact.firstName}" />

但是,也支持数组表示法:

However, array notation is also supported:

<inputText value="#{contact['firstName']}" />

因为后一种语法中的字段名称被视为字符串,而不是 bean 的隐式属性,因此可以动态计算.我发现定义这些动态字段的最简单方法是为每个基本类型的字段创建一个自定义控件,并将每个定义为接受数据源和字段名称.因此,该字段本身以类似于以下的语法结束:

Because the field name in this latter syntax is being treated as a string, not as an implicit property of the bean, it can be dynamically computed. What I've found to be the easiest way to define these dynamic fields is to create a custom control for each of the basic types of fields, and define each as accepting the data source and the field name. So the field itself then ends up with a syntax similar to the following:

<inputText value="#{compositeData.dataSource[compositeData.fieldName]}" />

通过使用该语法,可以使用任何复杂度的计算来确定要传递给自定义控件的字段名称.在您尝试完成的场景中,在围绕字段对的重复控件上指定一个 indexVar 将允许您为每个字段指定一个后缀......可能类似于以下内容:

By using that syntax, a calculation of any complexity can be used to determine what field name to pass to the custom control. In the scenario you're attempting to accomplish, specifying an indexVar on the repeat control that surrounds the field pair would allow you to designate a field suffix for each... perhaps something like the following:

<xp:repeat indexVar="fieldSuffix" value="#{viewScope.rowCount}">
 <xp:div>
  <xc:dynamicInputText dataSource="#{contact}" fieldName="fullName_#{fieldSuffix}" />
  <xc:dynamicInputNumber dataSource="#{contact}" fieldName="phoneNumber_#{fieldSuffix}" />
 </xp:div>
</xp:repeat>

使用这种方法,您最终会得到名为fullName_0"、fullName_1"等的字段,直至达到 viewScope 中指定的限制.通常,唯一的复杂之处是确保在打开现有文档时,将 viewScope 变量设置回正确的限制.当然,另一种方法实际上是将限制另存为文档中的另一个项目,并将重复值与其绑定.

With this approach, you would end up with fields named "fullName_0", "fullName_1", etc., up to the limit specified in the viewScope. Typically, the only complication is ensuring that when an existing document is opened, the viewScope variable is set back to the correct limit. Another approach to that, of course, is actually saving the limit as another item on the document and binding the repeat value to it instead.

这篇关于单击一个按钮,Xpages 更多字段(无限制)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 21:36