jsf2h:dataTable动态生成的行不绑定到支持bean。

时间:2020-12-27 20:12:36

I'm having problems saving/updating rows within a table.

我在保存/更新表中的行时有问题。

I have a method myBean.addRule to dynamically add new rows to the table.
This seems to work ok....I click it and a new row appears in the UI to enter new data.

我有一个方法myBean。addRule动态地向表添加新行。这似乎工作好....我点击它,一个新的行出现在UI中以输入新的数据。

But problems seem to arise when I save myBean.saveRules.

但是当我救我的时候,问题就出现了。

It looks like only the most recently added row is getting bound to my backing beans and updated.

看起来只有最近添加的行被绑定到后台bean并进行了更新。

For example if I have 3 rows of data:
- remove some text from row 1 and hit the save button this update is NOT saved.
- If I change text on row 3 this change is saved (Most recently added row).

例如,如果我有3行数据:-从第1行删除一些文本,然后点击save按钮,这个更新将不会被保存。-如果我改变第3行的文本,这个更改将被保存(最近添加的行)。

Am I missing some binding attribute on any of my components that would fix this?

我是否在我的任何组件上丢失了一些绑定属性来修复这个问题?

<a4j:repeat value="#{myBean.ruleSet}" var="rule" id="ruleIterator">

    <h:dataTable value="#{rule}" var="currentRuleItem">

        <h:column>

                <h:panelGrid columns="2" cellspacing="5">
                    <h:outputLabel value="#{msg.FrequencyOfSpending}" />

                    <h:selectOneMenu id="ruleFrequencyOptions" value="#{currentRuleItem.ruleControls.ControlOne.controlParams.Period.valueSelected}" styleClass="commonSelect">
                    <f:selectItems value="#{currentRuleItem.ruleControls.ControlOne.controlParams.Period.validValues}" itemLabelEscaped="true" />
                    <f:ajax event="valueChange" listener="#{currentRuleItem.ruleControls.ControlOne.controlParams.Period.valueSelectedChange}" onerror="handleAjaxError" render="rulesGroup" />
                    </h:selectOneMenu>
                </h:panelGrid>

                <h:panelGroup id="rulesGroup">
                    <a4j:repeat value="#{currentRuleItem.ruleParams.Action.properties}" var="RuleParamProperty" id="budgetRuleIterator">

                    <h:panelGrid columns="4" cellspacing="5" columnClasses="ruleParamCheckbox, ruleParamAction, ruleParamActionFrequency, ruleParamActionInput">

                    <h:selectBooleanCheckbox value="#{RuleParamProperty.selected}" immediate="true">
                        <a4j:ajax event="click" listener="#{RuleParamProperty.selectedChange}"  onerror="handleAjaxError" />
                    </h:selectBooleanCheckbox>

                    <h:outputText value="#{msg[RuleParamProperty.name]}" />

                    <h:panelGrid columns="3">
                        <h:outputText value="#{msg.Action_1}" />
                        <h:outputText value="#{msg[currentRuleItem.ruleControls.ControlOne.controlParams.Period.valueSelected]}" class="italic-text" />   
                        <h:outputText value="#{msg.Action_3}" />
                    </h:panelGrid>

                    <h:inputText value="#{RuleParamProperty.inputValue}" />

                    </h:panelGrid>

                    </a4j:repeat>
                </h:panelGroup>

            </h:column>

    </h:dataTable>

    <!--******* Link here to generate row with exact same format as all code above ***-->
    <h:panelGrid columns="2">
        <img id="AddIcon" src="#{facesContext.externalContext.requestContextPath}/images/icons/add.png" alt="#{msg.addControl}" />
        <h:commandLink value="#{msg.addControl}" action="#{myBean.addRule}" />
    </h:panelGrid>

</a4j:repeat>

<h:panelGrid columns="2" cellspacing="5">
    <h:commandButton value="#{msg.sc_cancel}" immediate="true" action="#{myBean.cancel}" />
    <h:commandButton value="#{msg.ua_save}" action="#{myBean.saveRules}" />
</h:panelGrid>

// my bean method
public void addRule()
{   
    iRuleSet.get("RuleControl1").add(createRule());     
}

I have not include my backing bean code as it's extremely complicated, I can edit and show snippets if required.

我没有包含支持bean代码,因为它非常复杂,如果需要,我可以编辑和显示代码片段。

Thanks in advance

谢谢提前

1 个解决方案

#1


1  

Ok so after many frustrating hours reading all sorts of articles about different tag handlers (c:forEach, ui:include, etc) and components (ui:repeat, h:datatable, etc..), the differences between them, the problems that can arise from mixing and mashing them, etc.........

好吧,在经历了许多令人沮丧的时间之后,我阅读了关于不同的标签处理程序(c:forEach, ui:include,等等)和组件(ui:repeat, h:datatable,等等)的各种文章,它们之间的差异,混合和混合它们可能产生的问题,等等……

It's fixed (I think!)

它是固定的(我认为)

I removed all my ui:repeat and replaced them with Richfaces a4j:repeat which provides the additional attribute keepSaved. I also removed my h:dataTable and replaced it with aj4:repeat.

我删除了所有的ui:repeat并将其替换为Richfaces a4j:repeat,它提供了附加属性keepsave。我还删除了我的h:dataTable并用aj4替换它:repeat。

Now for the life of me, I cannot find anywhere that explains what keepSaved does!!
The Richfaces 4 API Docs are as per usual, dreadful!
The description beside it reads just like most attributes....No Description!

现在,为了我的生命,我找不到任何可以解释保存的东西的地方!!Richfaces 4 API文档和往常一样,太可怕了!它读起来就像大多数属性....旁边的描述没有描述!

Could anyone shed some like on what this component attribute does?

有没有人可以对这个组件属性做一些类似的事情?

Thanks

谢谢

Solution:

解决方案:

<a4j:repeat value="#{myBean.ruleSet}" var="rule" id="ruleIterator">

    <a4j:repeat value="#{rule}" var="currentRuleItem">

        <h:column>

                <h:panelGrid columns="2" cellspacing="5">
                    <h:outputLabel value="#{msg.FrequencyOfSpending}" />

                    <h:selectOneMenu id="ruleFrequencyOptions" value="#{currentRuleItem.ruleControls.ControlOne.controlParams.Period.valueSelected}" styleClass="commonSelect">
                    <f:selectItems value="#{currentRuleItem.ruleControls.ControlOne.controlParams.Period.validValues}" itemLabelEscaped="true" />
                    <f:ajax event="valueChange" listener="#{currentRuleItem.ruleControls.ControlOne.controlParams.Period.valueSelectedChange}" onerror="handleAjaxError" render="rulesGroup" />
                    </h:selectOneMenu>
                </h:panelGrid>

                <h:panelGroup id="rulesGroup">

                <a4j:repeat value="#{currentRuleItem.ruleParams.Action.properties}" var="RuleParamProperty" id="budgetRuleIterator" keepSaved="true">

                    <h:panelGrid columns="4" cellspacing="5" columnClasses="ruleParamCheckbox, ruleParamAction, ruleParamActionFrequency, ruleParamActionInput">

                    <h:selectBooleanCheckbox value="#{RuleParamProperty.selected}" immediate="true">
                        <a4j:ajax event="click" listener="#{RuleParamProperty.selectedChange}"  onerror="handleAjaxError" />
                    </h:selectBooleanCheckbox>

                    <h:outputText value="#{msg[RuleParamProperty.name]}" />

                    <h:panelGrid columns="3">
                        <h:outputText value="#{msg.Action_1}" />
                        <h:outputText value="#{msg[currentRuleItem.ruleControls.ControlOne.controlParams.Period.valueSelected]}" class="italic-text" />   
                        <h:outputText value="#{msg.Action_3}" />
                    </h:panelGrid>

                    <h:inputText value="#{RuleParamProperty.inputValue}" />

                    </h:panelGrid>

                    </a4j:repeat>
                </h:panelGroup>

            </h:column>

    </a4j:repeat>

    <!--******* Link here to generate row with exact same format as all code above ***-->
    <h:panelGrid columns="2">
      <img id="AddIcon" src="#{facesContext.externalContext.requestContextPath}/images/icons/add.png" alt="#{msg.addControl}" />
      <a4j:commandLink value="#{msg.addControl}" action="#{myBean.addRule}">
        <f:param name="ruleType" value="BudgetRule" />
      </a4j:commandLink>
    </h:panelGrid>

</a4j:repeat>

<h:panelGrid columns="2" cellspacing="5">
    <h:commandButton value="#{msg.sc_cancel}" immediate="true" action="#{myBean.cancel}" />
    <h:commandButton value="#{msg.ua_save}" action="#{myBean.saveRules}" />
</h:panelGrid>

// my bean method
public void addRule()
{   
    iRuleSet.get("RuleControl1").add(createRule());     
}

#1


1  

Ok so after many frustrating hours reading all sorts of articles about different tag handlers (c:forEach, ui:include, etc) and components (ui:repeat, h:datatable, etc..), the differences between them, the problems that can arise from mixing and mashing them, etc.........

好吧,在经历了许多令人沮丧的时间之后,我阅读了关于不同的标签处理程序(c:forEach, ui:include,等等)和组件(ui:repeat, h:datatable,等等)的各种文章,它们之间的差异,混合和混合它们可能产生的问题,等等……

It's fixed (I think!)

它是固定的(我认为)

I removed all my ui:repeat and replaced them with Richfaces a4j:repeat which provides the additional attribute keepSaved. I also removed my h:dataTable and replaced it with aj4:repeat.

我删除了所有的ui:repeat并将其替换为Richfaces a4j:repeat,它提供了附加属性keepsave。我还删除了我的h:dataTable并用aj4替换它:repeat。

Now for the life of me, I cannot find anywhere that explains what keepSaved does!!
The Richfaces 4 API Docs are as per usual, dreadful!
The description beside it reads just like most attributes....No Description!

现在,为了我的生命,我找不到任何可以解释保存的东西的地方!!Richfaces 4 API文档和往常一样,太可怕了!它读起来就像大多数属性....旁边的描述没有描述!

Could anyone shed some like on what this component attribute does?

有没有人可以对这个组件属性做一些类似的事情?

Thanks

谢谢

Solution:

解决方案:

<a4j:repeat value="#{myBean.ruleSet}" var="rule" id="ruleIterator">

    <a4j:repeat value="#{rule}" var="currentRuleItem">

        <h:column>

                <h:panelGrid columns="2" cellspacing="5">
                    <h:outputLabel value="#{msg.FrequencyOfSpending}" />

                    <h:selectOneMenu id="ruleFrequencyOptions" value="#{currentRuleItem.ruleControls.ControlOne.controlParams.Period.valueSelected}" styleClass="commonSelect">
                    <f:selectItems value="#{currentRuleItem.ruleControls.ControlOne.controlParams.Period.validValues}" itemLabelEscaped="true" />
                    <f:ajax event="valueChange" listener="#{currentRuleItem.ruleControls.ControlOne.controlParams.Period.valueSelectedChange}" onerror="handleAjaxError" render="rulesGroup" />
                    </h:selectOneMenu>
                </h:panelGrid>

                <h:panelGroup id="rulesGroup">

                <a4j:repeat value="#{currentRuleItem.ruleParams.Action.properties}" var="RuleParamProperty" id="budgetRuleIterator" keepSaved="true">

                    <h:panelGrid columns="4" cellspacing="5" columnClasses="ruleParamCheckbox, ruleParamAction, ruleParamActionFrequency, ruleParamActionInput">

                    <h:selectBooleanCheckbox value="#{RuleParamProperty.selected}" immediate="true">
                        <a4j:ajax event="click" listener="#{RuleParamProperty.selectedChange}"  onerror="handleAjaxError" />
                    </h:selectBooleanCheckbox>

                    <h:outputText value="#{msg[RuleParamProperty.name]}" />

                    <h:panelGrid columns="3">
                        <h:outputText value="#{msg.Action_1}" />
                        <h:outputText value="#{msg[currentRuleItem.ruleControls.ControlOne.controlParams.Period.valueSelected]}" class="italic-text" />   
                        <h:outputText value="#{msg.Action_3}" />
                    </h:panelGrid>

                    <h:inputText value="#{RuleParamProperty.inputValue}" />

                    </h:panelGrid>

                    </a4j:repeat>
                </h:panelGroup>

            </h:column>

    </a4j:repeat>

    <!--******* Link here to generate row with exact same format as all code above ***-->
    <h:panelGrid columns="2">
      <img id="AddIcon" src="#{facesContext.externalContext.requestContextPath}/images/icons/add.png" alt="#{msg.addControl}" />
      <a4j:commandLink value="#{msg.addControl}" action="#{myBean.addRule}">
        <f:param name="ruleType" value="BudgetRule" />
      </a4j:commandLink>
    </h:panelGrid>

</a4j:repeat>

<h:panelGrid columns="2" cellspacing="5">
    <h:commandButton value="#{msg.sc_cancel}" immediate="true" action="#{myBean.cancel}" />
    <h:commandButton value="#{msg.ua_save}" action="#{myBean.saveRules}" />
</h:panelGrid>

// my bean method
public void addRule()
{   
    iRuleSet.get("RuleControl1").add(createRule());     
}