简体   繁体   English

JSF2,Ajax部分更新和ui重复 - 提交和更新动态添加的文本框

[英]JSF2, Ajax partial update and ui repeat - submit and update dynamically added text boxes

I have a JSF2 application and a page with the following code: 我有一个JSF2应用程序和一个包含以下代码的页面:

<h:inputText id="someInput" required="true" />

<p:outputPanel id="itemsPanel">
    <ui:repeat var="i" value="#{myBean.itemIndices}" id="items">
        <h:inputText 
            id="itemInput" 
            value="#{myBean.dataItems[i]}" 
            />
        <h:panelGroup 
            layout="block" 
            styleClass="clear" 
            rendered="#{((i+1) % 10 == 0)}" 
            />
    </ui:repeat>
</p:outputPanel>
<br/>
<p:commandButton
    classStyle="btn" 
    value="Add Row"
    actionListener="#{myBean.increaseItemsCount}" 
    update="itemsPanel" 
    immediate="false"
    ajax="true"
    />

The ui:repeat is dynamically rendering a number if text boxes depending on the itemIndices and dataItems properties. ui:repeat是动态呈现数字的文本框,具体取决于itemIndicesdataItems属性。 The "Add Row" button calls a method that will dynamically increase the number of itemIndices and dataItems , therefore additional text inputs appear. “添加行”按钮调用一个方法,该方法将动态增加itemIndicesdataItems的数量,因此会显示其他文本输入。

The current code will not work if there is no value in the text box someInput , because the AJAX request also validates the form and validation fails. 如果文本框someInput没有值,则当前代码将someInput ,因为AJAX请求也验证表单并且验证失败。 Pressing the button in this case has no visual effect, not even feedback for the user that validation did not pass (because I update itemsPanel only). 在这种情况下按下按钮没有视觉效果,甚至没有验证未通过的用户反馈(因为我只更新itemsPanel )。

If I change the button to have immediate="true" , then the validation issue is no more. 如果我将按钮更改为immediate="true" ,则验证问题不再存在。 Unfortunately, I want to be able to restore the values of the dynamic input fields that the user might have changed prior adding the new row. 不幸的是,我希望能够在添加新行之前恢复用户可能已更改的动态输入字段的值。 The immediate="true" attribute causes the form not to be submitted to the server, therefore the user-entered data will not be persisted. immediate="true"属性导致表单不被提交给服务器,因此用户输入的数据将不会被保留。

A possible solution to this could be to add process="all dynamic input ids csv" attribute to the command button and set the immediate attribute back to true . 可能的解决方案是将process="all dynamic input ids csv"属性添加到命令按钮并将immediate属性设置为true This will cause the server to validate only the inputs specified in the process attribute and they will be persisted. 这将导致服务器仅验证process属性中指定的输入,并且它们将被持久化。 The problem here is that these input ids are dynamically generated by JSF and I cannot come up with a proper way to get them as a CSV. 这里的问题是这些输入ID是由JSF动态生成的,我无法想出一种将它们作为CSV格式化的正确方法。 I would also not prefer a solution for dynamically generating ids in myBean relying on assumptions of how JSF would output them. 我也不喜欢在myBean动态生成id的解决方案, myBean依赖于JSF如何输出它们的假设。

All suggestions will be greatly appreciated. 所有建议将不胜感激。

You could use process="itemsPanel" . 您可以使用process="itemsPanel"

<p:commandButton
    classStyle="btn" 
    value="Add Row"
    actionListener="#{myBean.increaseItemsCount}" 
    process="itemsPanel" 
    update="itemsPanel" 
    ajax="true"
    />

You should only move the command button into <h:outputPanel id="itemsPanel"> . 您只应将命令按钮移动到<h:outputPanel id="itemsPanel">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM