PrimeFaces p:ajax event="change"不会在动态创建的 selecOneMenu 内容上触发

发布于 2024-10-24 23:00:21 字数 4043 浏览 6 评论 0 原文

我想在用户在 inputText 字段中键入内容时生成 selecOneMenu 内容,并响应组合框选择更改。
下面的代码在用户键入时更新 selecOneMenu 的内容。 (键入的数字和接下来的 9 个数字将添加到组合框中。这只是一个简化的示例代码。)
加载页面时,会正确触发 selecOneMenu 的更改事件。 但是,在 inputValue 字段中输入内容后,selecOneMenu 的内容发生更改,并且当我选择某个项目时不会触发更改事件。

如果 ComboBean 是会话范围的,则代码可以工作,但如果可能的话,我想避免使用此解决方案。

有可能做到这一点吗?
如果请求范围无法实现,原因是什么?

PrimeFaces 2.2
莫贾拉 2.0.2
GlassFish 3.0.1
浏览器:Chrome、Firefox、IEcombo.xhtml

<h:head>
    <title>Combo box example</title>
</h:head>

<h:body>
    <h:form>
        <p:panel id="mainPanel">
            <h:panelGroup id="formToSubmit" layout="block">
                <p:messages id="messages" />
                <h:panelGrid columns="2">
                    <h:outputLabel value="Enter a number" />
                    <h:inputText id="inputValue" value="#{comboBean.inputValue}">
                        <p:ajax event="keyup" update="combo"
                            listener="#{comboBean.onKeyUp}" />
                    </h:inputText>

                    <h:outputLabel value="Select a value:" />
                    <h:selectOneMenu id="combo" value="#{comboBean.selectedValue}">
                        <f:selectItem itemLabel="Select a value..."
                            noSelectionOption="true" />
                        <f:selectItems value="#{comboBean.values}" />
                        <p:ajax event="change" update="selectedValue"
                            listener="#{comboBean.valueSelected}" />
                    </h:selectOneMenu>
                    <h:outputLabel value="Selected value:" />
                    <h:inputText id="selectedValue" value="#{comboBean.selectedValue}" />
                </h:panelGrid>
            </h:panelGroup>
        </p:panel>
    </h:form>
</h:body>
</html>

ComboBean.java

package x;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

@Named
@RequestScoped
public class ComboBean implements Serializable
{
    private static final long serialVersionUID = 1L;
    private String inputValue;
    private String selectedValue;
    private List<String> values;

    @PostConstruct
    void init()
    {
        System.out.println("init");
        setValues(new LinkedList<String>());
        for(int i = 0; i<10 ; i++)
        {
            getValues().add(""+i);
        }
    }

    public void onKeyUp()
    {
        System.out.println("onkeyUp " + getInputValue());
        setValues(new LinkedList<String>());
        if (inputValue != null)
        {
            try 
            {
                int v = Integer.parseInt(inputValue);
                for(int i = 0; i<10 ; i++)
                {
                    getValues().add(""+(v+i));
                }
            } 
            catch (NumberFormatException ne) 
            {
                //doesn't matter
            }
        }
    }

    public void valueSelected()
    {
        System.out.println("valueSelected " + getSelectedValue());
    }

    public void submit()
    {
        System.out.println("submit " + getInputValue());
    }

    public void setInputValue(String inputValue)
    {
        this.inputValue = inputValue;
    }

    public String getInputValue()
    {
        return inputValue;
    }

    public void setSelectedValue(String selectedValue)
    {
        this.selectedValue = selectedValue;
    }

    public String getSelectedValue()
    {
        return selectedValue;
    }

    public void setValues(List<String> values)
    {
        this.values = values;
    }

    public List<String> getValues()
    {
        return values;
    }

}

I want to generate selecOneMenu content when the user types in an inputText field, and respond to combo box selection changes.
The below code updates the contents of the selecOneMenu as the user types. (The typed and the next 9 numbers gets added to the combo box. This is just a simplified example code.)
When the page is loaded, the change event of the selecOneMenu correctly gets fired.
However after typing in the inputValue field, content of selecOneMenu is changed, and the change event is not fired when I select an item.

The code works if ComboBean is session scoped, but I want to avoid this solution if possible.

Is it possible at all to do this?
What is the reason if it is not possible with request scope?

PrimeFaces 2.2
Mojarra 2.0.2
GlassFish 3.0.1
Browser: Chrome, Firefox, IE

combo.xhtml:

<h:head>
    <title>Combo box example</title>
</h:head>

<h:body>
    <h:form>
        <p:panel id="mainPanel">
            <h:panelGroup id="formToSubmit" layout="block">
                <p:messages id="messages" />
                <h:panelGrid columns="2">
                    <h:outputLabel value="Enter a number" />
                    <h:inputText id="inputValue" value="#{comboBean.inputValue}">
                        <p:ajax event="keyup" update="combo"
                            listener="#{comboBean.onKeyUp}" />
                    </h:inputText>

                    <h:outputLabel value="Select a value:" />
                    <h:selectOneMenu id="combo" value="#{comboBean.selectedValue}">
                        <f:selectItem itemLabel="Select a value..."
                            noSelectionOption="true" />
                        <f:selectItems value="#{comboBean.values}" />
                        <p:ajax event="change" update="selectedValue"
                            listener="#{comboBean.valueSelected}" />
                    </h:selectOneMenu>
                    <h:outputLabel value="Selected value:" />
                    <h:inputText id="selectedValue" value="#{comboBean.selectedValue}" />
                </h:panelGrid>
            </h:panelGroup>
        </p:panel>
    </h:form>
</h:body>
</html>

ComboBean.java

package x;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

@Named
@RequestScoped
public class ComboBean implements Serializable
{
    private static final long serialVersionUID = 1L;
    private String inputValue;
    private String selectedValue;
    private List<String> values;

    @PostConstruct
    void init()
    {
        System.out.println("init");
        setValues(new LinkedList<String>());
        for(int i = 0; i<10 ; i++)
        {
            getValues().add(""+i);
        }
    }

    public void onKeyUp()
    {
        System.out.println("onkeyUp " + getInputValue());
        setValues(new LinkedList<String>());
        if (inputValue != null)
        {
            try 
            {
                int v = Integer.parseInt(inputValue);
                for(int i = 0; i<10 ; i++)
                {
                    getValues().add(""+(v+i));
                }
            } 
            catch (NumberFormatException ne) 
            {
                //doesn't matter
            }
        }
    }

    public void valueSelected()
    {
        System.out.println("valueSelected " + getSelectedValue());
    }

    public void submit()
    {
        System.out.println("submit " + getInputValue());
    }

    public void setInputValue(String inputValue)
    {
        this.inputValue = inputValue;
    }

    public String getInputValue()
    {
        return inputValue;
    }

    public void setSelectedValue(String selectedValue)
    {
        this.selectedValue = selectedValue;
    }

    public String getSelectedValue()
    {
        return selectedValue;
    }

    public void setValues(List<String> values)
    {
        this.values = values;
    }

    public List<String> getValues()
    {
        return values;
    }

}

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

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

发布评论

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

评论(1

烛影斜 2024-10-31 23:00:21

问题是您在 init() 方法中的每个请求期间重置了列表。所以你选择的元素将不再存在。

如果您不想使用SessionScope,也许ViewScope将是一个解决方案:那么如果重新加载同一页面,bean将不会被重置。

The problem is that you reset your list during each request in the init() method. So your selected element will no longer be there.

If you don't want to use SessionScope, maybe the ViewScope would be a solution: then the bean won't be reset if the same page is reloaded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文