簡體   English   中英

如果值屬性為,Primefaces DataTable不會使用下拉框更新顯示值 <p:selectOneMenu> 和 <h:outputText> 是不同的

[英]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);
    }
}

JSF受管beanJSF頁面的完整代碼。


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:selectItemsitemValue更改為#{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>

嵌套的

[英]nested <p:selectOneMenu does not update value

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM