selectOneMenu ajax events

2019-01-11 09:20发布

I am using an editable primefaces selectOneMenu to display some values. If the user selects an item from the List a textarea should be updated. However, if the user types something in the selectOneMenu, the textarea should not be updated.

I thought I could work this with ajax event out. However, I don't know which event I can use here. I only know the valueChange event. Are there any other events, like onSelect or onKeyUp?

Here is my code:

<p:selectOneMenu id="betreff" style="width: 470px !important;"  
            editable="true" value="#{post.aktNachricht.subject}">
            <p:ajax event="valueChange" update="msgtext"
                listener="#{post.subjectSelectionChanged}" />
            <f:selectItems value="#{post.subjectList}" />
</p:selectOneMenu>

<p:inputTextarea style="width:550px;" rows="15" id="msgtext"
        value="#{post.aktNachricht.text}" />

4条回答
叼着烟拽天下
2楼-- · 2019-01-11 10:02

The primefaces ajax events are very poorly documented, so in most cases you must go to the source code and check yourself.

p:selectOneMenu supports change event:

<p:selectOneMenu ..>
    <p:ajax event="change" update="msgtext"
        listener="#{post.subjectSelectionChanged}" />
    <!--...-->
</p:selectOneMenu>

which triggers listener with AjaxBehaviourEvent as argument in signature:

public void subjectSelectionChanged(final AjaxBehaviorEvent event)  {...}
查看更多
劫难
3楼-- · 2019-01-11 10:04

I'd rather use more convenient itemSelect event. With this event you can use org.primefaces.event.SelectEvent objects in your listener.

<p:selectOneMenu ...>
    <p:ajax event="itemSelect" 
        update="messages"
        listener="#{beanMB.onItemSelectedListener}"/>
</p:selectOneMenu>

With such listener:

public void onItemSelectedListener(SelectEvent event){
    MyItem selectedItem = (MyItem) event.getObject();
    //do something with selected value
}
查看更多
对你真心纯属浪费
4楼-- · 2019-01-11 10:12

Be carefull that the page does not contain any empty component which has "required" attribute as "true" before your selectOneMenu component running.
If you use a component such as

<p:inputText label="Nm:" id="id_name" value="#{ myHelper.name}" required="true"/>

then,

<p:selectOneMenu .....></p:selectOneMenu>

and forget to fill the required component, ajax listener of selectoneMenu cannot be executed.

查看更多
仙女界的扛把子
5楼-- · 2019-01-11 10:18

You could check whether the value of your selectOneMenu component belongs to the list of subjects.

Namely:

public void subjectSelectionChanged() {
    // Cancel if subject is manually written
    if (!subjectList.contains(aktNachricht.subject)) { return; }
    // Write your code here in case the user selected (or wrote) an item of the list
    // ....
}

Supposedly subjectList is a collection type, like ArrayList. Of course here your code will run in case the user writes an item of your selectOneMenu list.

查看更多
登录 后发表回答