我有一个使用 Primefaces 3.5 的数据表,如图 below .
现在我正在编辑 id 为 43 的第二行,如下所示 image .
当我单击勾号(最右边的列)时,该行将被编辑,如下所示 picture .
很容易注意到状态的名称从 xxxx
更改为至zzz
但该国家/地区似乎保持不变,预计将更新为 America
来自Germany
.
实际上,已经对数据库进行了更改,但在 rowEdit
完成后,这些更改似乎没有反射(reflect)到数据表中。事件。
要观察对国家/地区所做的更改,需要重新加载页面。只有重新加载此页面时,才会显示正确的数据,如图 below .
这是列出国家/地区下拉框的列。
<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>
下面是 onRowEdit()
单击勾号时触发的方法(在 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);
}
}
完整代码JSF managed bean和 JSF page .
在 rowEdit()
方法(如上所述),stateTable.getCountry().getCountryId()
显示更新后的countryId
但使用这个国家对象来引用相应的国家名称,如 stateTable.getCountry().getCountryName()
仅显示旧的国家/地区名称(而不是更新的国家/地区名称)。有什么方法可以解决这个问题?
重要:
在上面的XHTML代码片段中,两者的value属性,
<h:outputText value="#{state.country.countryName}"/>
^^^^^^^^^^^^^_^^^^^^^^^^^
<p:selectOneMenu id="cmbCountryMenu" value="#{state.country.countryId}" .../>
^^^^^^^^^^^^^_^^^^^^^^^
有所不同,这对于显示国家/地区名称而不是显示国家/地区 ID 并引用相应的国家/地区 ID 至关重要。
如果将它们更改为反射(reflect)相同的 value
属性,
<h:outputText value="#{state.country.countryId}"/>
^^^^^^^^^^^^^_^^^^^^^^^
<p:selectOneMenu id="cmbCountryMenu" value="#{state.country.countryId}" .../>
^^^^^^^^^^^^^_^^^^^^^^^
然后它就会按预期工作(Primefaces 展示示例就像这样演示)。
这与动态更新显示列数值总数的页脚值相同。更新该列中的行时,页脚不会更新。该问题已报告here .
最佳答案
问题是您没有更改整个国家/地区对象。因为您使用 #{state.country.countryId}
作为 p:selectOneMenu
的值,所以当前所选国家/地区的属性 countryId
正在发生变化,但不是国家/地区,即 countryName
等其他属性将保留。
您必须将 p:selectOneMenu
的值更改为 #{state.country}
以及 f 的
到 itemValue
: selectItems#{country}
。看showcase对于p:selectOneMenu
。
此外,由于country
是一个POJO,默认情况下没有可用的转换器,因此您必须实现countryConverter
并在选择项目时使用它进行转换通过p:selectOneMenu
。
对 p:selectOneMenu
和此 article 使用以下代码作为实现转换器的支持。
<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>
关于jsf - 如果 value 属性 <p :selectOneMenu> and <h:outputText> is different,Primefaces DataTable 不会使用下拉框更新显示值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16639256/