如果另一个菜单中的值发生更改,如何更改 SelectOneMenu 中的列表?

发布于 2024-08-02 17:48:15 字数 120 浏览 1 评论 0 原文

如果第一个 SelectOneMenu 中的值发生更改,我想更改第二个 SelectOneMenu 中的 SelectItem[] 数组。这可能吗?

I want to change the SelectItem[] array in the second SelectOneMenu, if the value is changed in the first one. Is that possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

决绝 2024-08-09 17:48:15

我明白了这一点,但我使用了 RichFaces 的 AJAX 功能,而不仅仅是 JSF。刚刚将标签添加到我的第一个 selectOneMenu 中,并且它有效:)

<a4j:support event="onchange" action="#{bean.onChange}"
             reRender="otherSelectOneMenuID"/>

无论如何,感谢您的回复!

I figured this out, but I used RichFaces's AJAX functionality, not only JSF. Just added the tag to my first selectOneMenu, and it works:)

<a4j:support event="onchange" action="#{bean.onChange}"
             reRender="otherSelectOneMenuID"/>

Thanks for the response anyway!

坐在坟头思考人生 2024-08-09 17:48:15

如果将值更改侦听器绑定到第一个 selectOneMenu,应该是可能的。

从 ValueChangeEvent 获取新值并相应更新列表。然后,JSF 页面应该显示更新后的列表。

希望这是有道理的!

Should be possible if you bind a value change listener to the first selectOneMenu.

Get the new value from the ValueChangeEvent and update the list accordingly. the JSF page should then display the updated list.

Hope that makes sense!

手心的温暖 2024-08-09 17:48:15

好吧,我使用了 a4j 并且它有效。

<code>
//JSF
<h:outputLabel value="First selectOneMenu: "/>
<h:selectOneMenu value="#{yourBackingBean.selectedItem}">
<f:converter converterId="defaultConverter"/>
<f:selectItem id="df01" itemLabel="Item01" itemValue="1" />
<f:selectItem id="df02" itemLabel="Item02" itemValue="2" />
<f:selectItem id="df03" itemLabel="Item03" itemValue="3" />
<a4j:support event="onchange" reRender="secondSelectOneMenu"/> //secondSelectOneMenu is the id of the dropdown you want to change
</h:selectOneMenu>


<h:outputLabel value="Second selectOneMenu: "/>
<h:selectOneMenu value="#{yourBackingBean.attributeToStoreSelectedValue}" id="secondSelectOneMenu">
<f:converter converterId="defaultConverter"/>
<f:selectItem id="df00" itemLabel="Select" itemValue="0" /> //Default value
<f:selectItems value="#{yourBackingBean.returnByChoice}" />
</h:selectOneMenu>


//Converter

public class DefaultConverter implements Converter {
public Object getAsObject(FacesContext ctx, UIComponent component, String value) {
    return value;
}

public String getAsString(FacesContext ctx, UIComponent component, Object value) {
    String label = "";
    if (value != null) {
        label = value.toString();
    }
    return label;
}
}

//Backing Bean Sample
public List<SelectItem> returnByChoice() { //it must return a list of SelectItems so it can be displayed on the jsf page
   String id = (String) getSelectedItem(); //this is the value chosen from the first dropDownMenu wich selectedItem is the attribute onthe binding of the first dropDownMenu.
   ArrayList<SelectItem> arrItems = new ArrayList<SelectItem>();
   if (id != null) {

            List<YourClass> yourObjectList = yourDao.findAllItemsFromType(new Integer(id));

         Iterator<YourClass> iterator = yourObjectList.iterator();
         String tempName = "";
         String tempId = "";
         YourClass tempYourObject = null;

        while (iterator.hasNext()) {
           tempYourObject = iterator.next();
           tempId = String.valueOf(tempYourObject.getId());
           tempName = tempYourObject.getName();
           arrItems.add(new SelectItem(tempId, tempName));
        }
    }
    return arrProfiles;
}
</code>

Well I used a4j and it worked.

<code>
//JSF
<h:outputLabel value="First selectOneMenu: "/>
<h:selectOneMenu value="#{yourBackingBean.selectedItem}">
<f:converter converterId="defaultConverter"/>
<f:selectItem id="df01" itemLabel="Item01" itemValue="1" />
<f:selectItem id="df02" itemLabel="Item02" itemValue="2" />
<f:selectItem id="df03" itemLabel="Item03" itemValue="3" />
<a4j:support event="onchange" reRender="secondSelectOneMenu"/> //secondSelectOneMenu is the id of the dropdown you want to change
</h:selectOneMenu>


<h:outputLabel value="Second selectOneMenu: "/>
<h:selectOneMenu value="#{yourBackingBean.attributeToStoreSelectedValue}" id="secondSelectOneMenu">
<f:converter converterId="defaultConverter"/>
<f:selectItem id="df00" itemLabel="Select" itemValue="0" /> //Default value
<f:selectItems value="#{yourBackingBean.returnByChoice}" />
</h:selectOneMenu>


//Converter

public class DefaultConverter implements Converter {
public Object getAsObject(FacesContext ctx, UIComponent component, String value) {
    return value;
}

public String getAsString(FacesContext ctx, UIComponent component, Object value) {
    String label = "";
    if (value != null) {
        label = value.toString();
    }
    return label;
}
}

//Backing Bean Sample
public List<SelectItem> returnByChoice() { //it must return a list of SelectItems so it can be displayed on the jsf page
   String id = (String) getSelectedItem(); //this is the value chosen from the first dropDownMenu wich selectedItem is the attribute onthe binding of the first dropDownMenu.
   ArrayList<SelectItem> arrItems = new ArrayList<SelectItem>();
   if (id != null) {

            List<YourClass> yourObjectList = yourDao.findAllItemsFromType(new Integer(id));

         Iterator<YourClass> iterator = yourObjectList.iterator();
         String tempName = "";
         String tempId = "";
         YourClass tempYourObject = null;

        while (iterator.hasNext()) {
           tempYourObject = iterator.next();
           tempId = String.valueOf(tempYourObject.getId());
           tempName = tempYourObject.getName();
           arrItems.add(new SelectItem(tempId, tempName));
        }
    }
    return arrProfiles;
}
</code>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文