SelectOneMenu重置并在ALT按下时触发更改事件 [英] SelectOneMenu resets and fires change event on ALT press

查看:177
本文介绍了SelectOneMenu重置并在ALT按下时触发更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Glassfish 4.1上使用PF 5.1,JSF 2.2.7.

Using PF 5.1, JSF 2.2.7 on Glassfish 4.1.

我有一个带有selectOneMenu的简单示例:

I have this simple example with a selectOneMenu:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  xmlns:p="http://primefaces.org/ui"
  xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
  xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
    <title>Test</title>
</h:head>
<h:body>
    <h:form>

        <p:selectOneMenu value="#{testBean.text}">
            <p:ajax listener="#{testBean.test()}" update="outputpanel"/>
            <f:selectItem itemLabel="1" itemValue="1"/>
            <f:selectItem itemLabel="2" itemValue="2"/>  
            <f:selectItem itemLabel="3" itemValue="3"/> 
        </p:selectOneMenu>

        <p:outputPanel id="outputpanel"> 
            #{testBean.text}
        </p:outputPanel>

    </h:form>
</h:body>
</html>

Bean:

import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

@Named
@ViewScoped
public class TestBean implements Serializable
{
private String text;

public String getText() {
    return text;
}

public void setText(String text) {
    System.out.println("settext: " + text);
    this.text = text;
}

public void test() {
    System.out.println("test called");
}

}

它按预期工作,但是如果下拉列表具有焦点,并且在Windows上按ALT或在Mac上按CMD,它将调用侦听器并重置下拉列表.当下拉列表未处于默认值时(当其已经为2或3时),就会发生这种情况.这就是说,例如,我不能按ALT + TAB来检查另一个打开的程序中的内容-当我回来时,它将被重置.

It works as expected, except that if the dropdown has focus and I press ALT on windows or CMD on mac it will call the listener and also reset the dropdown. This happens when the dropdown is not at its default value (when its already on 2 or 3). And this means I cannot for example press ALT + TAB to check something in another open program - when I come back it will be reset.

为什么这种邪恶的行为以及如何避免呢?我宁愿不要按ALT来触发event="change"并重置组件.

Why this wicked behaviour and how to avoid it? I would rather not have a press on ALT to fire an event="change" and reset the component.

推荐答案

这是与<p:selectOneMenu>关联的JavaScript中的错误.不能将控制键( Alt Ctrl 等)与字符键( A B 1 , 2 等).

This is a bug in the JavaScript associated with <p:selectOneMenu>. It's not distinguishing control keys (Alt, Ctrl, etc) from character keys (A, B, 1, 2, etc) when filtering menu items via keyboard.

基本上,在primefaces.js文件中的PrimeFaces.widget.SelectOneMenu对象定义中,必须将on("keyup.ui-selectonemenu", function...)替换为.on("keypress.ui-selectonemenu", function...). keypress事件仅在按下字符键时触发.这样,整个switch支票也可以删除.

Basically, in the PrimeFaces.widget.SelectOneMenu object definition in primefaces.js file, the on("keyup.ui-selectonemenu", function...) has to be replaced by .on("keypress.ui-selectonemenu", function...). The keypress event is only triggered when a character key is pressed. This way the whole switch check can also be removed.

  1. 获取/javax.faces.resource/primefaces.js.xhtml?ln=primefaces的副本并将其另存为/resources/primefaces/primefaces.js在您的Web应用程序中. WAR中的资源具有比JAR中的资源更高的加载优先级.

  1. Get a copy of /javax.faces.resource/primefaces.js.xhtml?ln=primefaces and save it as /resources/primefaces/primefaces.js in your webapp. Resources in WAR have higher loading precedence than those in JAR.

keyup.ui-selectonemenu上的Ctrl + F并将其替换为keypress.ui-selectonemenu.

Ctrl+F on keyup.ui-selectonemenu and replace this by keypress.ui-selectonemenu.

保存.

利润.

同时,将此问题报告给PrimeFaces员工,以便他们可以自己正确地解决它.修复程序发布后,您可以删除自定义脚本.

In the meanwhile, report this issue to PrimeFaces guys, so that they can fix it properly on their side. Once the fix is released, you can remove the customized script.

这篇关于SelectOneMenu重置并在ALT按下时触发更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆