[英]Primefaces DataTable doesn't update the display value with a drop box, if the value attribute of <p:selectOneMenu> and <h:outputText> is different
我有一個使用Primefaces 3.5的dataTable,如下所示 。
現在,我正在編輯ID為43的第二行,如下圖所示。
當我單擊勾號(最右邊的列)時,該行被編輯,如下圖所示 。
可以很容易地注意到,該州的名稱已從xxxx
更改為zzz
但該國家似乎保持不變,預計將從Germany
更新為America
。
實際上,已對數據庫進行了更改,但是在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>
下面是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);
}
}
在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是必不可少的。
如果更改它們以反映相同的value
屬性,
<h:outputText value="#{state.country.countryId}"/>
^^^^^^^^^^^^^_^^^^^^^^^
<p:selectOneMenu id="cmbCountryMenu" value="#{state.country.countryId}" .../>
^^^^^^^^^^^^^_^^^^^^^^^
然后它會按預期工作(Primefaces展示示例就這樣演示)。
它與動態更新頁腳的值相同,以動態顯示一列數字的總和。 在更新該列中的一行時,頁腳不會更新。 該問題已在此處報告。
問題是您沒有更改整個國家對象。 由於您使用#{state.country.countryId}
作為值p:selectOneMenu
剛財產countryId
當前選定的國家正在發生變化,而不是國家,即其它屬性,如countryName
將保持不變。
您必須將p:selectOneMenu
的值更改為#{state.country}
,將f:selectItems
的itemValue
更改為#{country}
。 查看展示櫃中的p:selectOneMenu
。
另外,由於country
是默認情況下沒有可用轉換器的POJO,因此當通過p:selectOneMenu
選擇項目時,您將必須實現countryConverter
並將其用於轉換。
將以下代碼用於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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.