问题
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.
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; }
When the user clicks on menuItem: extend the
fileDownloader
to thedownloadInvisibleButton
, then simulate the click on thedownloadInvisibleButton
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