【问题标题】:Removing the selections on selectOneListbox / selectManyMenu on a button click in Primefaces在 Primefaces 中单击按钮时删除 selectOneListbox / selectManyMenu 上的选择
【发布时间】:2018-09-20 17:49:19
【问题描述】:

所以我设计了一个简单的表单,其中包含 selectOneListbox / selectManyMenu 元素,我用它来根据用户选择生成一段文本。

一切正常。现在我想要的是当用户点击“清除”按钮时,选择应该被撤消并且页面应该回到初始状态(没有选择)。

我怎样才能做到这一点?

xhtml

        <h:form>
    <h:panelGrid columns="6" style="margin-bottom:10px;" cellpadding="5" columnClasses="label, value">
    <p:panel>
    <h6 style="width:10.4em;background-color:#ACBECE;">Comment  Type</h6>
    <!-- comment type -->
    <p:selectOneListbox id="basic1" value="#{decisionTreeBean.option1}" style=" font-size: 12px;background-color:silver;width:12em; height:10em; border-style:solid !important;border-width:0.5px !important; border-color:grey !important;">
         <f:selectItems value="#{decisionTreeBean.commentType}" var="X" itemLabel="#{X}" itemValue="#{X}" />
    </p:selectOneListbox>
    </p:panel>
<p:panel>
        <h6 style="width:10.4em;background-color:#ACBECE;">MTCN</h6>
        <p:selectManyMenu id="advanced1" value="#{decisionTreeBean.option2}" showCheckbox="true" style="font-size: 12px;background-color:silver;width:12em; height:10em; border-style:solid !important;border-width:0.5px !important; border-color:grey !important;">
            <f:selectItems value="#{decisionTreeBean.mtcns}" var="X" itemLabel="#{X}" itemValue="#{X}" />                  
        </p:selectManyMenu>
        </p:panel>
    </h:panelGrid>

    <!-- Text Area -->
    <p:inputTextarea  id="decisionNotes" readonly="#{facesContext.renderResponse}" value="#{decisionTreeBean.decisionNotes}" styleClass="dispostionInputTextArea" autoResize="true">
    </p:inputTextarea>
    <br/>
    <p:commandButton  id="generateBtn" value="Generate Text" action="#{decisionTreeBean.generateText}" update=":#{p:component('decisionNotes')}">
    </p:commandButton>

    <p:commandButton  id="clearBtn" value="Clear" action="#{decisionTreeBean.clearText}" update=":#{p:component('decisionNotes')}">
    </p:commandButton>
    <p:ajaxStatus onstart="PF('statusDialog').show()"
            onsuccess="PF('statusDialog').hide()" />
        <p:dialog widgetVar="statusDialog" modal="true" draggable="false"
            closable="false" resizable="false" showHeader="false">
            <p:graphicImage value="/images/ajax-loader.gif" />
        </p:dialog>

    </h:form>

在我的 bean 中,我有一个 clear 方法可以清除 textarea 的内容。我希望此按钮撤消选择以及 ajax 调用的一部分。

public void clearText() {
        this.decisionNotes=" ";
    }

(为简洁起见,我在 xhtml 中仅包含 2 列。共有 5 个具有相同功能的单独选择列)

【问题讨论】:

  • 从清除按钮更新组件怎么样?
  • 是的。那行得通。谢谢:-)

标签: jsf primefaces


【解决方案1】:

我也会在 bean 方法中简单地重置 Menu/Box 的值:

public void clearText() {
    this.decisionNotes=" ";
    this.option1 = null;
    //and so on...
}

然后你只需要更新按钮的更新属性中的组件。但是,如果您想刷新表单中的几乎每个组件,我会更新整个表单而不是每个组件。所以就用update="@form":

注意:p:commandButton 的默认进程值为@form。因此,您的清除按钮将在每次单击时处理整个表单,尽管它不需要。我将它设置为process="@this",所以它只会执行它的操作。

【讨论】:

  • 您能告诉我如何在列中包含行吗?我的 selectOneListbox / selectManyMenu 中的所有项目都应该用水平线分隔。
  • 这应该可以使用 CSS,但我不是 CSS 专家。
猜你喜欢
  • 2013-07-19
  • 2014-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多