vaadin 23:addpropertychangelistener用于JavaScript更改

发布于 2025-01-27 14:54:20 字数 1262 浏览 3 评论 0原文

预期的:在Vaadin 23应用程序中,当更改HTML属性的值时,我想知道。

我尝试的:我创建了此输入公式:

“示例输入表单”

    // Text input field
    Input input = new Input();
    input.setId("myInputField");
    Element textInput = input.getElement();
    
    // Change listener for property "value"
    // Works well for manual input
    // BUT doesn't work for JavaScript changes
    textInput.addPropertyChangeListener("value", "change", 
        e -> {System.out.println(e.getValue());}
    );
    
    // Button that changes the value of attribute "value"
    Button button = new Button("Change value");
    button.addClickListener(e -> {UI.getCurrent().getPage().executeJs(
        "document.getElementById('myInputField').setAttribute('value','hello');"
    );});

    this.add(input, button);

什么工作:当用户在输入字段中键入“某物”时,服务器会打印控制台的“东西”。这是打算和观察的。好的。

什么不起作用:当用户按下按钮时,输入字段的内容为“ Hello”(OK)。但是属性ChangeListener不会发射事件,因此服务器不会向控制台打印任何东西。

问题:即使JavaScript已更改了属性的值,是否有办法使属性Changelistener射击事件?

Intended: In a Vaadin 23 application I'd like to get informed when the value of an HTML attribute is changed.

What I tried: I created this input formula:

Sample input form

    // Text input field
    Input input = new Input();
    input.setId("myInputField");
    Element textInput = input.getElement();
    
    // Change listener for property "value"
    // Works well for manual input
    // BUT doesn't work for JavaScript changes
    textInput.addPropertyChangeListener("value", "change", 
        e -> {System.out.println(e.getValue());}
    );
    
    // Button that changes the value of attribute "value"
    Button button = new Button("Change value");
    button.addClickListener(e -> {UI.getCurrent().getPage().executeJs(
        "document.getElementById('myInputField').setAttribute('value','hello');"
    );});

    this.add(input, button);

What does work: When the user types "something" into the input field then the server prints "something" to the console. This is intended and observed. OK.

What does NOT work: When the user pushes the button then the content of the input field is "hello" (OK). But the PropertyChangeListener does not fire an event, so the server does not print anything to the console.

Question: Is there a way to get the PropertyChangeListener fire an event, even if the attribute's value has been changed by JavaScript?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倦话 2025-02-03 14:54:20

我在这里看到两个问题。

首先,vaadin流中的服务器端属性更改侦听器需要由客户端事件触发。当用户直接更改字段中的值时,< input>元素会触发事件,而不是通过编程更改值时。如果您从代码更改值,则还需要手动触发事件以触发更新。

其次,正如评论中指出的那样,您应该更改属性而不是属性。在大多数情况下,具有相同名称的属性和属性是同步的,但是输入的值是例外。在这里,属性是初始值,属性是实际值。如果您仅更改属性,则保持不变,但是在用户通过UI编辑或更改属性之后差异。

I see two problems here.

First, the server-side property change listener in Vaadin Flow needs to be triggered by a client-side event. The <input> element fires events when the the user directly changes the value in the field, but not when the value is changed programmatically. If you change the value from code, then you need to also manually fire an event to trigger the update.

Second, as pointed out in the comments, you should change the property rather than the attribute. In most cases, the attribute and the property with the same name are kept in sync, but the value of an input is an exception. Here, the attribute is the initial value and the property is the actual value. The stay the same if you only change the attribute, but diverge after the user edits through the UI or if the property is changed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文