Vaadin Gird getSelectedRows()返回空,但是有选定的行

时间:2017-03-15 10:12:23

标签: java vaadin7 vaadin-grid

使用vaadin-gird时,我遇到了一种奇怪的行为。我只是试图将来自LinkedHashMaps的arrayList存储在网格中所选行的信息中(gRegisteredRecords)。出于任何原因,当我第一次点击按钮btnGenerateXML时,方法getSelectedRows()返回一个空列表,但第二个,它工作正常...为什么它在第一次尝试时不起作用?这是代码..谢谢!

_btnGenerateXML.addClickListener(p -> {
        ArrayList <LinkedHashMap<String, String >> alSelected = new ArrayList<>();

        for (Object itemId: gRegisteredRecords.getSelectedRows()) {

            LinkedHashMap<String, String> lhmProgrammValue = new LinkedHashMap<>();

            this.gRegisteredRecords.getContainerDataSource().
            getContainerPropertyIds().forEach(propertyId ->
                lhmProgrammValue.put(propertyId.toString(),
                    this.gRegisteredRecords.getContainerDataSource()
                    .getItem(itemId)
                    .getItemProperty(propertyId)
                    .getValue().toString()));
            alSelected.add(lhmProgrammValue);
        }

    }
}

1 个答案:

答案 0 :(得分:0)

以防其他人在将来遇到此问题我发现了问题,我想分享解决方案。正如我在开始时想到的那样,问题并没有出现在clicklistener中。 _btnGenerateXML按钮链接到Vaadin com.vaadin.server.FileDownloader对象,因此元素已正确检索并添加到列表中,但filedownloader元素在生成要下载的文件时不会使用它们。我解决了这个问题,创建了一个从FileDownloader扩展的类,并重写方法handleConnectorRequest,如下所示:

public class CustomFileDownload extends FileDownloader {
 public interface OnDemandStreamResource extends StreamSource {
    String getFilename ();
  }

  private static final long serialVersionUID = 1L;
  private final OnDemandStreamResource onDemandStreamResource;

  public CustomFileDownload (OnDemandStreamResource onDemandStreamResource) {
    super(new StreamResource(onDemandStreamResource, ""));
    this.onDemandStreamResource = onDemandStreamResource;
   }

  @Override
  public boolean handleConnectorRequest (VaadinRequest request, VaadinResponse response, String path)
  throws IOException {
    getResource().getStream().setParameter("Cache-Control", "private,no-cache,no-store");
    getResource().getStream().setParameter("Pragma", "no-cache");
    getResource().setCacheTime(0);
    getResource().setFilename(onDemandStreamResource.getFilename());
    return super.handleConnectorRequest(request, response, path);
  }
}

希望有所帮助