Using the FileDownloader with Combobox

那年仲夏 提交于 2019-12-13 07:34:58

问题


I've tried using the new FileDownloader in Vaadin7. Unfortunately, it needs an AbstractComponent for the "extend" component (where it listens for the clicks)

Is there a way to use it with combobox items? As they are not AbstractComponents and thus do not fit with the "extend" method.


回答1:


The Vaadin forums have discussed this a lot, and there is no scheme now using FileDownloader or the similarly functioning BrowserWindowOpener. They all only work on AbstractComponents, and thus don't work on Action handlers for Table and Tree, or row click handlers on Table, or MenuItem in Menu, etc. The same applies to selected elements in their various select boxes.

You have to revert to the popup window style (so browsers will need to allow popups for it to work) using a regular click/valuechange listener, creating a Resource and passing it to the deprecated, but still working, Page.getCurrent().open(Resource...) method.




回答2:


Here is my work-around. It works like a charm for me. Hope it will help you. This example is for the MenuItem, but you can modify for ComboBox.

  1. Create a button and hide it by Css (NOT by code: button.setInvisible(false))

    final Button downloadInvisibleButton = new Button();
    downloadInvisibleButton.setId("DownloadButtonId");
    downloadInvisibleButton.addStyleName("InvisibleButton");
    

    In your theme, add this rule to hide the downloadInvisibleButton:

    .InvisibleButton {
        display: none;
    }
    
  2. When the user clicks on menuItem: extend the fileDownloader to the downloadInvisibleButton, then simulate the click on the downloadInvisibleButton by JavaScript.

    menuBar.addItem("Download", new MenuBar.Command() {
      @Override
      public void menuSelected(MenuBar.MenuItem selectedItem) {
        FileDownloader fileDownloader = new FileDownloader(...);
        fileDownloader.extend(downloadInvisibleButton);
        //Simulate the click on downloadInvisibleButton by JavaScript
        Page.getCurrent().getJavaScript()
           .execute("document.getElementById('DownloadButtonId').click();");
      }
    });
    


来源:https://stackoverflow.com/questions/17106081/using-the-filedownloader-with-combobox

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