0

我在使用 vaadin-gird 时遇到了一种奇怪的行为。我只是想将来自网格中选定行的信息(gRegisteredRecords)存储到 LinkedHashMaps 的 arrayList 中。出于任何原因,当我第一次单击按钮 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);
        }

    }
}
4

1 回答 1

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);
  }
}

希望能帮助到你

于 2017-08-21T11:13:39.497 回答