【问题标题】:Check any celleditor in a data table is in edit mode检查数据表中的任何单元格编辑器是否处于编辑模式
【发布时间】:2016-07-12 19:32:14
【问题描述】:

我有一个数据表,其中显示了一些包含 3 列的应用程序参数: 名称(输出外部), 值(p:cellEditor)和 编辑(p:rowEditor 使用)

单击任何行值字段中的编辑按钮时,将转换为输入字段并附加验证器。更改并接受(单击检查图标)值后,页面底部会提供一个“更新按钮”以保存所有更改。

我的问题是,如果出现验证错误,我们按下“更新按钮”,然后调用以旧值保存托管 bean 中的函数。因此,为了停止这种情况,我想在任何行打开编辑模式时禁用“更新按钮”。我可以检查第 2 列中所有单元格编辑器的模式,所以我将在更新按钮的禁用属性中使用它。 请建议任何其他更好的方法也是可能的?

使用 jsf 2.1 和 primefaces 3.5

XHTML sn-p

        <!-- Body panel for display of individual configuration mode -->
        <p:panel id="mainConfigPanel" >
        <!-- status message section -->
        <p:messages id="msg" autoUpdate="true" closable="true"/>
        <!-- Parameter configuration mode -->
            <p:panel
                rendered="#{configMBean.configUtility.configParamModeOn}"
                styleClass="panelNoBorder">
                <p:dataTable id="configParamTable" var="configParamVar"
                    value="#{configMBean.configParamList}" editable="true">

                    <p:ajax event="rowEdit" listener="#{configMBean.onRowEdit}" update=":mainForm:msg" />
                    <p:ajax event="rowEditCancel" listener="#{configMBean.onRowCancel}"  update=":mainForm:msg" />

                    <p:column headerText="Parameter Name" sortBy="#{configParamVar.paramConfigName}">
                        <h:outputText id="paramNameId" value="#{configParamVar.paramConfigName}" />
                    </p:column>


                    <p:column headerText="Param Value" sortBy="#{configParamVar.paramConfigValue}">
                        <p:cellEditor>
                            <f:facet name="output" > <h:outputText value="#{configParamVar.paramConfigValue}" /> </f:facet>
                            <f:facet name="input">
                                <p:inputText id="paramValueId" value="#{configParamVar.paramConfigValue}" required="true"
                                    validator="#{configMBean.validateParam}"   >
                                    <f:validateLength maximum="2000" />
                                    <f:attribute name="input" value="#{configParamVar}" />
                                </p:inputText>
                            </f:facet>
                        </p:cellEditor>
                    </p:column>

                    <p:column headerText="Edit" style="text-align:center;vertical-align:top;width:20px">
                        <p:rowEditor />
                    </p:column>

                </p:dataTable>


                <h:panelGrid columns="2" >
                    <p:commandButton value="Update Parameters" actionListener="#{configMBean.saveParamUpdate}" update=":mainForm" />
                    <p:commandButton value="Cancel" actionListener="#{configMBean.cancelParamUpdate}" immediate="true" update=":mainForm">
                    </p:commandButton>
                </h:panelGrid>
            </p:panel>
            <!-- End of Parameter configuration mode panel -->


</p:panel>
<!-- End of body panel for individual configuration mode -->

    </p:panelGrid>
    <!-- end of main panel -->

托管 Bean 中的函数

public void onRowEdit(RowEditEvent event) {
    System.out.println(" In Row Edit");
}

public void onRowCancel(RowEditEvent event) {
    System.out.println("In Row Canel of Parameter Config");
}

public void validateParam(FacesContext facesContext, UIComponent component,
        Object value) throws ValidatorException, Exception {
    if (value != null) {
        //Getting  parameter Name and Value for validation
        String paramName = ((RowEntity) component.getAttributes().get("input")).getParamConfigName();
        String paramValue = (String) value;
        FacesMessage msg = null;

        //Validation Cases
        if (paramName.equalsIgnoreCase(ResourceBundle.getMsg("Param_Enable_FTP"))) {
            if (!paramValue.equalsIgnoreCase("true") || !paramValue.equalsIgnoreCase("false")) {
                msg = new FacesMessage(FacesMessage.SEVERITY_WARN, ResourceBundle.getMsg("Param_True_False_Validation")+ paramName, "");
                throw new ValidatorException(msg);
            }
        } else if (paramName.equalsIgnoreCase(ResourceBundle.getMsg("Param_Contingency_Reserve"))) {
            if (!Pattern.matches("-?\\d*\\.?\\d*", paramValue)) {
                    msg = new FacesMessage(FacesMessage.SEVERITY_WARN, ResourceBundle.getMsg("Param_Number_Validation") + paramName, "");
                    throw new ValidatorException(msg);
                }
        }// end if else if

    }

}

【问题讨论】:

  • 不确定我是否正确。为什么要为同一行使用 cellEditor 和 rowEditor?为什么不对所有可以更新的列使用 rowEditor?所以你不需要保存按钮。可能您为您的问题提供了一些代码。每个人都更容易进入。

标签: jsf-2 primefaces


【解决方案1】:

来自文档:rowEdit 有一个 Ajax 行为事件

rowEdit | org.primefaces.event.RowEditEvent | When a row is edited.

您可以在RowEditEvent 被触发时禁用更新按钮,并在取消或保存行编辑后释放该按钮。

我希望你的问题是正确的,这会有所帮助。

【讨论】:

  • 感谢您的回复。如果验证失败,则抛出 ValidatorException,因此程序不会到达 rowEdit 事件来切换更新按钮。在 rowEdit 事件之前需要一些东西
  • 我想你为你的数据表使用了一个自定义的 FacesValidator。这意味着您可以在验证中禁用/启用按钮,不是吗?
  • 是的,我尝试在验证失败时以及在 RowEdit 和 RowCancel 中设置禁用条件 - 我添加了启用条件。它可以工作,但是由于启用代码是在 rowEdit 和 rowCancel 中编写的,因此在下面的场景中存在一个小问题。验证错误发生在第一行并禁用按钮,之后如果我转到第二行并进行更改并接受它(第二行的 rowEdit 触发)更新按钮被启用。因此,如果我有办法检查是否有任何行处于编辑模式/出现异常,我将禁用更新按钮,反之亦然。
  • 你可以防止多行编辑...stackoverflow.com/questions/19376811/…我认为一条一条地更新记录会更有意义。
  • 是的,这是实现上述目标的可能方式。感谢您的帮助:)
猜你喜欢
  • 1970-01-01
  • 2020-01-28
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 2017-07-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多