selectOneMenu ajax 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16587585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 11:01:32  来源:igfitidea点击:

selectOneMenu ajax events

ajaxjsfprimefaces

提问by leostiw

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.

我正在使用可编辑的primefaces selectOneMenu 来显示一些值。如果用户从列表中选择一个项目,则应更新文本区域。但是,如果用户在 selectOneMenu 中键入内容,则不应更新 textarea。

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 valueChangeevent. Are there any other events, like onSelector onKeyUp?

我以为我可以用 ajax 事件解决这个问题。但是,我不知道我可以在这里使用哪个事件。我只知道valueChange事件。是否还有其他事件,例如onSelectonKeyUp

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}" />

回答by Danubian Sailor

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

PrimeFaces ajax 事件有时记录得很差,因此在大多数情况下,您必须自己查看源代码。

p:selectOneMenusupports changeevent:

p:selectOneMenu支持更改事件:

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

which triggers listener with AjaxBehaviorEventas argument in signature:

AjaxBehaviorEvent在签名中使用as 参数触发侦听器:

public void subjectSelectionChanged(final AjaxBehaviorEvent event)  {...}

回答by ?MER TA?CI

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

请注意,在 selectOneMenu 组件运行之前,页面不包含任何“required”属性为“true”的空组件。
如果您使用诸如

<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.

忘记填写需要的组件,selectoneMenu的ajax监听器无法执行。

回答by Micha? Stochmal

I'd rather use more convenient itemSelectevent. With this event you can use org.primefaces.event.SelectEventobjects in your listener.

我宁愿使用更方便的itemSelect事件。通过此事件,您可以org.primefaces.event.SelectEvent在侦听器中使用对象。

<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
}

回答by Stefanos Kargas

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

您可以检查您的selectOneMenu组件的值是否属于主题列表。

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 subjectListis a collection type, like ArrayList. Of course here your code will run in case the user writesan item of your selectOneMenulist.

据说subjectList是一个集合类型,比如ArrayList. 当然,您的代码将在这里运行,以防用户写入您的selectOneMenu列表中的项目。