selectOneMenu ajax events

后端 未结 4 1035
臣服心动
臣服心动 2020-12-03 02:45

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 so

相关标签:
4条回答
  • 2020-12-03 03:08

    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.

    0 讨论(0)
  • 2020-12-03 03:09

    The PrimeFaces ajax events sometimes 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 AjaxBehaviorEvent as argument in signature:

    public void subjectSelectionChanged(final AjaxBehaviorEvent event)  {...}
    
    0 讨论(0)
  • 2020-12-03 03:30

    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.

    0 讨论(0)
  • 2020-12-03 03:32

    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
    }
    
    0 讨论(0)
提交回复
热议问题