Question

I have a TextField component and added ValueChangeListener on it.

@Theme("myTheme")
@SuppressWarnings("serial")
@Title("VaadinTest")
@PreserveOnRefresh
@JavaScript({ "vaadin://js/jquery/jquery-1.10.1.min.js", "vaadin://js/test.js" })
public class HelloWorldUI extends UI {

@Override
protected void init(VaadinRequest request) {
    final VerticalLayout layout = new VerticalLayout();
    final TextField txtName = new TextField();
    txtName.setId("test");
    txtName.setImmediate(true);
    txtName.addValueChangeListener(new ValueChangeListener() {

        @Override
        public void valueChange(final ValueChangeEvent event) {
            System.out.println(txtName.getValue());
        }
    });
    layout.addComponent(txtName);

    Button btnOK = new Button("OK");
    btnOK.addClickListener(new ClickListener() {

        public void buttonClick(ClickEvent event) {
            Page.getCurrent().getJavaScript().execute("doSomething()");

        }
    });
    layout.addComponent(btnOK);

    setContent(layout);
}
}

And at my js file (test.js)

function doSomething() {
$('input#test').val("John").change();
}

But I never get the ValueChangeEvent , I mean I never get the console log for text John.

What am I missing ? Or can It be possible to trigger component's event by javascript ?

Was it helpful?

Solution

unfortunately JQuerys .trigger() and .change() functions don't work since vaadin version 7.2.

I am not sure if its a Jquery or Vaadin Bug. Probably a Jquery Bug.

however, you have to use the native javascript function .dispatchEvent()

  com.vaadin.ui.JavaScript.getCurrent().execute(
    "var field = document.getElementById('test');" +
            "field.dispatchEvent(new Event('change'));"
    );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top