Tin*_*iny 2 jsf selectonemenu primefaces
我有一个使用Primefaces 3.5的dataTable,如下所示.

现在我正在使用id 43编辑第二行,如下图所示.

当我单击刻度线(最右边的列)时,将对该行进行编辑,如下图所示.

它可以很容易地注意到,国家的名字是从改变xxxx到zzz,但该国仍显得预计将被更新为相同America的Germany.
实际上,已对数据库进行了更改,但在rowEdit事件完成时它们似乎没有反映到dataTable .
要观察对国家/地区所做的更改,需要重新加载页面.仅当重新加载此页面时,它才会显示正确的数据,如下所示.

这是列出国家/地区下拉框的列.
<p:ajax event="rowEdit" listener="#{stateManagedBean.onRowEdit}" update=":form:dataTable :form:systemMessages :form:messages" process=":form:dataTable :form:systemMessages :form:messages"/>
<p:ajax event="rowEditCancel" listener="#{stateManagedBean.onRowEditCancel}" update=":form:systemMessages :form:messages" process=":form:systemMessages :form:messages"/>
<p:column headerText="Country" sortBy="#{state.country.countryName}" filterBy="#{state.country.countryName}" filterMaxLength="45">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{state.country.countryName}" />
</f:facet>
<f:facet name="input">
<p:selectOneMenu id="cmbCountryMenu" value="#{state.country.countryId}" rendered="true" editable="false" converter="#{longConverter}" converterMessage="The supplied value is incorrect." required="true" requiredMessage="Select an appropriate option." style="width:100%;">
<f:selectItems var="country" value="#{stateManagedBean.countries}" itemLabel="${country.countryName}" itemValue="${country.countryId}" itemLabelEscaped="true" rendered="true"/>
</p:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
Run Code Online (Sandbox Code Playgroud)
以下onRowEdit()是单击tick时触发的方法(在JSF托管bean中).
public void onRowEdit(RowEditEvent event)
{
StateTable stateTable=(StateTable) event.getObject();
if(stateService.update(stateTable))
{
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Success : ", "The row with the id "+stateTable.getStateId()+" has been updated successfully.");
FacesContext.getCurrentInstance().addMessage(null, message);
}
}
Run Code Online (Sandbox Code Playgroud)
在rowEdit()方法(如上所示)中,stateTable.getCountry().getCountryId()显示更新countryId但使用此国家/地区对象引用相应的国家/地区名称,stateTable.getCountry().getCountryName()只显示旧国家/地区名称(而不是更新的国家/地区名称).有什么方法可以解决这个问题?
重要:
在上面的XHTML代码段中,两者的value属性,
<h:outputText value="#{state.country.countryName}"/>
^^^^^^^^^^^^^_^^^^^^^^^^^
<p:selectOneMenu id="cmbCountryMenu" value="#{state.country.countryId}" .../>
^^^^^^^^^^^^^_^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)
是不同的,这对于显示国家/地区名称而不是显示国家/地区ID并参考相应的国家/地区ID是必不可少的.
如果更改它们以反映相同的value属性,
<h:outputText value="#{state.country.countryId}"/>
^^^^^^^^^^^^^_^^^^^^^^^
<p:selectOneMenu id="cmbCountryMenu" value="#{state.country.countryId}" .../>
^^^^^^^^^^^^^_^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)
然后它按预期工作(Primefaces展示示例(s)就像这样).
问题是你没有改变整个国家的对象.因为您使用的#{state.country.countryId}值p:selectOneMenu只是countryId当前所选国家/地区的属性正在变化,而不是国家,即其他属性countryName将保留.
你必须改变的值p:selectOneMenu,以#{state.country}和itemValue的f:selectItems来#{country}.看看展示的p:selectOneMenu.
此外,与country默认情况下没有转换器可用的POJO一样,您必须实现a countryConverter并在选择项目时使用它进行转换p:selectOneMenu.
使用以下代码p:selectOneMenu和本文作为实现转换器的支持.
<p:selectOneMenu id="cmbCountryMenu" value="#{state.country}" rendered="true" editable="false" converter="#{countryConverter}" converterMessage="The supplied value is incorrect." required="true" requiredMessage="Select an appropriate option." style="width:100%;">
<f:selectItems var="country" value="#{stateManagedBean.countries}" itemLabel="#{country.countryName}" itemValue="#{country}" itemLabelEscaped="true" rendered="true"/>
</p:selectOneMenu>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5621 次 |
| 最近记录: |