【问题标题】:primefaces rowEdit Event to update table only if the event is a successprimefaces rowEdit 事件仅在事件成功时更新表
【发布时间】:2017-09-06 13:52:34
【问题描述】:

我正在调用 rowEdit 事件,并且我只想在事件成功执行数据库更新时刷新表。下面是我的代码。

XHTML

 <p:dataTable id="xobjTable" var="xobj" value="#{xListView.xListResponseObject}"
                                 scrollRows="20" scrollable="true" liveScroll="true" scrollHeight="600"
                                 rowKey="#{xobj.X}"
                                 selection="#{xListView.selectedObject}" selectionMode="single" 
                                 style="margin-top: 20px; margin-bottom:20px" editable="true">

                        <f:facet name="header">
                            Search Results
                            <p:spacer width="20"/>
                            <h:commandLink id="csv">
                                <p:graphicImage value="csv.png" width="24"/>
                                <p:dataExporter type="csv" target="xobj" fileName="xListSearch" />
                                <p:tooltip id="toolTipFade" for="csv" value="Click on CSV to download entire table as a csv file. " />
                            </h:commandLink>
                        </f:facet>
                        <p:ajax event="rowEdit" listener="#{xListView.onRowEdit}" update=":xListform:xobjTable" />
                        <p:column headerText="X" style="width:60px;text-align: center">
                            <h:outputText value="#{xobj.X}" />
                        </p:column>
                        <p:column headerText="Y" style="width:60px;text-align: center">
                            <h:outputText value="#{xobj.Y}" />
                        </p:column>
                        <p:column headerText="Modified Date" style="width:160px;text-align: center">
                            <h:outputText value="#{xobj.modifiedDate}" />
                        </p:column>
                        <p:column headerText="Active" style="width:160px;text-align: center">
                            <p:cellEditor>
                                <f:facet name="output">
                                    <h:outputText value="#{xobj.active}"/>
                                </f:facet>
                                <f:facet name="input">
                                    <h:selectOneMenu value="#{xobj.active}">
                                        <f:selectItems value="#{xListView.activeList}" />
                                    </h:selectOneMenu>
                                </f:facet>
                            </p:cellEditor>
                        </p:column>
                        <p:column headerText="Location" style="width:160px;text-align: center">
                            <p:cellEditor>
                                <f:facet name="output">
                                    <h:outputText value="#{xobj.location}"/>
                                </f:facet>
                                <f:facet name="input">
                                    <p:inputText id="location" value="#{xobj.location}" />  
                                </f:facet>
                            </p:cellEditor>
                        </p:column>
                        <p:column headerText="State" style="width:160px;text-align: center">
                            <p:cellEditor>
                                <f:facet name="output">
                                    <h:outputText value="#{xobj.state}"/>
                                </f:facet>
                                <f:facet name="input">
                                    <p:inputText id="state" value="#{xobj.state}" />  
                                </f:facet>
                            </p:cellEditor>
                        </p:column>
                        <p:column headerText="x Country Code" style="width:160px;text-align: center">
                            <p:cellEditor>
                                <f:facet name="output">
                                    <h:outputText value="#{xobj.xCountryCode}"/>
                                </f:facet>
                                <f:facet name="input">
                                    <p:inputText id="xCountryCode" value="#{xobj.xCountryCode}" />  
                                </f:facet>
                            </p:cellEditor>
                        </p:column>
                        <p:column headerText="xrarier" style="width:160px;text-align: center">
                            <p:cellEditor>
                                <f:facet name="output">
                                    <h:outputText value="#{xobj.carrier}"/>
                                </f:facet>
                                <f:facet name="input">
                                    <h:selectOneMenu value="#{xobj.carrier}">
                                        <f:selectItems value="#{xListView.xarrierList}" />
                                    </h:selectOneMenu>
                                </f:facet>
                            </p:cellEditor>
                        </p:column>
                        <p:column style="width:32px" headerText="Edit" rendered="#{allTabs.isShow(xListView.title)}">
                            <p:rowEditor />
                        </p:column>
                        <p:column style="width:160px;text-align: center" rendered="#{allTabs.isShow(xListView.title)}" headerText="Delete">
                            <p:commandButton id="deleteDid"  actionListener="#{xListView.deletexy(xobj)}"  
                                             icon="ui-icon ui-icon-trash red" title="Delete"  update="@form">
                                <p:confirm header="Confirmation" message="#{xListView.finalDeleteMessage}"  icon="ui-icon-alert" />
                            </p:commandButton>
                        </p:column>
                    </p:dataTable>

这里是事件方法

public void onRowEdit(RowEditEvent event) {
        xListResponseObject editObject = (xListResponseObject) event.getObject();
        logger.debug("Coming to edit the row.");
        String editXYStatus = xListDAO.editxy(editObject);
        if (editxyStatus.equals(SUCCESS_RESPONSE)) {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "INFO", "X/Y edit successful!");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        } else {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "INFO", "X/Y edit failed :" + editxyStatus);
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }

    }

我希望仅在 rowEdit 事件更新数据库时才进行更新。 我可以在我的方法中设置一个布尔值并在 p:ajax 的更新中访问数据吗?

【问题讨论】:

  • 在您的 bean 中使用 RequestContext 并仅在您的 dbaction 成功时从那里更新组件

标签: jsf primefaces


【解决方案1】:

添加到您的 SUCCESS_RESPONSE 末尾

RequestContext.getCurrentInstance().update("form:npaobjTable");

或者无论你的桌子真实 ID 是什么。

【讨论】:

  • 我尝试了上述方法并从 p:ajax 中删除了更新并将其添加到 rowEdit 事件中,但表格仍然使用无效记录进行更新。以下是我的更改。
  • if (editNpaNxxStatus.equals(SUCCESS_RESPONSE)) { FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "INFO", "NPA/NXX 编辑成功!"); FacesContext.getCurrentInstance().addMessage(null, msg); RequestContext.getCurrentInstance().update(":npaListform:npaobjTable"); }
  • 你没有处理新值
  • 不工作。即使我的 DAO 操作失败,它仍然使用无效值更新单元格。
  • 即使我不使用 RequestContext.getCurrentInstance().update 如果我在单击编辑铅笔后单击勾号,单元格也会自行更新。所以想知道我们是否可以控制单元格更新而不是自动进行。谢谢
【解决方案2】:

我遇到了同样的问题。 下面的网址将解决您的问题。

http://forum.primefaces.org/viewtopic.php?f=3&t=38412&p=153402#p153402

实际反应:

public void onRowEdit(RowEditEvent event) {
        NpaListResponseObject editObject = (NpaListResponseObject) event.getObject();
        logger.debug("Coming to edit the row.");
        String editNpaNxxStatus = npaListDAO.editNpaNxx(editObject);
        if (editNpaNxxStatus.equals(SUCCESS_RESPONSE)) {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "INFO", "NPA/NXX edit successful!");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        } else {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "INFO", "NPA/NXX edit failed :" + editNpaNxxStatus);
            FacesContext.getCurrentInstance().addMessage(null, msg);
           FacesContext.getCurrentInstance().validationFailed();
        }

    }

添加 FacesContext.getCurrentInstance().validationFailed();最后在失败的情况下。

【讨论】:

  • 请在此处添加实际的解释和回复,而不仅仅是指向另一个回复的链接
  • 根据您的建议编辑了代码。添加 FacesContext.getCurrentInstance().validationFailed();在最后一行。
  • 不过,您应该就如何解决问题提供真实的解释,例如解释代码的作用、添加位置等。答案应该是描述性的,而不是复制粘贴!
  • 所有的解释都在问题本身中。无需更多解释。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-30
相关资源
最近更新 更多