簡體   English   中英

在動態創建的selecOneMenu內容上未觸發PrimeFaces p:ajax event =“ change”

[英]PrimeFaces p:ajax event=“change” not fired on dynamically created selecOneMenu content

我想在用戶在inputText字段中鍵入內容時生成selecOneMenu內容,並響應組合框選擇的更改。
以下代碼在用戶鍵入時更新selecOneMenu的內容。 (將鍵入的數字和接下來的9個數字添加到組合框中。這只是一個簡化的示例代碼。)
加載頁面后,將正確觸發selecOneMenu的change事件。 但是,在inputValue字段中鍵入后,selecOneMenu的內容將更改,並且在選擇項目時不會觸發change事件。

如果ComboBean是會話作用域的,則該代碼有效,但我希望盡可能避免這種解決方案。

有可能做到這一點嗎?
請求范圍無法實現的原因是什么?

PrimeFaces 2.2
Mojarra 2.0.2
GlassFish 3.0.1
瀏覽器: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;
    }

}

問題是您在init()方法中的每個請求期間重置了列表。 因此,您選擇的元素將不再存在。

如果您不想使用SessionScope ,也許可以使用ViewScope解決方案:如果重新加載同一頁面,則不會重置Bean。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM