【问题标题】:Toggle <p:panel> depending on selected value of <p:selectOneRadio>根据 <p:selectOneRadio> 的选定值切换 <p:panel>
【发布时间】:2012-12-11 02:44:23
【问题描述】:

我想在用户选择特定单选按钮时切换隐藏面板。 使用最新版本的 primefaces。

例如:

<p:panel id="panel1" visible="false" widgetVar="firePanel" closable="true" toggleable="true">
 hello
</p:panel>

<p:selectOneRadio value="#{formBean.selectedOptions}" layout="pageDirection">
  <f:selectItem itemLabel="Option 1" itemValue="opt1" />
  <f:selectItem itemLabel="Option 2" itemValue="opt2" />
  <f:selectItem itemLabel="Option 3" itemValue="opt"/>                           
</p:selectOneRadio>

我想实现这样的目标,但是在单击 RadioButton3 时,不使用命令按钮

<p:commandButton id="but" value="Show" onclick="firePanel.show();"/>

这可能吗? 谢谢!

【问题讨论】:

    标签: jsf primefaces


    【解决方案1】:

    采取以下步骤:

    1. 在您的支持 bean 中声明一个boolean 变量来管理面板的状态并将其绑定到面板的visible 属性

      boolean panelIsVisible;
      //getter and setter
      
      <p:panel id="panel1" visible="#{formBean.panelIsVisible}" widgetVar="firePanel" closable="true" toggleable="true">
      
    2. 定义一个方法来根据selectedOptions的值切换可见性布尔值

      public void changePanelState(){
      
          //set panelIsVisible to true or false based on whatever conditions you choose
      
      }
      
    3. &lt;p:ajax/&gt; 事件添加到您的 &lt;p:selectOneRadio/&gt; 以在该菜单上的任何选择上触发 ajax 事件

      <p:selectOneRadio value="#{formBean.selectedOptions}" layout="pageDirection">
        <f:selectItem itemLabel="Option 1" itemValue="opt1" />
        <f:selectItem itemLabel="Option 2" itemValue="opt2" />
        <f:selectItem itemLabel="Option 3" itemValue="opt"/> 
        <p:ajax listener="#{formBean.changePanelState}" update="panel1"/>                          
      </p:selectOneRadio>
      

      这是假设 panel1 与 selectOneRadio 位于同一 &lt;h:form/&gt; 中。

    【讨论】:

    • 你也可以使用visible="#{formBean.selectedOptions == 'opt'}"而不需要额外的属性和监听器。
    • 感谢@kolossus & BalusC。它按预期工作,确实有很大帮助。干杯!
    • 不幸的是,它仅在我第一次运行该页面时才有效。如果我在尝试提交表单后遇到任何验证错误,则 ajax 调用将停止工作。关于@BalusC 的任何建议。谢谢
    猜你喜欢
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多