How to open a Vaadin ComboBox by code?

点点圈 提交于 2019-12-13 15:25:56

问题


Is it possible to open a Vaadin ComboBox by code?

I'd like to present 2 comboboxes that depend on each other. When the user selects a value in the first, I'd like the 2nd combobox to automatically open the possible selections, so that the user can directly select one, instead of having to open the 2nd combobox himself.

Maybe there is an event that I could send to trigger the opening?


回答1:


The only thing I can think of is, after giving focus to the combobox, try sending it a keystroke (ie the down arrow to try and make it open). There is a good example here

The other way, maybe to try and use Selenium to trigger the combobox, but thats probably overkill. For examples of that, look into vaadin's testbench.




回答2:


I've done this in Vaadin 7 with Selenium WebDriver:

public void selectValueInCombobox(WebElement cmb, String value) {
    cmb.findElement(By.tagName("div")).click(); //shows the menu
    List<WebElement> findElements = webDriver.findElements(By.cssSelector("td[role='listitem']"));
    findElements.stream()
        .filter(item-> value.equals(item.findElement(By.tagName("span")).getText()))
        .findFirst().get().click();
}

The WebElement is the Combobox (its the div when rendered in html), and value is the listitem in the combobox you want selected.

  1. It clicks the down arrow button on the rightmost (its an inner div)
  2. Which renders the listitem html elements, just find those with role is listitem, then via Java 8's stream, filter with the same value
  3. Once found, click and it will be selected


来源:https://stackoverflow.com/questions/25565527/how-to-open-a-vaadin-combobox-by-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!